Chapter 2: Virtual Environments

Activating your environment

face Josiah Wang Ivan Procaccini

Once you’ve created the virtual environment, you can activate it to ‘enter’ the environment.

Activate your environment on Linux/MacOS

To activate your environment on Linux/MacOS, you run a script called activate in my_env/bin as follows:

user@MACHINE:~/ai_project$ source my_env/bin/activate
(my_env) user@MACHINE:~/ai_project$

Activate your environment on Windows

To activate your environment on Windows, you run a script called activate in my_env/Scripts as follows:

C:\Users\username\Documents\ai_project> .\my_env\Scripts\activate
(my_env) C:\Users\username\Documents\ai_project>

At this point, your console prompt should be prefixed with something like (my_env). This shows that you are currently using the version of Python in the my_env virtual environment.

When you run python or python3 with (my_env) displayed, you are running the version of Python in the my_env virtual environment. You can type which python (for Linux/MacOS) or where python (for Windows) to check which version you are using.

Leaving your environment

To deactivate a virtual environment, simply run deactivate. The (my_env) prefix will disappear, and you will go back to using your main version of Python.

(my_env) user@MACHINE:~/ai_project$ deactivate
user@MACHINE:~/ai_project$

Just activate the environment again as above when you need to use my_env.