Virtual Environments in Python
How does the tool venv work?
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 “
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:
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 “
Test whether your Python installation works correctly by opening a shell and typing “
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.
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):
Or the command (Win):
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:
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):
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:
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.