Python Virtual Environments

Python Virtual Environments: A Primer

# Install
$ pip install virtualenv

# Python 3 using apt install
$ python3 -m venv myapp

# Python 3 using pip install
$ virtualenv -p python3 myapp

$ source myapp/bin/activate
(env) $

(env) $ deactivate
$
$ python3 -m venv myapp
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/XXX/myapp/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']
$ pip install virtualenv
Defaulting to user installation because normal site-packages is not writeable
Collecting virtualenv
  Downloading virtualenv-20.0.20-py2.py3-none-any.whl (4.7 MB)
Collecting distlib<1,>=0.3.0
  Downloading distlib-0.3.0.zip (571 kB)
Collecting importlib-metadata<2,>=0.12; python_version < "3.8"
  Downloading importlib_metadata-1.6.0-py2.py3-none-any.whl (30 kB)
Collecting appdirs<2,>=1.4.3
  Downloading appdirs-1.4.4-py2.py3-none-any.whl (9.6 kB)
Collecting filelock<4,>=3.0.0
  Downloading filelock-3.0.12-py3-none-any.whl (7.6 kB)
Collecting importlib-resources<2,>=1.0; python_version < "3.7"
  Downloading importlib_resources-1.5.0-py2.py3-none-any.whl (21 kB)
Requirement already satisfied: six<2,>=1.9.0 in /usr/lib/python3/dist-packages (from virtualenv) (1.11.0)
Collecting zipp>=0.5
  Downloading zipp-3.1.0-py3-none-any.whl (4.9 kB)
Building wheels for collected packages: distlib
  Building wheel for distlib (setup.py) ... done
  Created wheel for distlib: filename=distlib-0.3.0-py3-none-any.whl size=336972 sha256=09fc154d5bab1f792599644aa5c06884f122de197a99cc8545827150f1ca945a
  Stored in directory: /home/bacr/.cache/pip/wheels/33/d9/71/e4e3cac73529e1947df418af0f140cd7589d5d9ec0e17ecfc2
Successfully built distlib
Installing collected packages: distlib, zipp, importlib-metadata, appdirs, filelock, importlib-resources, virtualenv
Successfully installed appdirs-1.4.4 distlib-0.3.0 filelock-3.0.12 importlib-metadata-1.6.0 importlib-resources-1.5.0 virtualenv-20.0.20 zipp-3.1.0
WARNING: The script virtualenv is installed in '/home/bacr/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
WARNING: You are using pip version 20.0.2; however, version 20.1.1 is available.
You should consider upgrading via the '/usr/bin/python3 -m pip install --upgrade pip' command.
$ which python
/usr/bin/python
(env) $ which python
/home/andreas/my_python_project/venv/bin/python
>>> import sys
>>> sys.prefix
'/usr'

>>> import site
>>> site.getsitepackages()
[
  '/usr/local/lib/python3.6/dist-packages',
  '/usr/lib/python3/dist-packages',
  '/usr/lib/python3.6/dist-packages'
]
>>> import sys
>>> sys.prefix
'/home/andreas/my_python_project/venv'

>>> import site
>>> site.getsitepackages()
[
  '/home/andreas/my_python_project/venv/lib/python3.6/site-packages',
  '/home/andreas/my_python_project/venv/local/lib/python3.6/dist-packages',
  '/home/andreas/my_python_project/venv/lib/python3/dist-packages',
  '/home/andreas/my_python_project/venv/lib/python3.6/dist-packages'
]

Leave a Reply

Your email address will not be published. Required fields are marked *