Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to create a virtual environment in Python

Introduction

Python is a versatile and powerful programming language, widely used for web development, data analysis, artificial intelligence, and more. As you progress in your programming journey, you might find yourself working on multiple projects, each with its unique set of dependencies and packages.

To avoid conflicts between dependencies or package versions, it's a good practice to create isolated environments for each project. This is where virtual environments come in. In this blog post, we will learn how to create, activate, and deactivate virtual environments in Python, step by step. We will also cover how to install packages within these environments and why virtual environments are essential for your programming projects.

What is a virtual environment?

A virtual environment in Python is an isolated space where you can install packages and dependencies specific to a particular project without affecting other projects or the global Python installation. By doing so, you can avoid conflicts between different package versions required by different projects.

Imagine you have two projects, A and B. Project A requires version 1.0 of a package, while project B requires version 2.0. If you install both versions globally, they might conflict with each other and cause unexpected behavior or errors. But by creating separate virtual environments for each project, you can install the required package versions in isolation, preventing conflicts.

How to create a virtual environment

Python provides a built-in module called venv to create virtual environments. It comes pre-installed with Python 3.3 and above. If you have an older version of Python, you can use the virtualenv package instead. In this tutorial, we will focus on using venv.

To create a virtual environment, follow these steps:

  1. Open your terminal or command prompt.
  2. Navigate to the directory where you want to create the virtual environment. This is usually the root directory of your project.
  3. Run the following command to create a virtual environment:
python3 -m venv myenv

Replace myenv with the desired name for your virtual environment. This will create a new directory with the same name as the virtual environment, containing the necessary files and directories for the environment.

Note: If you are using Windows, you might need to use python instead of python3 in the command.

Activating the virtual environment

After creating the virtual environment, you need to activate it to use it. Activation sets up your shell to use the Python interpreter and installed packages within the virtual environment.

To activate the virtual environment, follow these steps:

On macOS and Linux

  1. Open your terminal.
  2. Navigate to the directory containing the virtual environment.
  3. Run the following command:
source myenv/bin/activate

Replace myenv with the name of your virtual environment.

On Windows

  1. Open your command prompt.
  2. Navigate to the directory containing the virtual environment.
  3. Run the following command:
myenv\Scripts\activate

Replace myenv with the name of your virtual environment.

After activation, you should see the name of the virtual environment in parentheses before your command prompt, indicating that the environment is active.

Installing packages

Once the virtual environment is active, you can install packages and dependencies specific to your project. Use the pip command to install packages, just like you would in a non-virtual environment.

For example, to install the popular web framework Flask, run:

pip install Flask

This will install Flask and its dependencies within the virtual environment.

Note: If you want to know which packages are installed in the virtual environment, use the following command:

pip list

Deactivating the virtual environment

When you're done working in the virtual environment, you can deactivate it to return to the global Python environment. To do this, simply run:

deactivate

The virtual environment name should disappear from your command prompt, indicating that it has been deactivated.

Managing dependencies with a requirements file

In a collaborative project, it's essential to share the list of packages and their versions required for the project with your team. You can do this using a requirements file.

To create a requirements file, follow these steps:

  1. Activate the virtual environment if it's not active.
  2. Run the following command:
pip freeze > requirements.txt

This will create a file called requirements.txt containing the list of installed packages and their versions.

To install the dependencies from a requirements file, follow these steps:

  1. Create and activate a new virtual environment.
  2. Run the following command:
pip install -r requirements.txt

This will install the packages listed in the requirements.txt file within the virtual environment.

Conclusion

Virtual environments are an essential tool for managing dependencies and packages in Python projects. They allow you to create isolated environments for each project, preventing conflicts between package versions.

In this tutorial, we learned how to create, activate, and deactivate virtual environments using the venv module. We also covered how to install packages within virtual environments, manage dependencies with a requirements file, and share the list of required packages with your team.

By using virtual environments, you can ensure that your projects remain organized and maintainable, allowing you to focus on writing great code. So, the next time you start a new Python project, don't forget to create a virtual environment for it!