Python venv: How To Create, Activate, Deactivate, And Delete

Python virtual environments allow you to install Python packages in an isolated location from the rest of your system instead of installing them system-wide. Let’s look at how to use the Python venv, short for Python virtual environment, also abbreviated as virtualenv.

In this article, you will learn:

  • The advantages of using virtual environments
  • How to create a venv
  • How to activate and deactivate it
  • Different ways to delete or remove a venv
  • How a venv works internally

Why you need virtual environments

There are multiple reasons why virtual environments are a good idea. Let’s go over them one by one.

Preventing version conflicts

If you install third-party packages system-wide, you could argue that you’re very efficient. After all, you only need to install it once, saving you precious time and disk space. There’s a problem with this approach that may start to unfold weeks or months later, however.

Suppose your project, Project A, is written against a specific version of library X. In the future, you might need to upgrade library X. Say, for example, you need the latest version for another project you started, called Project B. You upgrade library X to the latest version, and project B starts working fine. Great! But once you did this, it turns out your Project A code broke badly. After all, APIs can change significantly on major version upgrades.

A virtual environment fixes this problem by isolating your project from other projects and system-wide packages.

Easy to reproduce and install

Virtual environments make it easy to define and install the packages specific to your project. Using a requirements.txt file, you can define exact version numbers for the required packages to make sure your project will always work with a version that has been tested with your code. This also helps other users of your software since a virtual environment will help others reproduce the exact environment for which your software was built.

Works everywhere, even when not root

If you’re working on a shared host, like those at a university or a web hosting provider, you won’t be able to install system-wide packages, since you don’t have the administrator rights to do so. In these places, a virtual environment allows you to install anything you want locally in your project.

Virtual environments vs. other options

There are other options to isolate your project:

  1. In the most extreme case, you could buy a second PC and run your code there. Problem fixed! It’s a bit expensive, though!
  2. A virtual machine is a much cheaper option but still requires you to install a complete operating system. A bit of a waste as well for most use-cases.
  3. Next in line is containerization, with the likes of Docker and Kubernetes. These can be very powerful, and are actually a good alternative.

Still, there are cases when we’re just creating small projects or one-off scripts. Or perhaps you just don’t want to containerize your application. Whatever the reason is, virtual environments are a great way to isolate your project’s dependencies.

How to create a Python venv

There are several ways to create a Python virtual environment, depending on the Python version you are running.

Before you read on, I want to point you to another tool, called Pipenv. It combines the functionality of tools that you are about to learn; virtualenv and pip. To get a good understanding of virtual environments, I recommend you to learn the basics first though.

Python 3.4 and above

If you are running Python 3.4+, you can use the venv module baked into Python:

$ python -m venv [directory]

This command will create a venv in the specified directory and copy pip and easy_install into it too.

All other Python versions

The alternative that works for any Python version is using the virtualenv package. You may need to install it first with pip install:

$ pip install virtualenv

Once installed, you can create a virtual environment with:

$ virtualenv [directory]

Python venv activation

How you activate your virtual environment depends on the OS you’re using.

Windows venv activation

To activate your venv on Windows, you need to run a script that gets installed by venv. If you created your venv in a directory called myenv, the command would be:

myenv\Scripts\activate.bat

Linux and MacOS venv activation

On Linux and MacOS, we activate our virtual environment with the source command. If you created your venv in the myvenv directory, the command would be:

$ source myvenv/bin/activate

That’s it! We’re ready to rock! You can now install packages with pip, but I advise you to keep reading to understand the venv better first.

How a Python venv works

When you activate a virtual environment, your PATH variable is changed. On Linux and MacOS, you can see it for yourself by printing the path with echo $PATH. On Windows, use echo %PATH%. In my case, on Windows, it looks like this:

/Users/erik/myvenv/bin:/usr/local/bin:/usr/bin:/bin:etcetera

As you can see, the bin directory of my venv is put in front of everything else, effectively overriding all the system-wide Python software. This works because when you enter a command that can’t be found in the current working directory, your OS starts looking at all the paths in the PATH variable. If your venv is there first, the OS will look there first before looking at system-wide directories like /usr/bin.

If you take a look inside the directory of your venv (in this case: myvenv), you’ll see something like this:

A Python venv directory tree
Virtualenv directory tree

You can see that:

  • The Python command is made available both as python and python3, and the version is pinned to the version with which you created the venv by creating a symlink to it.
  • All packages you install end up in the site-packages directory.
  • We have activation scripts for multiple shell types (bash, csh, fish)
  • Pip is available under the names pip and pip3

Deactivate the Python venv

Once you finished working on your project, it’s a good habit to deactivate its venv. By deactivating, you basically leave the virtual environment. Without deactivating it, all other Python code you execute, even if it is outside of your project directory, will also run inside of it.

Luckily, deactivating your virtual environment couldn’t be simpler. Just enter this: deactivate. It works the same on all operating systems.

Deleting a Python venv

You can completely remove a virtual environment, but how you do that depends on what you used to create the venv. Let’s look at the most common options.

Delete a venv created with Virtualenv or python -m venv

There’s no special command to delete a virtual environment if you used virtualenv or python -m venv to create your virtual environment, as is demonstrated in this article. When creating the virtualenv, you gave it a directory to create this environment in.

If you want to delete this virtualenv, deactivate it first and then remove the directory with all its content. On Unix-like systems and in Windows Powershell, you would do something like:

$ deactivate
# If your virtual environment is in a directory called 'venv':
$ rm -r venv

Delete a venv with Pipenv

If you used Pipenv, it’s a lot easier. You can use the following command to delete the current venv:

pipenv --rm

Make sure you are inside the project directory. In other words, the directory where the Pipenv and Pipenv.lock files reside. This way, pipenv knows which virtual environment it has to delete.

If this doesn’t work, you can get a little nastier and manually remove the venv. First ask pipenv where the actual virtualenv is located, with the following command:

$ pipenv --env
/home/username/.local/share/virtualenvs/yourproject-IogVUtsM

It will output the path to the virtual environment and all of its files, and it will look similar to the example above. The next step is to rm -rf that entire directory and you’re done.

Delete a venv with Poetry

If you created the virtualenv with Poetry, you can list the available venv’s with the following command:

poetry env list

You’ll get a list like this:

test-O3eWbxRl-py2.7
test-O3eWbxRl-py3.6
test-O3eWbxRl-py3.7 (Activated)

You can remove the environment you want with the poetry env remove command. You need to specify the exact name from the output above, for example:

poetry env remove test-O3eWbxRl-py3.7

Conclusion

You learned how to create, activate, deactivate, and delete virtual environments. We also took a look behind the curtains, to see why and how a venv works. Now that you know how to create a venv, you need to learn how to install packages inside of it. After that, I strongly recommend you to learn about Pipenv or Poetry. These tools combine the management of your virtual environment with proper package and dependency management.

Keep learning

About Erik van Baaren

Erik is the owner of Python Land and the author of many of the articles and tutorials on this website. He's been working as a professional software developer for 25 years, and he holds a Master of Science degree in computer science. His favorite language of choice: Python! Writing good articles takes time and effort. Did you like this tutorial? You can buy him a coffee to show your appreciation.

Subscribe
Notify of
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments