DEV Community

Cover image for Virtual Environments in Python - All You Need To Know
Oluwatosin Kupolati
Oluwatosin Kupolati

Posted on

Virtual Environments in Python - All You Need To Know

In Python, virtual environments are a way to isolate the dependencies that are needed for each project that you work on.

It is usually considered best practice to create a separate virtual environment for each of your projects.

In the sections below, we will discuss all you need to know about virtual environments, and how to set one up with the venv module.

What is a virtual environment?

The official Python documentation states that:
“A virtual environment is used to contain a specific Python interpreter and software libraries and binaries which are needed to support a project (library or application).

These are by default isolated from software in other virtual environments and Python interpreters and libraries installed in the operating system.”

Breaking this down, this means that when you create and activate a virtual environment in your project’s directory.

It comes with its own libraries folder, and a Python interpreter, which is based on the Python version you used to create the environment.

By default, it is isolated from other virtual environments, and also from the system installed python.

How to create a virtual environment

To create a new virtual environment, we will utilise the venv module. To do this, go to your project’s directory and run the following command.

This will create a new virtual environment in a folder named .venv:

python -m venv .venv 
Enter fullscreen mode Exit fullscreen mode

The second argument is the location to create the virtual environment. In our case, we called it .venv, however it can be named anything you want.

How to activate a virtual environment

Before you can start installing or using packages in your virtual environment, you will need to activate it.

To activate a virtual environment, follow the steps below:

On Linux and MacOS, you would run:

source .venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

On Windows, you would run:

.venv\Scripts\activate
Enter fullscreen mode Exit fullscreen mode

While the virtual environment is activated, all Python commands run will use the Python interpreter contained within the virtual environment.

Also, when installing packages with pip, packages will not be installed globally, but rather into the virtual environment.

Furthermore, you will notice that when you activate a virtual environment, your terminal path changes to signify the activated virtual environment.

name@machine project % source .venv/bin/activate
(.venv) name@machine project % 
Enter fullscreen mode Exit fullscreen mode

How to deactivate a virtual environment

If you want to deactivate your virtual environment, simply type the following command:

deactivate
Enter fullscreen mode Exit fullscreen mode

If you also want to reactivate an existing virtual environment, just follow the same instructions about activating a virtual environment.

Also, if your virtual environment is in a git repository, it is always recommended to add it to your gitignore file.

This is to ensure that you do not check in the entire virtual environment folder into git.

Conclusion

Virtual environments allow for full control over dependencies used in each project, and ensure that each project’s environment can be easily replicated.

I hope that by now, you are comfortable with setting up a virtual environment. If you found this guide useful, do share with others. Cheers!

Top comments (0)