~/Blog

Brandon Rozek

Photo of Brandon Rozek

PhD Student @ RPI studying Automated Reasoning in AI and Linux Enthusiast.

Python Virtual Environments

Published on

Updated on

Warning: This post has not been modified for over 2 years. For technical posts, make sure that it is still relevant.

Dependency management is hard. Luckily with Python there is a program called virtualenv that can help isolate different projects and manage dependencies.

Commands

To create a new python 3.7 environment type in the following:

virtualenv --python=python3.7 environment_name

Of course you can replace the python version with whichever version you like. Now to go into the environment do the following:

source environment_name/bin/activate

This now sets up your python interpreter and other utilities to use the installation in the environment_name folder. You can now install python packages using pip and have it only reside in this environment.

To save all currently installed packages into requirements.txt:

pip freeze > requirements.txt

You can then install those packages in a different virtualenv session with:

pip install -r requirements.txt

You can leave the virtualenv session with:

deactivate
Reply via Email Buy me a Coffee
Was this useful? Feel free to share: Hacker News Reddit Twitter

Published a response to this? :