- Python Snacks
- Posts
- Python virtual environments - what you need to know
Python virtual environments - what you need to know
Reduce "dependency hell" by leveraging Python's venv module.

When we’re working on a project, we will most likely need to leverage Python packages found on PyPi or conda forge.
While you can install these packages directly onto your machine, it may only work for that one specific project. Over time, packages update and become incompatible with dependencies from a previous versions.
Take the NumPy 2 release, for instance. As of Jan 9, 2024 it was incompatible with scikit-image. If you were to install the scikit-image package with numpy 2, scikit-image wouldn’t work as you’d anticipate. However, installing it with a version prior to numpy 2 would allow the package to work as anticipated.
Article Contents
What is a virtual environment?
A virtual environment is a tool where you can isolate project dependencies. You can install different versions of packages (and even Python itself) without it affecting other projects.

Virtual environment with installed packages example
In the above example, our 3 projects have different dependencies. Project 1 and 2 share flask as a common dependency, but project 3 doesn’t. Moreover, Project 1 doesn’t require matplotlib, but Project 3 does.
Virtual environments allow us to define these dependencies independently of other.
Creating a virtual environment
There are a few ways to create a virtual environment. The first is by using venv, a Python built-in that allows you to create these with one command. Another is by using Conda.
For the purposes of this, we’ll be sticking with venv, as conda can get quite bulky (unless you use miniconda or mamba). To create a virtual environment using venv, navigate to the root of your project and type the following command:
python venv -m <environment name>
Where <environment name> is whatever you’d like to call the environment.
Activating your virtual environment
Now that the virtual environment is created, we need to be able to use it. It’s operating system dependent:
Windows: <environment name>\Scripts\activate.bat
Mac/Linux: source <environment name>/bin/activate
Where <environment name> is the name of your virtual environment.
Installing packages into a virtual environment
To install packages, you’ll be leveraging pip. Python’s venv module comes bundled together with pip. You can install any package listed in PyPi using pip. To do so:
pip install <package>
Where <package> is the name of the package you want to install. You can get a bit crafty with specific versions:
pip install <package>==<version>
Where <version> is the version you want to install. For instance, if we wanted to install flask 2.0.1:
pip install flask==2.0.1
» Fun tidbit: pip stands for “pip install python”
📧 Join the Python Snacks Newsletter! 🐍
Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:
Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.
Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.
Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.
You can unsubscribe at any time.
Interested in starting a newsletter or a blog?
Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.
This article may contain affiliate links. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you. All purchases go to support the newsletter and blog.
Reply