When working on a Python project, creating a virtual environment for the project is recommended, such that you isolate the project requirements from the system. Besides, a virtual environment is the best way to separate the dependencies when you have different projects requiring different versions of the same package.
This guide offers a hands-on tutorial about Python’s virtual environment. We will begin by understanding what a Python virtual environment is, its benefits, and how to create, activate, and delete the Python virtual environment.
Understanding Python Virtual Environment
Using virtual environments is common when working with Python development. A Python virtual environment is an isolated development environment that allows developers have an isolated space containing a copy of Python.
In the virtual environment, you can install other tools required for a project. Moreover, you can have multiple virtual environments using different versions of the same program without affecting the global packages.
Ideally, a virtual environment lets you work on multiple side-by-side Python copies with different installed programs for various projects. Each created Python virtual environment contains the Python interpreter and a folder for the installed third-party libraries. Changes made in a virtual environment do not affect the dependencies of other created virtual environments.
Why Use Python Virtual Environments?
Working in a virtual environment ensures an isolated working space that cannot affect system-wide packages. Therefore, you can have room to develop your projects without affecting other packages or dependencies installed in the system.
Moreover, it is possible to have multiple virtual environments having the same installed programs but running on different versions. For instance, if your project requires NumPy 2.1 and another requires NumPy 2.0, you can install the specific version for each virtual environment. Using a virtual environment ensures that we do not get compatibility issues, as Python cannot have two versions of a package running on one system.
When working on a Python project, it is best to create a virtual environment and install all its needed dependencies instead of installing packages globally. That way, you will keep your system clean and avoid global dependency errors.
Working with Python Virtual Environments
We have discussed what a Python virtual environment is and what benefits it offers. The next step is to create and use a Python virtual environment.
Python offers two tools for creating a virtual environment, virtualenv, and venv. The virtualenv works with older Python versions and must be installed on the system before you can use it. However, the venv supports newer Python versions and comes preinstalled with Python 3.3 or higher versions. So, check the Python version you are using to know which tool works for your case.
Creating a Python Virtual Environment
For this tutorial, we are using Ubuntu 22.04, and it’s running Python3. So, we will use venv as a virtual environment tool. We will create a folder where we will make our Python virtual environment. Let us name our project folder linuxhint.\
Before we create the virtual environment, you must have pip installed. Verify so by checking its version.
If pip is not installed, install it as follows.
Also, for Debian/Ubuntu systems, you must install the python3-venv package for you to use it to create your virtual environment. If you try to create the virtual environment without installing the package, you will encounter this error.
So, run the command below to install the pyuthon3-venv package
Press y when prompted to proceed with the installation.
You are now ready to create your virtual environment inside the project folder using the venv tool. Use the command below and name your virtual environment to your liking. In our case, we have named it test1.
You will note that a new folder will be created inside the project folder, confirming that our virtual environment has been successfully created. The created folder contains the necessary tools required for Python development.
Hence, you now have a copy of Python in a virtual environment where you can install the required packages for your project.
Activating the Python Virtual Environment
You cannot use the created virtual environment until you activate it. Here’s the syntax for activating the virtual environment.
Once you activate the virtual environment, you will note that its name will appear at the start of the terminal prompt. Another way of verifying if the virtual environment has been activated is by using the which command to locate Python in the virtual environment.
If the virtual environment is activated, the command will return the path to the Python interpreter created once the virtual environment is activated.
Note that the Python version for your system is the same version available in the created virtual environment. Check the Python version to verify. We used Python 3.10.6 when creating the virtual environment and it is the same version available in the activated virtual environment.
You can now start installing packages for your Python development.
Installing Packages in the Virtual Environment
In a newly created Python virtual environment, only pip and setuptools are available. The two packages are installed by default and you can list the available packages using the pip command.
You can use the pip command when you want more packages. Here is the syntax for installing a package inside the virtual environment.
The above syntax will install the latest version for the specified package. Let us look in to this example to install NumPy with the command below.
Pip will collect the specified package, download it, and install it. Once the installation completes, you can list the installed packages using the pip list command to check if numpy is available and the installed version.
For this case, we installed numpy 1.23.5. What if we wanted a lower numpy version than 1.2? In that case, you can specify the specific version using the syntax below.
Here, we are specifying the exact version using the double equal sign.
Alternatively, using the less than sign with the syntax below, you can specify pip to install any lower version.
Let us go ahead and see how to achieve that, but first, we must install the installed numpy version. Use the command below to uninstall it.
Press y to complete the uninstallation. We can verify that numpy no longer appears in the installed packages list.
Once it is uninstalled, let us try installing a lower numpy version.
In the image above, you will note that pip is installing NumPy-1.19.5, which is a lower version than 1.20. This feature is helpful when building a Python project that relies on older versions of a given package. In that case, you can comfortably handle the development by installing any version of a required package.
Suppose you wanted a higher version. In that case, use the greater than sign, as illustrated in the syntax below.
For instance, the example below specifies that we want numpy installed but of a higher version than 1.20. We can note in the last line that pip installed numpy version 1.23.5, which is what we wanted.
When working with a Python virtual environment, it is best to specify the exact package version that you want to use for the given project. The good news is that you can have two projects using the same package but with a different version, depending on each project’s requirement.
For instance, if we wanted to install pandas version 1.3.5, we could use the command below.
In the above example, we notice that installing pandas also automatically installed its dependencies. Therefore, if we list the available packages, we will notice we installed the specified package and its version and other dependencies.
Reproducing the Virtual Environment
When working with a Python virtual environment, it is common to reproduce it when someone wants to work on the same project using the same packages and versions you installed. It could be you want to share your project with someone, and they must use the same packages you’ve used for the project to run. In that case, first, use the freeze command to list all the project’s packages and the installed versions.
Our target is to create identical environments with the same dependencies. Hence, we should export the above output into a text file, such that we can share it with someone to create an identical environment.
Here is how to export the dependencies.
We can verify the contents of the created text file to ensure we have the same dependencies before sharing it.
Now, let us reproduce the identical environment in the same project folder and activate it.
To reproduce the environment, install the dependencies using the text file we generated earlier for the other virtual environment. Here is how to install the dependencies.
Pip will install all the named packages and their versions.
The last step is to verify that we have an identical virtual environment containing all the required dependencies. Verify that using the pip list command.
We can verify we now have an identical virtual environment. Note that the shared file lets the other person create an identical virtual environment containing the packages, Python interpreter, etc. However, the source code for the project is not contained in the virtual environment but in the project folder.
So, if you want to share the source code, copy it from the project folder, not the virtual environment folder.
Deactivating the Virtual Environment
There are two options for deactivating a Python virtual environment. You can deactivate it to access it later or delete the virtual environment without affecting your system.
Suppose you want to deactivate the virtual environment and switch to use another. Use the deactivate command.
Once you deactivate the virtual environment, the terminal prompt will return to the project folder. If you prefer to delete the virtual environment, you must delete its folder using the rm command.
In the example below, we’ve deleted the demo2 virtual environment.
That’s how to delete a Python virtual environment completely.
Conclusion
Virtual environments come in handy when you want to work on different projects requiring different versions of the same package. All that is needed is to create a virtual environment for each project and install its dependencies without affecting other virtual environments or global packages. This guide has discussed what a Python virtual environment is, why you need it, and how to create it, activate it, and delete the virtual environment. With that, you can implement the same for your Python projects.