Python Virtual Environments: Outline
Python Virtual Environments: Outline
It is often useful to have one or more Python environments where you can experiment with
different combinations of packages without affecting your main installation. Python
supports this through virtual environments. The virtual environment is a copy of an existing
version of Python with the option to inherit existing packages. A virtual environment is also
useful when you need to work on a shared system and do not have permission to install
packages as you will be able to install them in the virtual environment.
Outline
Open a terminal
Setup the pip package manager
Install the virtualenv package
Create the virtual environment
Activate the virtual environment
Deactivate the virtual environment
Optional: Make the virtual environment your default Python
More: Python virtualenv documentation
Requirements
An installation of Python
Jargon
Link to Jargon page with terms: terminal
Open a terminal
https://uoa-eresearch.github.io/eresearch-cookbook/recipe/2014/11/26/python-virtual-env/ 1/3
12/04/2019 Python Virtual Environments
The method you use to open a terminal depends on your operating system.
Windows
Open the Windows Command Prompt (show path via Start menu and keyboard shortcuts)
Mac OS / Linux
Open the Terminal program. This is usually found under Utilities or Accessories.
pip -h
If you see the help text for pip then you have pip installed, otherwise download and install
pip
virtualenv mypython
Mac OS / Linux
https://uoa-eresearch.github.io/eresearch-cookbook/recipe/2014/11/26/python-virtual-env/ 2/3
12/04/2019 Python Virtual Environments
source mypython/bin/activate
Windows
mypthon\Scripts\activate
You should see the name of your virtual environment in brackets on your terminal line e.g.
(mypython).
Any python commands you use will now work with your virtual environment
deactivate
eResearch cookbook - 2 minute jekyll A template for capturing task recipes for
recipes for scientists jekyllrb repeatable scientific practices in a
your-email@domain.com consistent format and hosted in a
centralised online repository
https://uoa-eresearch.github.io/eresearch-cookbook/recipe/2014/11/26/python-virtual-env/ 3/3