Key Takeaways
- Virtual environments in Python are crucial for managing project dependencies and preventing conflicts. These isolated environments contain a fresh duplicate of Python binaries and a standalone copy of the entire Python standard library, allowing developers to maintain local machine packages, share dependencies with others, and deploy Python apps in dedicated servers.
- Python offers built-in tools like ‘venv’ and external packages like ‘virtualenv’ and ‘virtualenvwrapper’ to create and manage virtual environments. While ‘venv’ is a built-in module from Python 3.3 onwards, ‘virtualenv’ is a third-party package compatible with Python 2.7 and above and offers more features. ‘Virtualenvwrapper’ simplifies working with multiple virtual environments by organizing them in one place.
- The Python package manager ‘pip’ is integral to the workflow of virtual environments. It allows developers to install and manage Python packages within these environments, generate a ‘requirements.txt’ file listing all project dependencies, and install specific versions of packages. Any installations or updates done within a virtual environment do not affect the system-wide Python installation.
Virtual Environments
A virtual environment helps us to solve project dependency conflicts by creating isolated environments. These “isolated environments” contain all the goodies Python programmers might need to develop their projects. Virtual environments include a fresh duplicate of Python binaries, and a standalone copy of the entire Python standard library. That’s why it can work by itself. Using virtual environments give us the following advantages:- we’re able to maintain our local machine packages intact
- we can share dependencies with others with a
requirements.txt
file - we can deploy a Python app in a dedicated server (PythonAnyWhere, Heroku, and so forth)
The Need for Virtual Environments
I use many libraries for my projects. Among them are three web application development frameworks, and there are other libraries I’d like to explore in future. This serves as the main argument that serious projects in Python depend on other packages written by other developers. If you’re a Django developer, I’m confident you use Django rest framework to create powerful rest APIs, Django Debug Toolbar for gathering various debug information about the current request/response, Celery for taking care of real-time operations, and scheduling as well, and so on. For example, I rely heavily on therequests
package for some of my projects, and a Django web application I’m currently working on depends on version 2.3.0. According to the official documentation, at the time of writing the latest version of this package is version 3.2.
Let’s suppose I go ahead and install the latest version of the library on my Ubuntu machine because I need it for another project. Everything seems to work fine until I try to make use of my older project, which worked fine with 2.3.0. Suddenly, everything is broken.
What happened? Maybe the API of the latest version of Django has changed since version 2.3.0? The reason doesn’t matter at this point, as my older project is broken and no longer works.
A conflict between two projects has been created. They make use of the same library, but they require different versions of it.
Various packages solve this problem. Let’s see some that stand out.
Before Starting
In this tutorial, we’ll be using Python 3, so let’s start by checking your Python installation. To do this, open up a terminal — cmd/PowerShell on Windows — and type the following command:python --version
Python 3.9.5 # My result
Note: Most macOS and Linux systems have Python installed. You can check the Python installation guide if you’re using Windows.
If you didn’t get a result of the form Python 3.x
there are two options:
- if this command returned a
Python 2.x
version, you’ll need to usepython3
along with this tutorial - if you got an
Unknown command
error, try to runpython3
, and if you get another error, follow the Python installation guide
python3
binary by checking its version:
python3 --version
Python 3.9.5
Note: if the command above worked, you’ll need to run python3
instead of python
.
Now that you know which Python command runs on your machine, let’s get into virtual environments.
Built-in venv
Module
Let’s use the built-in Python venv module to create your first virtual environment.
Note: to use this module you need Python 3.3 or greater installed in your system.
To create a Python virtual environment with venv
, type the following command:
python -m venv virt1
Note: the -m
flag means Python is running the built-in venv
module as a script.
This will create a virtual environment with the name of virt1
, but this is just an argument. You can create the virtual environment with any name you want.
Everything installed in the virt1
directory won’t affect the global packages or system-wide installations, thus avoiding dependency conflicts.
Activating Virtual Environments
It’s crucial to know that each time we want to use a created virtual environment, we need to activate it with the following command:source virt1/bin/activate
This won’t work in every system, so you can check the table below to have a clear idea of which command to use:
Platform | Shell | Command to activate virtual environment |
---|---|---|
POSIX | bash/zsh | $ source (venv-name)/bin/activate |
fish | $ source (venv-name)/bin/activate.fish | |
csh/tcsh | $ source (venv-name)/bin/activate.csh | |
PowerShell Core | $ (venv-name)/bin/Activate.ps1 | |
Windows | cmd.exe | C:> (venv-name)\Scripts\activate.bat |
PowerShell | PS C:> (venv-name)\Scripts\Activate.ps1 |
$
sign on POSIX and the C:>
, PS C:>
signs on Windows aren’t part of the command.
As you may notice, I’m using a bash shell in a POSIX (macOS and Linux), which is why I’m running the command above.
After the environment is created
Once the virtual environment gets activated, the terminal prompt changes a bit. The following command lets you deactivate the virtual environment:deactivate
Note how your terminal prompt has changed again.
Now activate your virtual environment again and use the which
command to check the Python binary that’s being used:
source virt1/bin/activate
which python
If everything worked well, you should get something similar to the following output:
/home/daniel/tests/python-tests/venvs/virt1/bin/python
If you deactivate
and which
again, you should get a different output:
deactivate
/usr/bin/python
This is because, when working inside a virtual environment, the binary copy placed inside that environment is being used. The same applies to packages.
Pip with virtual environments
Although this isn’t a pip guide it’s important to show off the workflow between pip and virtual environments. pip — whose name stands for “Pip Installs Packages” — is a package manager used to install and manage Python packages. It’s extremely useful when you want to distribute your project to others, as it allows other developers — and end-users — to install all the dependencies of your project at a glance. For example, a fellow developer can activate a virtual environment and then run the following command to install the dependencies of the project:pip install -r requirements.txt
Here, requirements.txt
is the file that contains all the project dependencies — the specific versions of packages.
To generate the dependencies file of your project, you can run the command below:
pip freeze > requirements.txt
If you want to install a specific version of a package, you can run pip install
followed by the package name, double equal sign (==
), and its version:
pip install package==version
In other situations, we can also uninstall a package from our machine (or virtual environment):
pip uninstall some-package-name
Virtualenv
Virtualenv is an external package used to create virtual environments. In reality, the Python built-invenv
is a subset of it, so virtualenv
has more features than the first option we saw. You can learn more about virtualenv
advantages over venv
in the official documentation.
For now, let’s install virtualenv
with pip (make sure you’ve deactivated the previous venv
) making use of the command below:
pip install virtualenv
This tool works similar to venv
, so let’s test it out by creating another virtual environment:
virtualenv virt2
Note: make sure you deactivate
the other environment before running the above command.
As with venv
, we must activate the virtual environment before using it:
source virt2/bin/activate
If I now install the newest version of requests, it will be installed only on the virtual environment venv2
:
pip install requests
The above command produces the following output:
Collecting requests
...
Installing collected packages: urllib3, idna, chardet, certifi, requests
Successfully installed certifi-2021.5.30 chardet-4.0.0 idna-2.10 requests-2.25.1 urllib3-1.26.5
If I run the pip freeze
command, which prints a list of all my installed packages, I’ll get this:
certifi==2021.5.30
chardet==4.0.0
idna==2.10
requests==2.25.1
urllib3==1.26.5
As you can see, the only packages I get are the latest version of requests
— at the time of writing — and its dependencies.
Other Virtualenv features
We can use the-p
flag while working with virtualenv
to use a specific version of Python that’s globally installed on the machine.
For example, the following command can be used to create the virtual environment virt2
with Python3 in it, if you have Python3 installed on your machine:
virtualenv -p /usr/bin/python3 virt2
And to delete a virtual environment, you use the rm -r
command as you do with any other directory you want to delete:
rm -r virt2
You can learn more about advanced usage of the virtualenv CLI interface in the official documentation.
Virtualenvwrapper
Virtualenvwrapper provides very useful commands that make working with virtual environments even easier, by organizing all of them in a simple place. As with virtualenv, It can be installed easily with pip.pip install virtualenvwrapper
This will create a shell file virtualenvwrapper.sh
located at your ~/.local/bin/
directory. This folder is used to store package binaries which let you to use Python packages directly from your terminal.
Before using virtualenvwrapper, you’ll need to edit your shell configuration file. Since I’m using a bash shell, I’ll append the following content to the .bashrc
file, located in my home directory:
cat <<EOT>> ~/.bashrc
# Virtualenwrapper settings
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python
export VIRTUALENVWRAPPER_VIRTUALENV=~/.local/bin/virtualenv
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Documents
source ~/.local/bin/virtualenvwrapper.sh
EOT
This will append — write at the end of the file — the above content to the .bashrc
file. If the command above didn’t work, open the file and modify it manually.
The VIRTUALENVWRAPPER_PYTHON
is pointing to the Python binary of your machine. You can check it with the following command (without any virtual environment activated):
which python
/usr/bin/python # My result
Make sure to modify the .bashrc
file according to your Python binary path.
Note: if you’re using Windows, you can use virtualenvwrapper-win.
Then, we reload the bash shell with the changes we made to the .bashrc
file by running the following command:
source ~/.bashrc
Now, the mkvirtualenv
command can be used to easily make new environments placed by default inside this folder:
mkvirtualenv sitepoint
You can see the sitepoint
virtual environment folder by entering to the WORKON_HOME
path, which we defined above as $HOME/.virtualenvs
:
ls ~/.virtualenvs
sitepoint # Virtual env folder
To get a list of all the virtual environments created by virtualenvwrapper, you can run the workon
command without arguments:
workon
sitepoint # My result
We can easily activate the virtual environment with the help of the workon
command:
workon sitepoint
The command to deactivate the virtual environment is the same as the one we used before:
deactivate
It’s very easy to switch between different virtual environments. For example, to workon
another virtual environment:
workon another_virtualenv
To delete a virtual environment, the command rmvirtualenv
should be used:
rmvirtualenv sitepoint
Conclusion
In this tutorial, you’ve learned about the essential workflow that every Python developer should master. Virtual environments are a crucial part of any collaborative Python project, and you can instantly improve your productivity by using them. The tools are out there. Now it’s time to incorporate them into challenging personal projects. Do you use any other interesting approaches in your Python development workflow? Let us know in the Python forum!Frequently Asked Questions (FAQs) about Python Virtual Environments
What is the difference between venv and virtualenv in Python?
Both venv and virtualenv are tools in Python that allow you to create isolated Python environments. However, there are some key differences between the two. Venv is a built-in Python module available from Python 3.3 onwards, while virtualenv is a third-party package that can be used with Python 2.7 and above. Venv creates environments that share the Python system site-packages directory, while virtualenv creates completely isolated environments. This means that with venv, you can still access system-wide packages, while with virtualenv, you have to install all packages you need within the environment.
How do I use pip in a virtual environment?
To use pip in a virtual environment, you first need to activate the environment. Once the environment is activated, you can use pip to install packages as you normally would. These packages will only be installed within the virtual environment, and won’t affect your system-wide Python installation. To deactivate the environment and return to your system-wide Python, you can use the ‘deactivate’ command.
Can I create a virtual environment without pip?
Yes, it is possible to create a virtual environment without pip. When you create a virtual environment using venv or virtualenv, it will by default include pip. However, if you want to create an environment without pip, you can use the ‘–without-pip’ option with the virtualenv command.
How do I manage multiple virtual environments?
Managing multiple virtual environments can be done using tools like virtualenvwrapper or pyenv. These tools provide commands for creating, deleting, and switching between different virtual environments. They also allow you to keep all your environments in one place, making them easier to manage.
Can I share a virtual environment with others?
While it’s technically possible to share a virtual environment by copying the environment directory, it’s generally not recommended. This is because the environment includes specific paths and dependencies that may not work on another machine. Instead, it’s better to share a ‘requirements.txt’ file, which lists all the packages used in the project. Others can then use this file to create an identical environment on their own machine.
How do I update packages in a virtual environment?
To update packages in a virtual environment, you can use the ‘pip install –upgrade’ command followed by the package name. This will update the specified package to the latest version. Remember to activate the environment before running this command.
What is the purpose of a .venv folder in a Python project?
The .venv folder in a Python project is where the virtual environment for that project is stored. It contains all the necessary files and directories for the environment, including the Python interpreter, libraries, and scripts.
How do I use a virtual environment with an IDE like PyCharm or VS Code?
Most modern IDEs like PyCharm or VS Code have built-in support for Python virtual environments. You can usually specify the path to the environment in the IDE’s settings or configuration. Once set up, the IDE will use the Python interpreter and packages from the specified environment.
Can I use different versions of Python in different virtual environments?
Yes, one of the main benefits of using virtual environments is the ability to use different versions of Python in different environments. This can be useful if you have projects that require different Python versions.
How do I delete a virtual environment?
Deleting a virtual environment is as simple as deleting the environment’s directory. However, make sure to deactivate the environment first if it’s currently active.
Self-taught Python/Django Developer, Technical Writer, and long life learner. I enjoy creating software from scratch and sharing knowledge with stunning technical articles.