There are two very simple ways to create a Python virtual environment on Debian 10. They’re very similar and offer nearly the same benefits. As an added bonus, you won’t need to install anything outside of the default Debian repositories to use them.
In this tutorial you will learn:
- How to Install the Dependencies
- How to Use Python 3’s Venv
- How to Use Virtualenv
Software Requirements and Conventions Used
Category | Requirements, Conventions or Software Version Used |
---|---|
System | Debian 10 Buster |
Software | Python 3 |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions |
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux commands to be executed as a regular non-privileged user |
Install the Dependencies
Before you get started, make sure that you have Python 3.
$ sudo apt install python3 python3-venv
Then, if you plan to use Virtualenv, install that too.
$ sudo apt install virtualenv python3-virtualenv
Use Python 3’s Venv
Python 3’s venv
functionality is built-in, and you can use it to get set up without anything else.
$ python3 -m venv /path/to/virtual/environment
It will only take a few seconds to get set up. Once it’s done, you can activate the virtual environment with:
$ source your-broject/bin/activate
Now, you’re working with the Python install from your virtual environment, instead of the system wide one. Anything you do now, should reside in your project folder. When you’re done, just run deactivate
to exit the virtual Python.
Use Virtualenv
To start, create your environment with the virtualenv
command. You’ll also need to tell it to use Python 3 with the -p
flag.
$ virtualenv -p python3 /path/to/virtual/environment
This will take a few seconds to get itself setup with Pip and the other Python packages it includes. When it’s finished, activate the environment.
$ source your-project/bin/activate
Do your work inside the project directories. When you’re done, use deactivate
to exit the virtual environment.
Conclusion
It’s super easy to get set up with Python virtual environments, and the benefits are pretty clear. You’ll be able to compartmentalize your projects, and keep things from conflicting. It’s also easier to manage Python package versions as you work.