Python Virtual Environments

Python Virtual Environments: A Primer

# Install
$ pip install virtualenv

# Python 3
$ python3 -m venv env

$ source venv/bin/activate
(env) $

(env) $ deactivate
$
$ 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 *