Skip to content

Setting Up a Python Virtual Environment

Jason Sewell edited this page Feb 25, 2017 · 8 revisions
  1. First we want to use pip to install a package called virtualenv which we will use to install a specific version of Python and any pip modules to any specific project.

To install virtualenv run the following command at your command line prompt:

  • pip install virtualenv
  1. Next we want to use this new utility to create our first virtualenv.

To create a virtual environment, take the following steps:

  • mkdir project
  • cd project
  • virtualenv venv

You should see something similar to the screenshot below if everything went well.

virtualenv

To create a virtual environment with a specific version of Python, use one of the following commands:

  • virtualenv -p /usr/bin/python2.7 venv
  • virtualenv -p /usr/bin/python3 venv

virtualenv python 3

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

virtualenv 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!! 🎉

Clone this wiki locally