26th June 2015 | By: aeguana
Python Virtual Environment Tutorial
This is a basic tutorial designed to help you setup a virtualenv and start using it.
The purpose of virtualenv is to isolate Python environments and make sure there are no conflicts when you have multiple applications deployed on the same machine. In short if you have two or more Python applications which require different versions of the same package can be nicely isolated by using virtualenv.
The installation process is straightforward:
pip install virtualenv
After the installation you will have a virtualenv command available, you can now start creating your first virtual environment.
This will create a folder venv_foo which will be your isolated environment. You can create as many environements as you wish. Now let’s activate the environment and start installing some packages within that environment.
pip install python-dateutil
Once you have activated the virtual environment your command line will indicate which virtual environment you are in. Everything you install within that environment will only be available when you are within that environment. You can list the installed packages within that environment by using the pip freeze.
It is also possible to run stand alone python scripts which make use of a specific virtual environment.
# setup virtual env
if os.name == ‘posix’:
bin_name = ‘bin’
# be nice to win32
bin_name = ‘Scripts’
activate_this = ‘/path/to/your/venv/%s/activate_this.py’ % bin_name
Once you are done and you want to quit the environment you can use the following command:
Deleteing a virtual environment is as simple as deleting the folder.