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

17,047

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 venv  
     source ./venv/bin/activate
    

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

  3. Install Python packages.

    cd /path/to/venv/ # venv 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
Share:
17,047

Related videos on Youtube

Malik A. Rumi
Author by

Malik A. Rumi

Updated on September 18, 2022

Comments

  • Malik A. Rumi
    Malik A. Rumi over 1 year
    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

    • Malik A. Rumi
      Malik A. Rumi about 9 years
      I'd still like to know what the difference - if any - is between all these different but similarly named virtual environment packages.
    • FriendFX
      FriendFX over 7 years
      I'm using 14.04, so this answer helped me.
  • knite
    knite almost 9 years
    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.
  • saaj
    saaj about 7 years
    Incorrect. You can use virtualenv (executable) to create environment with arbitrary interpreter, including PyPy, just fine E.g. virtualenv -p python3 newenv.
  • karel
    karel about 7 years
    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."
  • reducing activity
    reducing activity over 2 years
    What is the relation between python3-venv and python3-virtualenv? The first one is recommended by Ubuntu (Command 'pyvenv' not found, but can be installed with) but installing it fails to change anything.
  • karel
    karel over 2 years
    @reducingactivity First run the command apt show python3-venv python3-virtualenv and leave the terminal open for reference. Then read the accepted answer to What is the difference between venv, pyvenv, pyenv, virtualenv, virtualenvwrapper, pipenv, etc?.