Virtual Environments in Python
How does the tool venv work?
The venv
module is the new default way of creating basic virtual environments for new Python versions > 3.3. If you dive into virtual environments, you’ll quickly realize that there are a multitude of tools out there such as “virtualenv
“, “pyenv
“, and many more.
My recommendation for data scientists and beginners is the tool conda
that comes with the Anaconda Python distribution. I’ve written an article about the concepts of virtual environments in Python, including a tutorial on how to use conda for your own projects:
The “venv
” tool is the de-facto standard that is already preinstalled with your Python 3.3+ installation. You should learn this tool first—probably you can write Python code for many years before you are forced to touch another virtual environment tool.
Let’s start slowly: Python is a program such as everything else running on your computer. Programs are compiled into machine-readable binary code that is stored in a file. Hence, Python is nothing but a compiled binary file that you can execute on your computer just like Tetris or Minesweeper. If you run the command “python
” in your shell, the binary is executed by your operating system.
Note: you may have to explicitly specify the location (path) of the Python binary file in your operating systems “environment variables” so that your computer can find the program “python
“.
Test whether your Python installation works correctly by opening a shell and typing “python
“.
The default way of working on your code project is as follows:
- write code until you need some library,
- find the library via Google search,
- import it using the “
import
” statement, and - if Python tells you the library is not installed yet, install the library using the pip tool “
pip install library
” etc.
The problem is that all your projects share the same globally installed libraries. But some of them may require different versions or incompatible libraries. Also, you don’t want to clutter your Python installation with hundreds of external libraries.
This is where virtual environments come into play. A virtual environment serves as a “sandbox” for your Python program. You can install any external library or version there without having any global impact. The virtual environments are isolated, independent, and separate.
π Recommended Tutorial: Setup a Virtual Environment with Visual Studio Code in Python
Crash Course venv
So, how to create a virtual environment using the venv tool?
How to Create Virtual Environments with Python “venv”?
The simple answer lies in the following code snippet:
python -m venv ve
The placeholder “ve
” is simply the path to the virtual environment you want to create. In practice, it’ll be the path to the folder of your Python project that should be executed under the virtual environment.
The code snippet does multiple things: it creates a folder that contains a copy of the Python program itself. This means that any package you install within the virtual environment is not visible to your global Python installation.
Activate Your Virtual Environment
Now, the only thing left is to activate your virtual environment using the command (Bash):
source ve/bin/activate
Or the command (Win):
ve\Scripts\activate.bat
Now, you can simply execute “python
” in your shell, and all programs you execute there will be executed within the Python virtual environment.
How to Install Libraries in Your Virtual Environment?
That’s easy, simply use the pip tool to install packages after you have activated the virtual environment.
pip install package
It will automatically detect that you are currently in a virtual environment (as you have activated the environment).
How to Deactivate Your Virtual Environment
You can simply deactivate the virtual environment by typing the command:
deactivate
Next, I’ll show you the best virtual environment cheat sheets on the web! π
Most Comprehensive Virtualenv Cheat Sheet
The most simple and straightforward virtualenv cheat sheet was created by Aaron Lelevier. This is the screenshot from this site:
Virtualenv Cheat Sheet from Michael Noll
The following cheat sheet is pretty concise too—but doesn’t contain the same amount of information as the previous one.
Here’s a screenshot from this site:
Cheatography Virtual Environment Cheat Sheet
A nice cheat sheet is provided on the helpful cheatography website here. You can find a screenshot with the most relevant information next:
Quick and Easy Virtualenv Cheat Sheet
This cheat sheet almost doesn’t deserve the name–but it’s so concise that I just couldn’t resist including it here:
Dan’s Cheat Sheet
Finally, let’s end this cheat sheet collection with another useful one from Dan Poirier (source):
Summary
Virtual environments help you to isolate the dependencies of your Python projects. Simply create your virtual environment in your project location by using the command “python -m venv your_ve_path”. After activation, you can install new packages using pip. All new packages will be installed only in your virtual environment without global visibility.
If you love cheat sheets, feel free to check out my 100% free Python email course with 11+ Python cheat sheets for learning and relearning the most important Python concepts:
Hi,
thank you for this useful guide.
I have just one question, how can I “transmit” or “deliver” a virtual environment for testing purpose or other to a colleague?
or, is there another tool for doing this ?
thanks in advance for your help
Regards
Erix
Hey Erix,
thanks! This is a very good question.
For conda, it’s very easy: check out their guide here:
https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html
“With conda, you can create, export, list, remove, and update environments that have different versions of Python and/or packages installed in them. Switching or moving between environments is called activating the environment. You can also share an environment file.”
For the “venv” tool, it may work out to copy&paste the folder and use this guide to setup the used interpreter to the one in the virtual environment folder:
https://www.techcoil.com/blog/how-to-associate-a-virtualenv-environment-with-a-python-project-in-pycharm/
But I haven’t tried this out — and it certainly isn’t the cleanest solution because there may be some hard-coded paths in your virtual environment.
I would write a small installation script that recreates the virtual environment for your friend or colleague. It’s simple, lightweight, and very flexible.
The brute-force approach would be to use a Docker image, etc.
Hope this helps.
Chris