-
Notifications
You must be signed in to change notification settings - Fork 11
Setting Up a Python Virtual Environment
- First we want to use
pipto install a package calledvirtualenvwhich we will use to install a specific version of Python and anypipmodules to any specific project.
To install virtualenv run the following command at your command line prompt:
pip install virtualenv
- Next we want to use this new utility to create our first
virtualenv.
To create a virtual environment, take the following steps:
mkdir projectcd projectvirtualenv venv
You should see something similar to the screenshot below if everything went well.

To create a virtual environment with a specific version of Python, use one of the following commands:
virtualenv -p /usr/bin/python2.7 venvvirtualenv -p /usr/bin/python3 venv

NOTE: That version of Python must be installed in the path specified and can be other versions as well
The final step is to activate the virtual environment so that any python/pip commands you run will use the proper version(or interpreter) of Python.
To activate a virtual environment you must be in the project directory and run the following command:
source venv/bin/activate

NOTE: You can now install any packages with pip and all packages will not be installed at the system level but in your project's venv/bin directory!! Woot woot!!
The last thing you will need is to be able to deactivate or turn off the virtual environment and return to using the system level version of Python and it's modules.
To deactivate your virtual environment simply run the following command:
deactivate
FINALMENTE: You now have a virtual environment you can turn on and off at any time!!
Woooot woooot!! 🎉