Create a Python 3 virtual environment

20,508

venv is a convenience script to create virtual envs, but does not fully replace everything virtualenv can do. Using either is fine for most cases though. virtualenv is not obsolete.

python3 -m venv works after installing python3-venv because Debian strips out some scripts from the base Python package. python3-virtualenv doesn't put the virtualenv script on your path for some reason, but python-virtualenv does, and will work fine for either version.

It is not important which version virtualenv is installed for, it can produce envs for any installed version by passing -p pythonX (where X is the version). venv, being more simple, will only create envs of the version it is run from, and is only available since 3.3.

$ sudo apt-get install python-virtualenv
$ virtualenv -p python3 env
$ source ./env/bin/activate
$ sudo apt-get install python3-venv
$ python3 -m venv env
$ source ./env/bin/activate
Share:
20,508

Related videos on Youtube

Escher
Author by

Escher

Updated on September 18, 2022

Comments

  • Escher
    Escher over 1 year

    I installed python3-virtualenv on Lubuntu 15.1 using the official Ubuntu package apt-get install python3-virtualenv. The official documentation indicates that a simple virtualenv . in my project directory should be enough to create the venv. This does nothing, because which virtualenv does nothing. (Nothing from which python-virtualenv and which python3-virtualenv also).

    The package information, once installed, says that it installed /usr/lib/python3/dist-packages/virtualenv.py, so I invoke this under python3. It gives me this:

    $ python3 /usr/lib/python3/dist-packages/virtualenv.py .
    Running virtualenv with interpreter /usr/bin/python2
    New python executable in ./bin/python2
    Also creating executable in ./bin/python
    Installing setuptools, pip...done.
    

    Yep, when I look, everything in ./bin is python2. Is ubuntu's python3-virtualenv a dummy package for python2?

    The official python documentation says use a different package: python3-venv. This seems to work when I pyvenv ., with python3 stuff in the ./bin directory.

    How can I successfully create a Python 3 virtualenv?