What Are Python Virtual Environments (venv)?

What Are Python Virtual Environments (venv)? A Simple Explanation!

Did you ever start writing a Python script and then suddenly needed to use some external libraries?

A quick solution is to just run pip install xyz. But if you’re not careful, this can quickly make a mess of your system. Why? Because by default, those libraries are installed globally on your machine.

A cleaner way to handle this is by using a virtual environment (or “venv” for short). So what is that, and how do you actually use it?

The Problem

At first, installing Python libraries feels simple.

You just run pip install xyz and move on.

But over time, things start to get messy.

Maybe one project needs a specific version of a library. Another project needs a different version. At some point, you upgrade a package for one script and suddenly another script stops working.

Now you’re stuck trying to figure out what changed, why it broke, and which version you were using before.

What Is a Virtual Environment?

So, a virtual environment is basically an isolated space for your Python project.

Instead of installing libraries globally on your system, you install them in a virtual environment. And each project you create gets its own environment.

That’s really all it is. A simple way to keep your projects separate from each other.

Why Does This Matter?

Let’s say you’re working on project A that needs the requests library with version 2.18.1. Everything seems to work fine.

Then you start working on project B, which also requires the requests library. But now project B needs a newer version, like 2.34.1.

What do you do now?

If you upgrade requests, project A might stop working. If you keep the older version, project B won’t run properly.

One option would be to update project A so it works with the newer version. But if you’re working on multiple scripts, this quickly turns into constant maintenance.

A cleaner solution is to create a separate virtual environment for each project. That way, each project can use exactly the versions it needs without affecting anything else.

How to Create a Virtual Environment

Creating a virtual environment is straightforward. Python already comes with everything you need.

First, open your terminal and navigate to your project folder. If you don’t have one yet, you can just create a new directory and move into it.

Then run:

python -m venv venv

Let’s quickly break down this command:

  • python runs a new Python command
  • -m venv tells Python to run the built-in module that manages virtual environments
  • venv is the name of the folder where the environment is created

So, in simple terms you’re telling Python to “Create a virtual environment in a folder called venv“. This venv folder contains a separate Python setup, including its own place to install libraries.

But before you can use it, you need to activate the environment.

On macOS or Linux, run:

source venv/bin/activate

If you’re on Windows, I’d suggest to buy a MacBook first. If that’s not possible, run:

venv\Scripts\activate

Once activated, your terminal will usually show (venv) at the beginning of the line. That’s how you know you’re now working inside the virtual environment.

From this point on, anything you install with pip will stay inside this environment and won’t affect your global Python setup.

How to Install Libraries

Once your virtual environment is activated, installing libraries works the same way as usual.

You just run pip install followed by the name of the package.

For example:

pip install requests

That’s it.

The only difference is that the library is now installed inside your virtual environment instead of globally on your system.

You can install any library you need this way. For example, if you’re working with network automation, you might install something like netmiko or nornir.

If you want to see what’s currently installed in your environment, you can run:

pip list

This will show you all libraries that belong to this specific virtual environment.

At this point, your project is completely isolated. Any libraries you install here won’t affect other projects, and other projects won’t affect this one.

Virtual Environments Are Disposable

One important thing to understand is this: your virtual environment is not precious.

If something breaks, you don’t need to fix it. You can just delete the venv folder and create a new one.

That’s actually the idea.

Your code is what matters. The environment is just a temporary setup around it. If it gets messy or stops working, it’s often faster to start fresh than to debug it.

How to Uninstall a Library

If you want to remove a library from your virtual environment, you can use:

pip uninstall requests

Replace requests with whatever package you want to remove.

This only affects your current virtual environment. It won’t touch anything outside of it.

Version Pinning

Sometimes you don’t just want a library. You want a very specific version of it.

To install a specific library version, you can use this command:

pip install requests==2.18.1

This is called version pinning.

It’s useful when your code depends on a specific version and you don’t want it to break because of an update.

Working With requirements.txt

When you’re working on a project, you usually want to keep track of which libraries (and versions) you’re using.

That’s where a requirements.txt file comes in.

You can create one with:

pip freeze > requirements.txt

This will generate a file that lists all installed libraries and their exact versions.

Later, you (or someone else) can recreate the same environment with:

pip install -r requirements.txt

This makes your project reproducible. Instead of guessing which libraries are needed, everything is clearly defined in one file.

0 0 votes
Content Rating
Subscribe
Notify of
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments