Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to create virtual environment in Python

Introduction to Virtual Environments

When you start learning programming, you might find yourself working on multiple projects at the same time. Each project may have its own set of dependencies or require different versions of the software. This can quickly turn into a chaotic mess, as different projects require different versions of the same package, or worse, they could conflict with each other. This is where virtual environments come in handy.

A virtual environment in Python is an isolated workspace that you can create for each project. It contains its own set of packages and dependencies, separate from your system's global Python installation. This ensures that each project has its own clean environment, free from any conflicting packages or dependencies. It's like having a separate room for each project, where everything is organized and clutter-free.

In this blog post, we will learn how to create and manage virtual environments in Python using two popular tools: venv and virtualenv. We will also cover some best practices and tips to make the most out of your virtual environments.

Getting Started with venv

venv is the built-in virtual environment management tool in Python since version 3.3. It's included with the Python standard library, so you don't need to install any additional packages to use it.

Creating a New Virtual Environment

To create a new virtual environment using venv, simply run the following command in your terminal:

python3 -m venv my_project_env

Here, python3 -m venv invokes the venv module, and my_project_env is the name of the virtual environment you want to create. Feel free to replace it with any name you like.

This command will create a new directory called my_project_env in your current working directory. This directory contains all the necessary files and directories to manage your virtual environment, including a separate Python interpreter and a copy of the pip package manager.

Activating the Virtual Environment

Once you've created your virtual environment, you need to activate it before you can use it. Activation essentially tells your terminal to use the Python interpreter and packages inside the virtual environment, instead of the global Python installation.

To activate your virtual environment, run the following command:

# On macOS and Linux
source my_project_env/bin/activate

# On Windows
my_project_env\Scripts\activate

After activation, you should see the name of your virtual environment in your terminal prompt, like this:

(my_project_env) $

This indicates that your virtual environment is active, and any Python packages you install or commands you run will now use this environment.

Installing Packages in the Virtual Environment

With your virtual environment active, you can now install Python packages using pip. For example, let's say your project requires the popular requests library. You can install it by running:

pip install requests

This command will install the requests package inside your virtual environment, making it available for your project to use.

Deactivating the Virtual Environment

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

deactivate

Your terminal prompt should go back to its original state, indicating that your virtual environment is no longer active.

Managing Virtual Environments with virtualenv

virtualenv is a third-party package that provides similar functionality to venv. It's a popular choice for managing virtual environments, especially for older versions of Python that do not include the venv module.

Installing virtualenv

Before you can use virtualenv, you need to install it. You can do this using pip:

pip install virtualenv

This command will install virtualenv globally on your system, making it available to use for all your projects.

Creating a New Virtual Environment

To create a new virtual environment using virtualenv, run the following command:

virtualenv my_project_env

This command will create a new directory called my_project_env, just like venv. The directory structure and contents are also similar, containing a separate Python interpreter and a copy of pip.

Activating and Deactivating the Virtual Environment

The process of activating and deactivating a virtual environment with virtualenv is the same as with venv. Use the following commands to activate and deactivate your virtual environment:

# On macOS and Linux
source my_project_env/bin/activate

# On Windows
my_project_env\Scripts\activate

To deactivate the virtual environment:

deactivate

Installing Packages in the Virtual Environment

With your virtual environment active, you can install Python packages using pip, just like with venv. For example, to install the requests library, run:

pip install requests

Best Practices and Tips

Now that you know how to create and manage virtual environments in Python, here are some best practices and tips to help you make the most out of your virtual environments:

Use one virtual environment per project: This will ensure that each project has its own isolated environment, free from any conflicting packages or dependencies.

Add your virtual environment directory to your project's .gitignore file: This will prevent your virtual environment files from being accidentally committed to your version control system. This is important because virtual environments can be quite large, and they are specific to your system and setup.

Use a consistent naming convention for your virtual environments: This will help you easily identify and manage your virtual environments. Some popular naming conventions include using the project name followed by _env (e.g., my_project_env) or using a .venv directory inside the project directory (e.g., my_project/.venv).

Keep your virtual environments up to date: Make sure to regularly update the packages in your virtual environments to ensure you're using the latest and most secure versions. You can use the following command to update all the packages in your virtual environment:

pip install --upgrade package-name
  1. Use a requirements file to manage your project dependencies: A requirements file is a simple text file that lists all the packages and their specific versions that your project depends on. This makes it easy to reproduce your virtual environment on other systems or share it with your teammates. You can create a requirements file by running:
pip freeze > requirements.txt

And to install the packages listed in the requirements file, run:

pip install -r requirements.txt

Conclusion

In this blog post, we have learned how to create and manage virtual environments in Python using the built-in venv module and the third-party virtualenv package. We have also covered some best practices and tips to make the most out of your virtual environments.

By using virtual environments, you can ensure that each of your projects has its own isolated environment, free from any conflicting packages or dependencies. This will make your development process smoother, more organized, and less prone to errors.