Ubuntu – pyvenv vs venv vs python-virtualenv vs virtualenv and python 3

aptpippythonvirtualenv

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

Best Answer

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 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
    python3 -m pip 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.


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
python2 -m pip install package-name
Related Question