Python - virtualenv (Python Environment)


Python Virtual Environments allow Python packages to be installed in an isolated location for a particular project, rather than being installed globally.

By default (without virtual environment), all package are installed by default in the same default global site-packages directory (for instance /usr/lib/python2.7/site-packages). Therefore this is not possible to have system wide two package with two different versions. virtual environment resolves this problem.


A virtual env will install:


  • ENV/lib/ - virtual env library
  • ENV/include/ - virtual env library
  • ENV/lib/pythonX.X/site-packages/. - Linux - installed package
  • ENV/lib/site-packages/. - Windows - installed package
  • ENV/bin - executables on Linux
  • ENV/Scripts - executables on Windows

Executables are noticeably a new python, setuptools, pip,…

The python in your new virtualenv is effectively isolated from the python that was used to create it.

Example PySpark, the package are installed in

  • venv\lib\site-packages\pyspark\



There are two tools for creating Python virtual environments:

  • venv is available by default in Python 3.3
  • virtualenv needs to be installed separately, but supports Python 2.7+ and Python 3.3+


venv is available by default in Python 3.3

Example and usage:

  • venv
python3 -m venv ENV
source ENV/bin/activate


virtualenv needs to be installed separately, but supports Python 2.7+ and Python 3.3+


pip install virtualenv
  • Creation with virtualenv
# Usage:
# virtualenv [--system-site-packages] ENV
#     where: 
#        * ''ENV'' is a directory to place the new virtual environment.
#         * ''%%--system-site-packages%%'' to inherit packages from the [[..:grammar:regular#global|global site package dir]]
cd c:\tmp\
mkdir myVenv
virtualenv myVenv
Using base prefix 'c:\\python37-32'
New python executable in c:\tmp\myVenv\Scripts\python.exe
Installing setuptools, pip, wheel...


(ENV)$ deactivate
  • Remove
rm -r /path/to/ENV

Run a script


Running a script with #! /path/to/ENV/bin/python would run that script under this virtualenv’s python.

If you directly run a script or the python interpreter from the virtualenv’s bin/ directory (e.g. path/to/ENV/bin/pip or /path/to/ENV/bin/ then sys.path will automatically be set to use the Python libraries associated with the virtualenv. But, unlike the activation scripts, the environment variables PATH and VIRTUAL_ENV will not be modified.


Before running a script, you may activate it. Activating the virtual environment means running an activate script.

The script will:

  • change your $PATH so its first entry is the virtualenv’s bin/ directory
  • set the VIRTUAL_ENV variable
  • modify your shell prompt to indicate which environment is currently active. To disable this behaviour, see VIRTUAL_ENV_DISABLE_PROMPT


  • Bash
source /path/to/ENV/bin/activate
  • Dos
  • Powershell
Set-ExecutionPolicy AllSigned

Documentation / Reference

Powered by ComboStrap