12
malikarumi@Tetouan2:~$ pip install virtualenv
    Collecting virtualenv
    Downloading virtualenv-12.0.7-py2.py3-none-any.whl (1.8MB)
    100% |################################| 1.8MB 330kB/s 

malikarumi@Tetouan2:~$ pip freeze
    (a lot of stuff, but not virtualenv)

malikarumi@Tetouan2:~$ virtualenv testvenv1
    The program 'virtualenv' is currently not installed. You can install it by typing:
    sudo apt-get install python-virtualenv

What is going on here? is python-virtualenv == pyvenv? Isn't that still broken? Does original virtualenv still work with python? if venv (too many name variations!!!) is part of the standard library, https://docs.python.org/3/library/venv.html, why am I being told to install it?

And when I did try to install it, I got:

malikarumi@Tetouan2:~$ sudo apt-get install python-virtualenv
    Reading package lists... Done
    Building dependency tree       
    Reading state information... Done
    The following extra packages will be installed:
    python-colorama python-distlib python-html5lib python-pip python-setuptools

At which point I aborted, because installing python3-pip just before that had given me

Setting up python3-setuptools (3.3-1ubuntu1) ...
Setting up python3-pip (1.5.4-1) ...

And I wasn't sure if the extra packages would overwrite them or go onto python 2.7.

I read that python 3 is supposed to be the default going forward. Since both 2.7 and 3.4 are there, and have separate commands, how do I not only make sure Python 3 is my default, but that anything I install goes there and is used by it instead of 2.7?

BTW, I tried sudo apt-get python3-virtualenv and got: E: Unable to locate package python3-virtualenv

  • 1
    I'd still like to know what the difference - if any - is between all these different but similarly named virtual environment packages. – Malik A. Rumi Apr 2 '15 at 23:20
  • I'm using 14.04, so this answer helped me. – FriendFX Feb 11 '17 at 13:10
3

There is no python3-virtualenv package in the Ubuntu repositories. There is a python-virtualenv package in the Ubuntu repositories, but that package won't work for installing Python 3.x packages in a Python virtual environment. python-virtualenv only works for installing Python 2.x packages in a Python virtual environment.

The way to install Python 3.x packages in a Python virtual environment is to create the Python virtual environment using Python 3 virtual environment creator (python3-virtualenv). python3-virtualenv is in the default Ubuntu repositories in Ubuntu 14.10 and later.


Install Python 3.x packages in a Python virtual environment in Ubuntu 14.04

sudo apt-get install virtualenvwrapper  
gedit .bashrc

Add the following line to the end of .bashrc.

source /usr/share/virtualenvwrapper/virtualenvwrapper.sh

Save the changes to .bashrc and close gedit. Source your .bashrc for the changes to take effect.

cd 
source .bashrc

Create a Python virtual environment for python3. You can only install python3 packages inside this Python virtual environment. If you also want to install Python 2.x packages, then you need to make another Python virtual environment.

mkvirtualenv py3 -p /usr/bin/python3

The new Python virtual environment for python3 will be created in the ~/.virtualenvs/py3 directory. .virtualenvs is a hidden folder.

Install a package.

cd ~/.virtualenvs/py3
source bin/activate
pip3 install package-name

Install Python 3.x packages in a Python virtual environment in 16.04 and later

  1. Install Python 3 virtual environment creator

    sudo apt install virtualenv python3-virtualenv
    
  2. Create a Python virtual environment for python3. You can only install python3 packages inside this Python virtual environment. If you also want to install Python 2.x packages, then you need to make another Python virtual environment.

    virtualenv -p python3 env  
    source ./env/bin/activate
    

    The new Python virtual environment for python3 will be created in the env directory which is located in the current directory.

  3. Install Python packages.

    cd /path/to/env/ # env is the python3 virtual environment's directory
    source bin/activate
    pip3 install first-package-name next-package-name last-package-name

This is a new python3 virtual environment with the latest version of pip3, so installing Python packages in it will work great.

  • 4
    This doesn't seem to be correct. Vivid (15.04) has python3-virtualenv and python3-venv packages available. I'm assuming python3-venv is the correct package, in which case I have no idea what purpose python3-virtualenv serves. – knite May 26 '15 at 7:17
  • Incorrect. You can use virtualenv (executable) to create environment with arbitrary interpreter, including PyPy, just fine E.g. virtualenv -p python3 newenv. – saaj Feb 18 '17 at 13:22
0

Well, it gets complicated. The following is shown against Ubuntu Xenial (which is underlying release for Linux Mint I use). Let's sort it out:

pyvenv

It refers to the Python 3.3+ stdlib package's wrapper script. But it was deprecated since Python 3.6.

It is also a PyPi package, which is a virtual environment switcher.

venv

It's the Python 3.3+ stdlib package whose purpose was to improve and replace the PyPi virtualenv package (see PEP 405). But it seems it's not there yet (at least not as feature-complete).

python-virtualenv and virtualenv

$ apt-cache show virtualenv | grep Depends
Depends: python3, python3-virtualenv

As you can see it runs on Python 3. python-virtualenv package is meant to be used like python2 -m virtualenv py2. Let's see how virtualenv works on fresh Ubuntu Xenial (docker run --rm -it ubuntu:xenial):

$ virtualenv
bash: virtualenv: command not found
$ apt-get update
...
$ apt-get install --yes --no-install-recommends virtualenv
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  ca-certificates dh-python libexpat1 libmpdec2 libpython3-stdlib
  libpython3.5-minimal libpython3.5-stdlib libsqlite3-0 libssl1.0.0
  mime-support openssl python-pip-whl python3 python3-minimal
  python3-pkg-resources python3-virtualenv python3.5 python3.5-minimal
Suggested packages:
  libdpkg-perl python3-doc python3-tk python3-venv python3-setuptools
  python3.5-venv python3.5-doc binutils binfmt-support
Recommended packages:
  file
The following NEW packages will be installed:
  ca-certificates dh-python libexpat1 libmpdec2 libpython3-stdlib
  libpython3.5-minimal libpython3.5-stdlib libsqlite3-0 libssl1.0.0
  mime-support openssl python-pip-whl python3 python3-minimal
  python3-pkg-resources python3-virtualenv python3.5 python3.5-minimal
  virtualenv
...
$ virtualenv test
The executable python2 (from --python=python2) does not exist
$ virtualenv -p python3 py3 
Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /py3/bin/python3
Also creating executable in /py3/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
$ . py3/bin/activate
(py3) $ python --version
Python 3.5.2
(py3) $ deactivate
$ apt-get install --yes --no-install-recommends python    
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libffi6 libpython-stdlib libpython2.7-minimal libpython2.7-stdlib
  python-minimal python2.7 python2.7-minimal
Suggested packages:
  python-doc python-tk python2.7-doc binutils binfmt-support
The following NEW packages will be installed:
  libffi6 libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python
  python-minimal python2.7 python2.7-minimal
...
$ virtualenv py2 
Running virtualenv with interpreter /usr/bin/python2
New python executable in /py2/bin/python2
Also creating executable in /py2/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
$ . py2/bin/activate
(py2) $ python --version
Python 2.7.12

As you can see even though virtualenv package is using python3-virtualenv, its default is to create Python 2 environment.

  • I have also noticed the same thing on my machine which was running Ubuntu 14.04 at the time. - "As you can see even though virtualenv package is using python3-virtualenv, its default is to create Python 2 environment." – karel Feb 18 '17 at 14:08

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.