For some reason, I am always forgetting the syntax for creating virtual environments for Python so I thought I would throw this up real quick as a reference.

Using virtualenv

First, create a directory that you want to install your virtual environment into. I am doing this for a flask app, so I am going to name my directory along those lines.

mkdir flaskApp2

Now, in order to use venv and the pyvenv command to create a Python 2 virtual environment you will need to NOT install pip when running the pyvenv command:

virtualenv flaskApp2

Now activate that installed virtual environment:

source flaskApp2/bin/activate

Test Pip with something like:

pip list

Using venv

Note: In the latest version of Ubuntu, version 14.04.03, the pyvenv-3.4 command seems to have gone missing! Following the instuctions in this link for an alternate syntax: http://askubuntu.com/questions/682612/pyvenv-3-4-disappeared-in-ubuntu-14-04-3.

venv is the new pre-packaged way to create virtual environments with Python 3.

If you are on Ubuntu 14.04 though you will need to install venv

irst, create a directory that you want to install your virtual environment into. I am doing this for a flask app, so I am going to name my directory along those lines.

mkdir flaskApp1

Now, in order to use venv and the pyvenv command to create a Python3 virtual environment you will need to NOT install pip when running the pyvenv command:

pyvenv-3.4 --without-pip flaskApp1

Now activate that installed virtual environment:

source flaskApp1/bin/activate

Install pip "manually" with the following:

curl https://bootstrap.pypa.io/get-pip.py | python

Deactivate the virtual environment

deactivate

Reativate the virutal environment

source flaskApp1/bin/activate

Test Pip with something like:

pip list

Comments

comments powered by Disqus