Installing venv for python3 in WSL (Ubuntu) - python

I am trying to configure venv on Windows Subsystem for Linux with Ubuntu.
What I have tried:
1) Installing venv through pip (pip3, to be exact)
pip3 install venv
I get the following error
ERROR: Could not find a version that satisfies the requirement venv (from versions: none)
ERROR: No matching distribution found for venv
2) Installing venv through apt and apt-get
sudo apt install python3-venv
In this case the installation seems to complete, but when I try to create a virtual environment with python3 -m venv ./venv, I get an error, telling me to do apt-get install python3-venv (which I just did!)
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt-get install python3-venv
You may need to use sudo with that command. After installing the python3-venv
package, recreate your virtual environment.
Failing command: ['/mnt/c/Users/Vicubso/.../code/venv/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']
I have also read the following posts
post 1,
post 2, and several others. None of these seem to solve my problem.
Any help would be much appreciated.

Nothing here worked for me, but this did in WSL2:
sudo apt-get update
sudo apt-get install libpython3-dev
sudo apt-get install python3-venv
python3.8 -m venv whatever
Good luck!

Give this approach a shot:
Install the pip:
sudo apt-get install python-pip
Install the virtual environment:
sudo pip install virtualenv
Store your virtual environments somewhere:
mkdir ~/.storevirtualenvs
Now you should be able to create a new virtualenv
virtualenv -p python3 yourVenv
To activate:
source yourVenv/bin/activate
To exit your new virtualenv, just deactivate

This was more of a headache than it needed to be. It seems that it relates to WSL<->Windows file system mapping issues. This blog post perhaps describes it better, but the net is you need to store additional metadata with files on a particular mount, as described in this MS devblog.
I fixed the issue by running:
sudo umount /mnt/c
sudo mount -t drvfs C: /mnt/c -o metadata
After which I was able to create python venv without needing to sudo.

The error occurs when you're in /mnt/XXX (under Windows part).
Switch to Linux part by cd and run python3 -m venv ./venv again and that should be fine

I was getting the same error message, I have WSL(Ubuntu) installed on my computer, finally I found this doc:
https://learn.microsoft.com/en-us/windows/python/web-frameworks#open-a-wsl---remote-window
Ironically the only difference from what I was using as command was the name, I was using venv, then I run the command again using .venv so that the files become hidden files instead, and it worked. Hopefully it'll help someone else :)

You need to install also python3.8-venv via
sudo apt install python3.8-venv
this fixed the problem for me.

Related

I used pip3 to install virtualenv but I can't create venv and also I can't uninstall virtualenv

I'm using Linux Mint 20.2 with two directories / and /home.
I used the following command to install virtualenv:
>>> sudo pip3 install virtualenv
It worked fine and it installed in the following path:
>>> virtualenv --version
virtualenv 20.0.17 from /usr/lib/python3/dist-packages/virtualenv/__init__.py
But when I tried to create an environment I got the following error:
>>> python3 -m venv article
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt install python3.8-venv
You may need to use sudo with that command. After installing the python3-venv
package, recreate your virtual environment.
Failing command: ['/home/username/article_tools/article/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']
When I tried to uninstall it to install it using [b]sudo apt install python3.8-venv[/b], I got the following error:
>>> sudo pip3 uninstall virtualenv
Found existing installation: virtualenv 20.0.17
Not uninstalling virtualenv at /usr/lib/python3/dist-packages, outside environment /usr
Can't uninstall 'virtualenv'. No files were found to uninstall.
How can I fix it? By fix, I mean installing virtualenv in a way that I don't get such errors.
The fundamental problem here seems to be that you are mixing up two different packages.
Python 3 comes with a built-in virtual environment module venv which is however not installed by default on Debian-based platforms. Like the error message says, apt-get install -y python3-venv will install this package, which you can then use with python3 -m venv.
virtualenv is a separate third-party package which you invoke with the command virtualenv. It's not a bad alternative, but if you are only just learning, I would suggest you simply ignore it for the time being.

How to install python3.7 and create a virtualenv with pip on Ubuntu 18.04?

I'm trying to set up a standard virtual-environment(venv) with python 3.7 on Ubuntu 18.04, with pip (or some way to install packages in the venv). The standard way to install python3.7 seems to be:
% sudo apt install python3.7 python3.7-venv
% python3.7 -m venv py37-venv
but the second command fails, saying:
The virtual environment was not created successfully because ensurepip
is not available. On Debian/Ubuntu systems, you need to install the
python3-venv package using the following command.
apt-get install python3-venv
You may need to use sudo with that command. After installing the
python3-venv package, recreate your virtual environment.
Failing command: ['/py37-venv/bin/python3.7', '-Im', 'ensurepip',
'--upgrade', '--default-pip']
This is true; there is no ensurepip nor pip installed with this python. And I did install python3.7-venv already (python3-venv is for python3.6 on Debian/Ubuntu). I gather there has been some discussion about this in the python community because of multiple python versions and/or requiring root access, and alternate ways to install python modules via apt or similar.
Creating a venv without pip (--without-pip) succeeds, but then there's no way to install packages in the new venv which seems to largely defeat the purpose.
So what's the accepted "best practice" way to install and use python3.7 on 18.04 with a venv?
I don't know if it's best practices or not, but if I also install python3-venv and python3.7-venv then everything works (this is tested on a fresh stock Debian buster docker image):
% sudo apt install python3.7 python3-venv python3.7-venv
% python3.7 -m venv py37-venv
% . py37-venv/bin/activate
(py37-venv) %
Note that it also installs all of python3.6 needlessly, so I can't exactly say I like it, but at least it does work and doesn't require running an unsigned script the way get-pip.py does.
sudo apt install python3-venv
python3 -m venv env

Pip not working inside Virtual Env but works outside perfectly

Hello Guys I am tying to follow the installation here https://github.com/systers/portal and trying to deploy the server inside a virtual environment on my machine.
After lots of errors I decided to install a fresh copy of Ubuntu 16.04 and start
After the installation here are the things that I have installed using the given commands
I checked my current python and python3 versions using python --version and python3--version respectively and they are Python 2.7.12 and Python 3.5.2 respectively.
Easy Install. $ sudo apt-get install python-setuptools python-dev build-essential
pip. $ sudo easy_install pip
virtualenv. $ sudo pip install --upgrade virtualenv.
python3-dev tools.$sudo apt-get install python3-dev
Now after that I created a virtual env and activated it using the following commands
$ virtualenv venv1 --python=/usr/bin/python3
$ source venv/bin/activate
But now when I run the third command
$ pip install -r requirements/dev.txt
or even do
$pip --version
I get the error
bash: /media/rohan/New Volume/portal/venv1/bin/pip: "/media/rohan/New: bad interpreter: No such file or directory
Also in /venv1/bin the files pip,pip3 ,pip3.5 are present
I tried sudo easy_install pip thinking that it will install pip in the virtual environment but it installs to /usr/local/bin
Also I tried by creating a virtual env using the code
$virtualenv venv --python=/usr/bin/python
But that also doesnt work and this time also same error comes and in /venv/bin pip pip2 pip2.7 are present
PLEASE HELP
The problem appears to be that the path to your virtualenv has a space in it that isn't being escaped somewhere it should be.
Note the error you receive:
/media/rohan/New: bad interpreter: No such file or directory
So with that space in the path, it is trying to run a program that doesn't exist (/media/rohan/New) on a file that doesn't exist (Volume/portal/venv1/bin/pip).
Renaming New Volume to something without spaces like new_volume and then recreating a virtualenv should resolve this.
I had renamed the folder of virtual environment so that I was getting this error.
Then I renamed the venv folder name to the path mentioned in the error (That is the one which I had named while creating venv.) then tried to use pip and it worked.
Might be you have already solved your issue, but this is for the future visitors.

pyvenv not working because ensurepip is not available

I upgraded from ubuntu 14.04 to ubuntu 16.04 a few days ago.
When I try to create a virtual env by using
pyvenv .venv
or
python3 -m venv .venv
There is an error:
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt-get install python3-venv
You may need to use sudo with that command. After installing the python3-venv
package, recreate your virtual environment.
Failing command: ['/home/user/.venv/bin/python3.5', '-Im', 'ensurepip', '--upgrade', '--default-pip']
I tried running both
sudo apt-get install python3-venv
and
sudo apt-get install python3.5-venv
but it did not solve my problem.
try installing python3.6-venv:
sudo apt-get install python3.6-venv
It seems that it was a locale problem. Solved by executing:
export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
sudo dpkg-reconfigure locales
found on this thread Python locale error: unsupported locale setting
Under Windows Linux Subsystem and Ubuntu 18.04, this was caused by my not having upgraded recently.
I ran:
sudo apt update
sudo apt upgrade
Then sudo apt install python3-venv worked.
Note that I had also tried the UTF-8 solution beforehand (I made it part of my .bashrc), so that could have been a contributing factor.
One of the other answers fixed it for me last time, but with Python 3.7 I had to do:
apt install python3-pip python3-setuptools python3.7-venv
Followed by
python3.7 -m venv /path/to/venv
Resolved similar problems on Ubuntu18 when came upon this answer. It is similar to the one that worked for #Niko Rikken, except it doesn't really need any new PPA's and "python3.8-distutils" package. I was installing new python3.8 environment with venv and I already had "python3-venv" installed and up to date, so my solution was to install only "python3.8-venv":
% sudo apt-get install python3.8-venv
And that got this lines working:
% python3.8 -m venv ~/envs/new_env
% source ~/envs/new_env/bin/activate
In my case the next steps worked:
Ubuntu 18.04.4 LTS
$ sudo apt-get install python3-venv python3.7-venv
$ python3.7 -m venv [your_path_to_virtual_env_here]
In case this helps anyone down the line, I was getting the same error on Ubuntu 18.04. Setting the locales didn't work and trying to install python3-venv gave the error:
$ sudo apt-get install python3-venv
Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:
The following packages have unmet dependencies:
python3-venv : Depends: python3.6-venv (>= 3.6.5-2~) but it is not going to be installed
Depends: python3 (= 3.6.5-3) but 3.6.7-1~18.04 is to be installed
E: Unable to correct problems, you have held broken packages.
And it looks like the apt repository had two versions of python:
$ apt list python3 -a
python3/bionic-updates,now 3.6.7-1~18.04 amd64 [installed]
python3/bionic 3.6.5-3 amd64
I tried to install Python3.6.5-3 but apt wanted to uninstall every dependency. I was able to solve the problem by installing Python3.7 and creating the venv with that:
$ sudo apt-get install python3.7 python3.7-venv
$ python3.7 -m venv my_venv
I encountered this problem on Ubuntu 18.04 for the recent release of Python-3.8. My solution was to add the Deadsnakes PPA which supplies the required python3.8-distutils package. The python3.8-venv package is already in the repository. Thanks to this blogpost:
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.8
sudo apt install python3.8-distutils
sudo apt install python3.8-venv
Note: This is only a temporary solution. In the near future the required python3.8-distutils package will probably be available in the default Ubuntu repository.
Edit:
For Ubuntu 20.04 LTS the python3-distutils package is based on Python 3.8. As of yet there is no Python 3.8 package distutils package available for Ubuntu 18.04 LTS.
As mentioned in other comments on this thread, distutils might not be required. It was for my use-case, but please consider that solution before adding additional PPA's.
First, make a directory :
mkdir testing
Then, moved to this directory named testing :
cd testing
When you type following command in this directory:
python3 -m venv env
You got error like :
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt install python3.8-venv
Type the following command but before that keep an eye on the version of python you installed on the machine; in my case its python3.8
sudo apt install python3.8-venv
Now, we can create a virtual environment and store its tools in the "bhandari" folder .
python3 -m venv bhandari
Note: you can named this "bhandari" folder; anyname you like( Standard practice is to name it "env" ...)
Now to activate your virtual environment, from the directory of your folder, type the following command this will activate our virtual environment in the “bhandari” folder
source bhandari/bin/activate
If you have successfully activated your virtual environment, you should see the (bhandari) word indicating that we are working in a virtual environment.
After this, we can install anything that will be isolated from the rest of the system....
I was faced with the same problem and I am searching for a solution. It is about the problem:
ensurepip is disabled in Debian/Ubuntu for the system python.
And this my solution:
python3 -m venv myvenv --without-pip --system-site-packages
Try the following commands:
sudo apt install python-virtualenv
virtualenv --python=python3.6 myenv
These commands might work for you.
If you get any error like E: Unable to locate package python3-venv
Then try the following commands:
sudo apt install python3.6-venv
Python updated or Default python changed and venv already installed
the venv installed on your system is installed by your previous python version(let say python3.6). that's why venv is not working with current python version(lets say 3.8).
so first check your default python or python3 version,( suppose ur current version is python3.8).
reinstall virtual environment by mentioning current python version(3.8) as follow
sudo apt-get install python3.xx-venv >> replace xx with your current/default python version
if this helps you plz upvote, I'm new on this platform.
Try : python3.* -m venv myvenv -
And don't forget to replace * with your specific version of python
I had to mention the specific version of python and replace python3.10 with you version
$ sudo apt-get update -y && sudo apt-get upgrade -y
$ sudo apt-get install python3.10-venv
Creating a virtual environment
$ python3.10 -m venv --system-site-packages Project_Name
If your intention was to get python3.8 incl. pip and venv on Ubuntu 18.04:
sudo apt install python3.8 python3.8-venv python3-pip # there is no python3.8-pip package
python3.8 -m venv venv
source venv/bin/activate
python --version # -> python 3.8.0
pip --version # -> pip 9.0.1 from /home/user/venv/lib/python3.8/site-packages (python 3.8)
Try installing python3-distutils as well.
Altogether,
for python 3.8, the following worked for me.
$ apt-get install python3.8 python3.8-venv python3.8-distutils python3.8-dev
I just ran across this issue on several Debian/Ubuntu systems. Same error as above.
Findings
I tried to create a venv manually with:
python -m venv venvdir
This failed in the same way as others have mentioned. But it did create the shell of the venv. So I tried running ensurepip:
venvdir/bin/python -m ensurepip
Traceback (most recent call last):
File "/usr/lib/python3.9/runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/lib/python3.9/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/usr/lib/python3.9/ensurepip/__main__.py", line 5, in <module>
sys.exit(ensurepip._main())
File "/usr/lib/python3.9/ensurepip/__init__.py", line 266, in _main
return _bootstrap(
File "/usr/lib/python3.9/ensurepip/__init__.py", line 166, in _bootstrap
copy_wheels(dependencies, venv_wheel_dir, sys.path)
File "/usr/lib/python3.9/ensurepip/__init__.py", line 144, in copy_wheels
assert len(wheel_names) == 1, wheel_names
AssertionError: ['/usr/share/python-wheels/pyparsing-2.2.0-py2.py3-none-any.whl', '/usr/s
hare/python-wheels/pyparsing-2.4.7-py2.py3-none-any.whl']
This seemed odd. A quick look into /usr/share/python-wheels/ revealed - indeed - multiple versions of the same wheel as the error suggests. I have no clue why ensurepip is also ensuring that it only finds one wheel file, go figure.
Solution
A check of dpkg -S /usr/share/python-wheels indicated that the python-pip-whl package is the source of those files. This appears to be true for both Ubuntu and Debian.
So, I did:
cd /usr/share
sudo mv python-wheels python-wheels.old
sudo apt reinstall python-pip-whl
And it worked - no more errors. There are no longer duplicates in /usr/share/python-wheels
Absolutely no clue how there were duplicates or why ensurepip is so sensitive to duplicates there... Probably a package upgrade gone wrong somewhere.
I had the same problem - the python env has 2 versions for 2.7 and 3.6.
All you need to do is:
Install the latest version of pip by installing pyenv installer
Make sure you follow the steps of installing pyenv found here
Good Luck!
I had the same problem for an existing project when executing python3 -m venv venv. I had just updated my Ubuntu and Python versions. After removing the already existing venv folder the issue was solved. (I have also tried the UTF-8 solution.)
My problem were related to permissions and ownership.
I was logged in with a different user as the owner of the current directory, which led to this error.
After reviewing and fixing all permissions I was able to install the venv regularl
This worked for me...
Firstly, I ran
sudo apt-get update
Then
sudo apt-get install -y python3-venv zip
Ran into the same issue recently. None of the solutions mentioned above worked for me. I eventually get it to working by installing pip3.
apt-get install python3-pip
# then run
python3.8 -m venv env
If you came across this issue while trying to run python -m build to build a python package, this means there probabaly is a syntax issue in your setup.cfg or setup.py file that causes an error creating the temporary venv required for installing dependencies.
Using pip wheels . will give you a less misleading error message.
For Linux, it is not installed by default you have to install venv
// at first check python version
python --version
// install
sudo apt update
sudo apt install python3.8-venv
sudo apt install python3.8-distutils
// create new env
python3 -m venv project-name
source project-name/bin/activate
All of these suggestions didn't help me.
$ apt list python3 -a
python3/now 3.6.7-1~18.04 amd64 [installed,local]
python3/bionic 3.6.5-3 amd64
So I did: sudo apt-get install python3/bionic
Now I have python 3.6.5 and apt-list showed a better list:
$ apt list python3 -a
python3/bionic 3.6.5-3 amd64
With sudo apt-get install python3-venv/bionic I could install pythno3-venv and everything worked.
In my case, running sudo apt-get install python3.8-venv succeeds but it shows the same error when running python3 -m venv .venv.
Finally, this command works out without changing locale.
python3.8 -c 'import venv; venv.create(".venv", with_pip=True)'
Here is my answer for Ubuntu 14.04. I was able to make venv and pip work with various Python versions. Details:
3.4: Ubuntu 14.04 has Python 3.4 (as package python3.4 etc.). It works:
$ sudo apt-get install python3.4 python3.4-dev python3.4-venv gcc libc6-dev
$ mkdir /tmp/try3.4
$ python3.4 -m venv /tmp/try3.4
$ . /tmp/try/bin/activate
(try3.4) $ pip install print-hello-world
...
(try3.4) $ print-hello-world
Hello World!
If python3.4-venv is removed from the apt-get install command above, then python3.4 -m venv displays the same error message as in the question. However, the error message mentions apt-get install python3-venv to solve it, but that doesn't work, there is no such package. (The correct package name is python3.4-venv.)
Please note that Python 3.4 is fairly old, and some Python packages available in PyPI (via pip) don't work with it.
3.5: It can be installed from the deadsnakes repository. It works:
$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt-get update
$ sudo apt-get install python3.5 python3.5-dev python3.5-venv gcc libc6-dev
$ mkdir /tmp/try3.5
$ python3.5 -m venv /tmp/try
$ . /tmp/try/bin/activate
(try3.5) $ pip install print-hello-world
...
(try3.5) $ print-hello-world
Hello World!
3.6: Ditto, it can be installed from the deadsnakes repository. It works:
$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt-get update
$ sudo apt-get install python3.6 python3.6-dev python3.6-venv gcc libc6-dev
$ mkdir /tmp/try3.6
$ python3.6 -m venv /tmp/try3.6
$ . /tmp/try3.6/bin/activate
(try3.6) $ pip install print-hello-world
...
(try3.6) $ print-hello-world
Hello World!
3.7: It doesn't work, because pip install fails with import _ssl, and python3.7 in the deadsnakes repo doesn't have that module, because Ubuntu 14.04 ships with on old version of OpenSSL which Python 3.7 doesn't support. See more details in this bug.
3.8--: No Ubuntu 14.04 package for these Python versions in the Ubuntu or deadsnakes repositories.
In my case, the command failed because I was still in a virtual env but did not notice it very quickly.
If it is the case, try calling deactivate.
I do not have sudo rights and I have to use python3.5:
Install virtualenv: pip3 virtualenv
Create virtualenv without pip: python3 -m venv --without-pip <path>
Downloaded the proper pip bootstrap: https://bootstrap.pypa.io/pip/3.5/get-pip.py
Run: <path>/bin/python3 get-pip.py
I did 'source <path>/bin/activate' and I got a working environment.
The solution for installing python3-venv is accurate since debian/ubuntu split the python distribution across multiple packages, so you do not actually have a full python install. If you really do not want to install this apt package, here is an alternative
python3 -m pip install virtualenv
virtualenv .venv
This will create fully functioning venv.

How I can make apt-get install to my virtualenv?

It it's possible, of course.
For example - I can download python-dbus like this:
$ sudo apt-get download python-dbus
But what I should to do next, with this .deb package in my current virtualenv?
If you really need to do it this way, you can just copy the files that get installed globally directly into your virtualenv. For example I couldn't get pycurl working since the required libraries weren't installing, but apt-get install python-pycurl did. So I did the following:
sudo apt-get install python-pycurl
cp /usr/lib/python2.7/dist-packages/pycurl* ~/.virtualenvs/myenv/lib/python2.7/site-packages/
The install said it was adding it to /usr/lib/python2.7. So I looked in that directory for a site-packages or dist-packages with pycurl, after looking at the files I copied them into my virtualenv. You'd have to also copy any executables from bin into your virtualenv's bin directory.
Also, running a pip install -r requirements.txt successfully found pycurl in there and just skipped over it as if I had installed it via pip.
To include system site packages in your existing virtual environment open the config file:
<PATH_TO_YOUR_VENV_FOLDER>/pyvenv.cfg
and change false to true for include-system-site-packages
include-system-site-packages = true
Save and reload your virtual environment.
(tested with virtualenv 20.2.2 on Raspbian GNU/Linux 10 (buster) to pull in python3-pyqt5 installed with apt into my virtual environment)
If it is for a new environment #Joshua Kan's answer using the --system-site-packages flag with the venv command is probably what you want.
Why would you want to do this? The whole point is to avoid doing stuff like that...
virtualenv whatever
cd whatever
bin/pip install dbus-python
You may also choose to specify --no-site-packages to virtualenv to keep it extra isolated.
An alternative solution is to install globally, then followed by allowing the virtualenv to be able to see it.
As an example, let's say we want to install matplotlib for Python 3:
sudo apt update # Update first
sudo apt install python3-matplotlib # Install globally
sudo pip3 install -U virtualenv # Install virtualenv for Python 3 using pip3
virtualenv --system-site-packages -p python3 ./venv #the system-site-packages option allows venv to see all global packages including matplotlib
source ./venv/bin/activate #activate the venv to use matplotlib within the virtualenv
deactivate # don't exit until you're done using the virtualenv
First install the dbus development libraries (you may need some other dev libraries, but this is all I needed)
sudo apt-get install libdbus-1-dev libdbus-glib-1-dev
Next, with your virtualenv activated, run the following. It'll fail but that's ok.
pip install dbus-python
Finally, go into your virtualenv's build directory and install it the non-pythonic way.
cd $VIRTUAL_ENV/build/dbus-python
chmod +x configure
./configure --prefix=$VIRTUAL_ENV
make
make install

Categories

Resources