Python: Creating Virtual Environments and Managing Packages

Creating the Virtual Environment

Using virtualenv
virtualenv [env_name]
source [env_name]/bin/activate

Using pyvenv (includes pip in the default packages)
pyvenv [env_name]
source [env_name]/bin/activate

Managing Packages

Install the latest version of package:
pip install [package_name]

Alternatively, you can use python -m to install a package using pip module:
python -m [module_name] install [package_name]

Install a specific version of a package
pip install [package_name]==[version]

If you would like to install the flask package with a minimum version, this is how to execute it:
pip install flask>=0.10.1

Install packages just for the current user:
pip install --user [package_name]

Upgrade a package to the latest version:
pip install --upgrade [package_name]

Remove package(s) from the virtual environment:
pip uninstall [package1] [package2] ... [package]

Display information about a particular package:
pip show [package]

Display all the packages installed in the virtual environment:
pip list

Alternatively, use the one that uses the format that pip install expects:
pip freeze > requirements.txt

The requirements.txt can then be committed to version control and shipped as part of an application. Users can then install all the necessary packages with install -r:
pip install -r requirements.txt

Comments