Python virtual environments

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

In today's article, we'll be looking at Python virtual environments. What they are, why we need them and how to create them.
In basic, a virtual environment is an isolated environment for Python projects. Basically a container for your specific project. For those familiar with node modules, I find it quite similar to that.
You can have modules globally installed, and every project can access those, or you can have them project-based installed so only that project can find them.
It's because of Python's way of downloading packages that we want to differentiate between projects. In general, I find it a good choice to have project-based environments anyway since it will narrow down your error possibility.
Python can have a hard time differentiating between versions of a package to narrow it down, so let's say we want packageA but projectA needs v1.0.0 and projectB needs v2.0.0`.
As it would be installed globally, there is no way to differentiate between those two.
If we installed them in our virtual environment, each project would use its own specified version.
For my article, I'll be using venv. However, multiple options can create virtual environments for you.
Open up your project in a terminal and run the following command.
python -m venv .venv
The last argument, .venv, is the virtual environment's location and can be anything you want.
In this case, a folder called .venv is created.

We can activate this virtual environment by running the following command.
source .venv/bin/active
You will see it's active if the terminal places the environment name in front of your arrow like this.

Now, if we run pip install, it will install specific packages inside our virtual environment.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter