The Python virtual environment is used to isolate your project’s dependencies and avoid conflicts between projects. If you are a Python developer, DevOps, or a Linux System administrator, you would have worked on Python virtual environment at some point in time. In this blog post, we will walk you through the steps necessary to create a virtual environment for Python development on Ubuntu and Debian Linux systems.

Prerequsities

This article assumes, that you already have Python installed on your Ubuntu, Debian, or Linux Mint systems.

Step 1 – Install venv

First of all, you need to install the Python module for the virtual environment on your system. Python3 users can directly install the package for the env. The Python 2.7 users need to install virtualenv Python module. This will also install other required modules on your system.

For Python3:sudo apt install python3-venv For Python 2.7:sudo pip2 install virtualenv

Step 2 – Create Python Virtual Environment

Once the installation is finished. Let’s create an isolated Python environment for your application. The above commands create a directory named venv in the current directory with a local copy of files. While working on this website, you should activate the local environment in order to make sure you’re working with the right versions of your tools and packages.

Step 3 – Activate Python Virtual Environment

To work with a Python virtual environment, you need to activate the environment. After that, you can install a required module for your Python project as well as run your Python application in an isolated environment. Use the following command to activate the Python environment: Any package that you install using pip is now placed in the virtual environments project folder, isolated from the global Python installation. Use pip3 to install a module. To install the most commonly used ‘requests’ module, type: All the installed modules files are placed at venv/lib/python3.10/site-packages directory.

Step 4 – Deactivate Python Virtual Environment

After finishing your work inside the virtual environment, just type the “deactivate” command to exit from the isolated environment prompt. You will get the default system prompt.

Step 5 – Deleting the Python Virtual Environment

To Delete the Python virtual environment from your application. Simply delete the venv directory from your application folder.

Conclusion

In this tutorial, you have learned to create Python virtual environment on Ubuntu, Debian, and other Debian derivative Linux systems. The Python virtual environment helps us to deploy multiple Python applications on a single server without making conflicts for modules between each other.