The Python Virtual Environments system is a tool to make sure that packages related to different projects are stored in different places so that they do not conflict. A Python Virtual Environment is isolated from your other python projects or environments and allows for that environment to have its own dependencies to remain independent.
WINDOWS
Getting Started
- Install Python
- If you are on an ENGINEERING Windows Desktop, install Python 2.7 through software center
- Add Python to PATH
- Navigate: Control Panel -> User Accounts -> Change my Environment Variables
- Inside the top window labeled 'User Variables' select 'Path' and edit it
- Select 'Browse' and navigate to C:\python27 and select 'ok'
- Select 'Move Down' and repeat step 3 for C:\python27\Scripts
- Open a new CMD prompt (Windows Key + R, cmd.exe)
- Install virtualenv through the command pip install virtualenv. If you are not an administrator do not update if prompted
- Check that virtualenv is installed through the command pip --version
- Install virtualenvwrapper-win through the command pip install virtualenvwrapper-win
Test Functionality
- Create a test environment with mkvirtualenv example, and this will make a folder called Envs in your user directory
- Change your python directories to a project folder in command prompt and run setprojectdir to set that as your development folder.
- To leave an environment use deactivate
- You can enter an environment by using workon example and from there can invoke package installations like pip install flask
- To completely delete an environment make sure you leave the environment first and use rmvirtualenv example to delete the environment
LINUX
Getting Started
Virtual Environments on Python 2.7
- Create a virtual environment in your current directory for a project with the command: virtualenv my_project
- "my_project" is whatever name you would like to give this environment
- To create a virtual environment with a specific version of python use the command: virtualenv -p /usr/bin/python2.7 my_project
- "python2.7" can be replaced with the version you would like to use
Virtual Environments on Python 3.5+
- Create a virtual environment in your current directory for a project with the command: python3 -m venv my_project
- "my_project" is whatever name you would like to give this environment
Using the Virtual Environment
Bash shell:
- Start the virtual environment by activating with the command: source my_project/bin/activate
csh /tcsh:
- Start the virtual environment by activating with the command: source my_project/bin/activate.csh
Pip can now be used as usual for package installs
- When you run 'python' it will be the version specified in Step 1
- When you run 'pip', you're running the correct version
- All modules are installed inside that environment
When you are finished working in the environment, exit using the command: deactivate
For more details visit: http://docs.python-guide.org/en/latest/dev/virtualenvs/
or download the PDF: python-guide.pdf