PRACTICAL Introduction to Python Virtual Environment
If you just start your project using python for programming and data analysis, you might come to a common problem for beginners — why your project no longer runs after working on something else for a while. You may also not know where your python packages are stored or how to manage different versions of python between projects. Well, give a sigh of relief, virtual environments have come to the rescue.
In this blogpost, I will delve into the significance of Python virtual environments, providing a step-by-step tutorial on how to create and activate the virtual environment using Python’svenv
module and then install packges. I will also describe how to export the installed packages with their specific versions so as to recreate the same Python virtural environment.
Contents of this documents and quicklinks:
Python is a powerful programming language that offers flexibility and efficiency, and now it has been used in a variety of applications, including web development, machine learning, and scientific computing. One of the most significant features of Python is its ability to install and use packages, which extend the functionality of this language. However, managing packages and their dependencies are quite complex, especially when working on multiple projects that require different versions of the same packages. This is where virtual environments come into play.
A Python virtual environment is a self-contained space where you can run Python code and manage its dependencies, libraries, and configurations separately from the system-wide Python installation. This isolation allows you to work on different projects with different requirements without worrying about conflicts between them. Each virtual environment has its own set of installed packages and can be activated or deactivated as needed.
There are several reasons why you might want to use a virtual environments in your Python projects:
There are two primary tools available for creating a Python virtual environments: virtualenv
and venv
. These tools are essentially interchangeable, but they have slight differences in their compatibility and installation methods.
Virtualenv
supports older versions of Python and must be installed using the pip
command.venv
is integrated into Python 3.3 and above as part of the standard library, eliminating the need for separate installation.Here I will walk through the process of creating a Python virtual environment using venv
.
First, we need to create a project folder to contain your project files and the virtual environment.
Use the venv
module to create a Python virtual environment inside your project folder.
The -m
flag is necessary because we’re running the venv
module as a script. This command should generate a folder venv_4_Proj_A
, which contains the Python virtual environment.
Running the tree -L 2
command inside this folder will show the file structures in the venv_4_Proj_A/
folder.
The most important things in this folder are the site-packages
sub-folder within lib
and the activate
scripts within bin
directories.
site-packages
is the place where all third-party packages you install will be storedactivate
is the script that you should run before using the Python virtual environment.To activate the Python virtual environment, you should run the command:
After activation, the Python virtual environment’s name (here it is venv_4_Proj_A
) will appear below the command, indicating that you are now working inside the Python virtual environment.
We can verify this by checking the version of Python and where it is stored via the command below.
This should display something like this:
Two notes should be addressed here:
Python3
being referenced here is the one within the Python virtual environment. Any packages and libraries installed with the pip
command should be contained within this virtual environment, and inaccessible outside of it.venv_4_Proj_A
to store everything relevant to the virtual environment).Inside the Python virtual environment, you can install packages using pip
, which is the package installer for Python.
Before installing any packages or libraries, let's check the pre-installed packages after we created the Python virtual environment.
Running the command below we can get a list of the pre-installed packages as shown below.
Sometimes you might get the output info like [notice] A new release of pip available.
. If so, you can upgrade pip
to the latest version via running the command.
Running the command below to install three packages (numpy
, pandas
, and matplotlib
) inside the Python virtual environment.
This should display something like this:
If you want to install a specific version of a package, such as the version 2.2.0 for pandas, you should specify the version number of this package in the command line.
You can also install a range of versions using comparison operators.
After the installation of three packaged mentioned above, we will get the updated list of the all installed packages and their specific versions via the command below.
It’s common to reproduce a Python virtual environment to ensure consistency across different development environments or group members.
You can export a list of all installed packages along with their versions to a requirements file using the command below.
This command creates a requirements.txt
file containing all the packages and their exact versions as shown below.
requirements.txt
fileYou can distribute the requirements.txt
file to your group members, and they can later use this file to recreate the same Python virtual environment and install all packages listed in the requirements.txt
file via the commands below.
Once you are done working with your Python virtual environment, you can deactivate it or delete it.
Running the command below returns you to the system-wide Python environment.
To delete the Python virtual environment, simply remove its directory.
Python virtual environments are essential for managing dependencies and preventing conflicts between projects. By creating isolated environments for each project, you can ensure that each application has its specific packages and versions. This enables you to maintain a clear and efficient development environment, allowing you to focus on building great software without worrying about dependency conflicts.
Besides using
venv
module, you can also use other methods (virtualenv, pipenv,conda, etc.) to create the Python virtual environment. You can refer to this WEBPAGE for more details.