This article will teach you how to create a Python virtual environment. It is useful for Python developers to avoid issues with different versions of libraries.
A simple example: You have two applications that rely on the same library, but each requires a different version.
Another example: You want to ensure that your application runs independently of library updates installed in the global Python storage.
A third example: You do not have access to this global storage.
The solution in all three cases is to create a Python virtual environment.
The module name venv
is short for Virtual Environment. It is a great tool for project isolation, functioning like a sandbox. It allows you to run an application with its dependencies without interfering with other applications that use different versions of the same software. As a result, each application runs in its own virtual environment, isolated from others, increasing the overall stability of all applications.
Good news: You don’t need to install venv separately on Windows—it is part of the standard Python 3 library and comes with the interpreter.
On Linux, however, venv is not always included in the OS package, so you might need to install it. On Ubuntu/Debian, use the following command:
sudo apt install -y python3-venv
Some Python packages require compilation from source code, so you might also need to install the following dependencies:
sudo apt install -y build-essential libssl-dev libffi-dev python3-dev
Now, let's see how to create a Python 3 virtual environment in Windows and Linux using venv
.
Use the following command for all operating systems:
python -m venv venv
Here, -m
tells Python to run the venv
module. The second venv
specifies the directory (venv/lib/python3.8/site-packages/
, the version may vary) where Python will store all libraries and components required for isolated application execution.
Activation differs between Windows and Linux.
On Windows, run:
venv\Scripts\activate.bat
On Linux (and MacOS), use:
source venv/bin/activate
If everything is set up correctly, you will see an output like this:
(venv) root@purplegate:/var/test#
Now you can start working on your project within the isolated environment!
Of course, venv
is the most modern tool for creating virtual environments. However, it was only introduced in Python 3. So what should those working with older versions of Python do? The answer: try other tools that offer additional useful features—otherwise, we wouldn’t mention them at all. Below is a brief overview of these alternatives, followed by a more detailed look at the most popular one.
virtualenv
– A simple and user-friendly tool that is widely used when deploying applications. It’s useful to learn, and we’ll provide instructions on how to use it below.pyenv
– Allows you to isolate different Python versions. This is helpful if you need to run multiple versions of Python, for example, for testing purposes.virtualenvwrapper
– A wrapper for virtualenv
that helps manage virtual environments by simplifying tasks like creating, copying, and deleting environments. One of its advantages is that it allows easy switching between environments and supports various plugins for extended functionality.Let's go through the process using Linux as an example. However, running Python virtualenv
on Windows is almost the same, with differences mainly in file paths and scripts, which we’ll mention separately.
You can download the source code and install it manually, but the easiest way is to use pip
. Just enter the following command in your terminal:
pip install virtualenv
This step requires just a simple command:
virtualenv myenv
This will create a new directory in the current folder. Instead of myenv
, you can use any other name for your environment.
Directory structure of the virtual environment:
/myenv/bin
– Contains scripts for managing the environment, a copy of the Python interpreter, pip
, and some package management utilities. In Windows, this folder is located at /myenv/Scripts
./myenv/lib
and /myenv/include
– Store the environment’s core libraries. Any newly installed files will go into /myenv/lib/pythonX.X/site-packages/
, where X.X
represents your Python version.Activation differs slightly between Linux and Windows.
For Linux, use:
source myenv/bin/activate
For Windows, run:
myenv\Scripts\activate.bat
Once activated, you will see the virtual environment’s name in your command line prompt.
If you create the virtual environment with the --system-site-packages
flag, it will have access to the system-wide package storage:
virtualenv --system-site-packages myenv
Keep in mind that the system package paths differ:
/usr/lib/python3.8/site-packages
\Python38\Lib\site-packages
Version numbers may vary depending on your installation.
Once you’re done working in the Python virtual environment, you should exit it properly.
For Linux, run:
deactivate
For Windows, use the batch file:
myenv\Scripts\deactivate.bat
In addition to the venv
and virtualenv
modules we’ve already covered, there are more modern tools that provide greater flexibility in managing Python projects, including virtual environments:
pip
and virtualenv
, along with several other useful tools. It is designed to make environment and package management easier, as many developers eventually encounter version control issues in their projects.Each of these tools deserves a deep dive, but for now, let’s focus on the key features of both.
Poetry handles all aspects of managing libraries within a virtual environment, including installing, updating, and publishing them. The functionality of pip
alone is often insufficient for these tasks.
Additionally, Poetry allows you to create and package applications with a single command (replace myproject
with your actual project name):
poetry new myproject
If you want to initialize an existing directory as a Poetry project, use:
poetry init
Poetry can also:
In simple terms, Pipenv is like pip
+ virtualenv
, but with enhanced features. It eliminates the need for the traditional and sometimes cumbersome requirements.txt
file.
Instead, Pipenv uses:
requirements.txt
. Unlike its predecessor, Pipfile updates automatically as package versions change, which is particularly useful for teams, reducing dependency conflicts.Now you have a complete set of tools at your disposal, and managing multiple dependencies with different versions should no longer be a challenge!