Virtual Environments

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Virtual Environments

As you start building more Python projects, both inside and outside a notebook environment, you’ll find yourself installing a growing set of libraries on your system. This will eventually lead to problems such as not knowing which projects use which libraries and where different projects require different versions of the same library.

Creating a Virtual Environment

To create a virtual environment, go to the command line and select the directory where your project and the virtual environment will go.

python -m venv my_env

Activating the Virtual Environment

To use the virtual environment you created, you must activate it using one of the activation scripts in your virtual environment’s support directory. There are different scripts for Windows and Unix-based systems like macOS and Linux.

my_env\Scripts\activate
source my_env/bin/activate
(my_env) C:\Users\Me\Documents\Python\my-project
(my_env) ~/Documents/Python/my-project

Deactivating the Virtual Environment

The command to deactivate a virtual environment is the same, regardless of platform:

deactivate
See forum comments
Download course materials from Github
Previous: Python Libraries Demo Next: Virtual Environment Demo