Maker.io main logo

How To Create and Use Python Virtual Environments and Manage Python Packages

2023-09-27 | By Maker.io Staff

Python virtual environments allow you to create self-contained units that help manage dependencies and isolate Python projects from one another. These virtual environments have the potential to benefit Python developers in various ways, and this guide explains why you should look into using virtual environments and how to create and activate them. We’ll also explore how to manage Python packages.

How To Create and Use Python Virtual Environments and Manage Python Packages

What are the Benefits of Python Virtual Environments

Virtual environments allow you to create independent units such that the Python interpreter, libraries, and scripts in one environment are entirely isolated from those in other virtual environments. By creating a virtual environment, you can keep your Python projects self-contained, thus preventing conflicts between different packages and versions.

Aside from dependency management, versioning, and isolation, Python virtual environments also make sharing projects easier — by utilizing virtual environments, you can easily share and reproduce them on other machines, enabling seamless collaboration while streamlining the development, testing, and deployment processes.

Virtual environments always sit atop the base Python installation, and you may choose to make a global set of libraries available to all virtual environments. However, optionally, virtual environments may also be completely isolated.

Create and Activate Python Virtual Environments

Luckily, creating a new virtual environment is as simple as typing a single command into a terminal window:

Copy Code
python -m venv /some_folder/virtual_environment_folder/venv_name

The above command creates a new virtual environment in the specified folder. The -m option selects the venv module that comes pre-installed with Python. Alternatively, you can omit the path and only supply a name for the new virtual environment. Python then creates the new venv in the current folder. Either way, running the above command creates a folder for the new environment and also places a few files and sub-folders in it:

How To Create and Use Python Virtual Environments and Manage Python Packages This screenshot illustrates the steps for creating a Python virtual environment.

Opening the pyvenv.cfg configuration file reveals the default settings venv used when creating the environment. By default, it contains the path of the base Python installation, the Python version, and a flag that describes whether the environment should be fully isolated or use system-wide packages:

How To Create and Use Python Virtual Environments and Manage Python Packages This screenshot shows the default contents of the pyvenv configuration file located within the newly generated virtual environment.

Virtual environments don’t strictly need to be activated for usage. Instead, navigating to the venv folder and running the Python interpreter or pip to install packages (or using the environment’s full path) will suffice.

It’s possible, however, to activate a venv by running the activate script located in the environment’s bin folder. Note that doing so modifies the operating system’s registry key to point to the activated virtual environment’s Python installation, instead of the global one. Therefore, running Python in a command line will cause the OS to look for Python in the venv folder.

Similarly, virtual environments can be deactivated by running the deactivate script located in an environment’s bin folder. Doing so will point the operating system’s registry key back to the original system-wide Python installation.

Managing Python Packages in a Virtual Environment

You can use the same tools to manage packages in a global Python installation as well as every single virtual environment. To install a package in a virtual environment, navigate to that environment (or activate it) and then run the pip command:

Copy Code
pip install package_name

Pip can also be used to upgrade already installed packages to newer versions by running:

Copy Code
pip install --upgrade package_name

Finally, use pip uninstall to remove an existing package:

pip uninstall package_name

Whenever you install, upgrade, or uninstall packages, make sure to double-check whether you are modifying the intended virtual environment or executing the command using the global Python installation to prevent unintended side effects.

Sharing Dependencies with Developers

You can use pip to create a list of the installed packages and their versions in either a virtual environment or the global Python installation. Similarly, you can also restore another developer's dependency list locally or share dependencies between different projects, which can help with troubleshooting and building consistent development environments across different platforms and projects.

Use the following command to create the dependency list for sharing with other developers:

Copy Code
pip freeze > requirements.txt

Doing so creates a file called requirements.txt, which you can send to another developer or share between projects. Then, use the following command to restore the dependencies saved to the requirements.txt file:

Copy Code
pip install -r requirements.txt

Summary

Python virtual environments provide a powerful means of managing dependencies and creating isolated project environments. By leveraging virtual environments, you can ensure consistent and reproducible environments, collaborate effectively with team members, and streamline development processes.

Use the venv command that comes with Python3 to create virtual environments. Doing so creates a new folder for the environment and points it to the system-wide Python installation. Next, activate the environment by running the activate script located in the venv’s bin folder. This will make the OS use the virtual environment’s Python interpreter and libraries instead of the global installation.

You can install and manage packages in an environment by running pip after activating an environment. Similarly, upgrading and uninstalling packages using pip only affects the currently active virtual environment. Ppip can generate a dependency file that you can share with other developers to ensure a consistent development environment across multiple computers.

When done, don’t forget to deactivate the virtual environment by running the deactivate script in the venv’s bin folder.

TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum