HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

5) Virtual Environments and Packages Lesson

What is Python Venv

12 min to complete · By Martin Breuss

You can automatically generate the folder structure for a virtual environment (venv) in one line of code, activate it with a second command, and deactivate it with yet another one. To get started working with virtual environments, you only need to know these three commands, and you'll learn them in this lesson.

Creating a Python Virtual Environment

To create a new virtual environment that is set up for all your Python development needs, you'll need to run the following command:

python3 -m venv your_venv_name

This is a Bash command where you're using the program python3, which is your system-wide installation of Python 3, to call one of its built-in modules (-m) called venv. The venv module that comes with Python's standard library allows you to create virtual environments. As the final part of this command, you need to provide a name for your new virtual environment.

It's a default to call your virtual environments env or venv, but you could name them anything you want. You'll just need to remember what you called it because you need its name to activate it in the next step.

Illustration of a lighthouse

Note: Make sure that your virtual environment's path does not include any whitespace. Otherwise, the executables won't be found, and you might end up accidentally using the system-wide install of pip. See this StackOverflow answer for more information.

It's strongly suggested to stick with a default name, and this course will use venv as a name for any virtual environments created. Therefore, to create a virtual environment with that default name, you'd run the following command:

python3 -m venv venv

Give it a try wherever you are currently in your CLI. You won't break anything.

After you successfully run the command, navigate to the new folder that was created. It'll be named venv if you have followed the instructions above.

Inspect the folder using your Finder, Explorer, or CLI. Take a look into the bin/ subfolder on macOS and Linux or the Scripts\ folder on Windows, respectively. You might find that every freshly minted virtual environment comes with executable files for the Python interpreter, as well as pip.

Activating a Python Virtual Environment

You are now the proud owner of a virtual environment. But beware! You are not yet inside this safe space that you created! In order to enter your new venv, you'll have to activate it.

macOS and Linux

You can activate a virtual environment that you named venv by typing the following command on macOS and Linux:

source venv/bin/activate

The source command runs the activate script of your venv. You might have seen it inside of your venv's bin/ folder.

Windows

On Windows, the command looks a little bit different because the activation script is located in a different folder:

venv\Scripts\activate

Just like on UNIX systems, however, you're also executing a script called activate. On Windows, that script lives in a directory that's aptly named Scripts\.

The activation script grants you access to the safe kingdom of venv. After the command has finished executing, you'll see that your command prompt has slightly changed. As an indicator that you are now inside of the virtual environment, you'll see the name of your venv in brackets in front of each new line in your terminal:

Bash terminal showing an activated virtual environment prompt

Depending on the shell that you are using in your terminal, it might look different. But the concept is the same. As soon as you can see your virtual environment's name in brackets next to your command line prompt, you can finally start breathing again. You have arrived in the safe kingdom of venv.

Now you're ready to do all the work you need to do on the project for which you created the venv. From the terminal window where you activated your venv, you can now install all the packages you need:

python3 -m pip install mypy

The external packages are installed inside your venv. This means that when you deactivate your venv, they won't interfere with other packages you installed system-wide. It also means they'll be waiting for you when you re-activate your venv the next time you want to work on this project. And if you want to get rid of everything, all you need to do is delete the venv folder.

Deactivating a Python Virtual Environment

To return to your normal system's environment, you only need to type one passphrase into the console:

deactivate

The indication that you're in the venv will disappear, and your shell will show your normal prompt again. That's all there is to deactivating a venv. Welcome back!

Practice Activating Your Own Venv

  • Use your terminal to create a new folder called test.
  • Navigate into the folder with your CLI.
  • Create a venv called venv inside of the test folder.
  • Activate your venv and notice how the command prompt in your CLI changes.
  • Use pip to install mypy.
  • Open the Python interpreter and import mypy to verify that everything worked. If you don't get an error message, then the setup was successful.
  • If you want to dig deeper, then inspect the venv folder and look for where pip installed the mypy files. This is the location where your packages are installed when you run python3 -m pip install <package> while you have your venv activated.
  • Deactivate your venv and notice how the CLI prompt changes again.

Using a Python Virtual Environment With an IDE

Many IDEs, such as VS Code and PyCharm, can help you automate working with venvs. PyCharm, for example, automatically creates and activates a new virtual environment for you when you create a new project. If you use these features, you won't need to go through the process of creating and activating a virtual environment yourself.

Research the documentation of your favorite IDE and find out how it can help you to make working with virtual environments easier.

You'll notice whether or not your venv is activated in your IDE's terminal in the same way as you do in your normal Bash terminal:

VS Code Bash terminal showing an activated virtual environment prompt

The screenshot above shows a terminal window in VS Code with an activated virtual environment named venv.

Colorful illustration of a light bulb

Note: If you don't want to get out of the virtual pool quite yet (the water is warm!), then you can plunge back in for a deep dive in Python Virtual Environments: A Primer, written by yours truly.

In a future lesson, you'll learn how you can set environment variables to store information, and then you'll learn how you can combine that concept with venvs to keep sensitive information from leaking to version control.

Summary: What is Python Venv

  • There are three commands you need to know to start working with venvs:
    1. Create: python3 -m venv venv
    2. Activate: source venv/bin/activate (macOS and Linux) or venv\Scripts\activate (Windows)
    3. Deactivate: deactivate
  • You'll want to create a new virtual environment for any project that uses external third-party packages.
  • You can install them into your venv after creating and activating it.
  • When you're done working on a project, you deactivate your venv to return to your normal system-wide environment.