This article describes how to use the virtualenv and pip programs. The virtualenv program enables you to create custom Python environments, while the pip program enables you to install Python packages. By using these programs, you can ensure your Python applications have the exact environment setup that they need to function correctly.
All shared hosting accounts include support for virtualenv and pip by default.
Virtualenv allows you to create your own custom Python environments. A custom environment has its own installation directories, Python executables, packages, and modules. This means you can install any specific versions that an application may require. Because the environment is isolated, you do not have to worry about other programs installing incompatible versions or otherwise breaking the application's configuration.
To create a custom Python environment:
To create the environment, type the following command. You can replace project with any name you want:
Virtualenv sets up the Python environment and installs the pip program.
By default, virtualenv installs the system-wide Python version, which is currently version 2.6. To install a specific Python version in the environment, use the -p option. For example, to create an environment that uses Python 3, type the following command:
virtualenv -p python3 project
To use the new environment, you must activate it first. To do this, type the following command:
When the environment is active, the command prompt begins with (project), where project is the environment name.
When you are done working in the environment, type the following command:
The command prompt returns to normal.
When you set up a custom Python environment with virtualenv, the pip program is installed as well. You can use the pip installer to install a large selection of Python packages into custom environments.
To use the pip program, follow these steps:
After the environment is activated, you can use the pip program to install and manage Python packages:
pip search pkgname
For example, to search for all MySQL-related packages, type pip search mysql.
To install a Python package, type the following command. Replace pkgname with the name of the package that you want to install:
pip install pkgname
To uninstall a Python package, type the following command. Replace pkgname with the name of the package that you want to uninstall:
pip uninstall pkgname
What is the path to the Python executable? What are the correct file permissions for my Python scripts? What versions of Python are installed? Get the answers to these essential questions and more here.