/

Python Virtual Environments: Managing Dependencies in Multiple Python Applications

Python Virtual Environments: Managing Dependencies in Multiple Python Applications

If you have multiple Python applications running on your system, it’s likely that you will come across a situation where different applications require different versions of the same module. To effectively manage these dependencies, you can make use of virtual environments.

In this blog post, we will focus on creating virtual environments using the venv module, although other tools such as pipenv function in a similar way.

To create a virtual environment, navigate to the desired project folder (or an existing project folder) and run the following command:

1
python -m venv .venv

This creates a virtual environment named .venv within the current directory.

After creating the virtual environment, you need to activate it. In most shells, you can accomplish this by running:

1
source .venv/bin/activate

If you are using the Fish shell, use this command instead:

1
source .venv/bin/activate.fish

Once the virtual environment is activated, your terminal prompt will reflect this change. For example, the prompt might change from ➜ folder to (.venv) ➜ folder.

By using the virtual environment, any subsequent pip commands will be isolated within this environment instead of the global environment, allowing you to manage package versions specific to the project.

Tags: Python, virtual environment, venv, pip, dependencies