Β· Steve Grice Β· quick python  Β· 1 min read

Quick Python 8: Virtual Environments

In this video, we learn how to create, manage, and remove Python Virtual Environments.

Here’s a link to the source code.


Quick Tips

Commands

python3 -m venv <path>

Creates a virutal environment located at <path>

source <path>/bin/activate

Activates a virtual environment

deactivate

Deactivates a virutal environment

Bonus: Bash Alias for Quick Switching

Paste this function into your ~/.bashrc or ~/.bash_aliases file and restart your shell. It assumes you keep your virtual environments in the ~/venv folder.

venv() {
    source ~/venv/$1/bin/activate
}

Switch to the venv with venv <NAME> and get out of it by typing deactivate.

Example:

python3 -m venv ~/venv/my-env
venv my-env
# do stuff in your environment. When done:
deactivate

If you liked this video, check out the whole Quick Python series and be sure to sign up using the form below to get notified of new posts! Thanks for checking this one out.

Contents
Back to Blog