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 which you can execute on your computer just like Tetris or Minesweeper (I’m not really a gamer though). If you run the command “python” in your shell, the binary is executed by your operating system.
As a side 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 and import it using the “import” statement,
- if Python throws an error, 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.
How to create virtual environments with Python’s “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.
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).
You can simply deactivate the virtual environment by typing the command:
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.
Was this tutorial helpful for you? Comment below!
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.