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
Table of Contents
Why you need virtual environments
There are multiple reasons why virtual environments are a good idea, and this is also why I’m telling you about them before we continue to the part where we start installing 3rd party packages. Let’s go over them one by one.
Preventing version conflicts
You could argue that installing third-party packages system-wide is very efficient. After all, you only need to install it once and can use the package from multiple Python projects, 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 works 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. You install packages inside this virtual environment specifically for the project you are working on.
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 ensure your project will always work with a version tested with your code. This also helps other users of your software since a virtual environment helps others reproduce the exact environment for which your software was built.
Works everywhere, even when not administrator (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:
- In the most extreme case, you could buy a second PC and run your code there. Problem fixed! It was a bit expensive, though!
- A virtual machine is a much cheaper option but still requires installing a complete operating system—a bit of a waste as well for most use cases.
- Next in line is containerization, with the likes of Docker and Kubernetes. These can be very powerful and are a good alternative.
Still, there are many cases when we’re just creating small projects or one-off scripts. Or perhaps you just don’t want to containerize your application. It’s another thing you need to learn and understand, after all. 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 two other tools, Python Poetry and Pipenv. Both these tools combine the functionality of tools you are about to learn: virtualenv and pip. On top of that, they add several extras, most notably their ability to do proper dependency resolution.
To better understand virtual environments, I recommend you learn the basics first though, using this article. I just want to ensure that you know there are nicer ways to manage your packages, dependencies, and virtual environments.
Python 3.4 and above
If you are running Python 3.4+, you can use the venv module baked into Python:
This command creates a venv in the specified directory and copies pip into it as well. If you’re unsure what to call the directory: venv is a commonly seen option; it doesn’t leave anyone guessing what it is. So the command, in that case, would become:
A little further in this article, we’ll look closely at the just-created directory. But let’s first look at how to activate this virtual environment.
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:
Once installed, you can create a virtual environment with:
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:
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:
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 cmd.exe) or $Env:Path (in PowerShell). In my case, on Windows, it looks like this:
C:\Users\erik\Dev\venv\Scripts;C:\Program Files\PowerShell\7;C:\Program Files\AdoptOpen.
It’s a big list, and I only showed the beginning of it. As you can see, the Scripts directory of my venv is put in front of everything else, effectively overriding all the system-wide Python software.
So what does this PATH variable do?
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. It’s the same for Python. When you import a library, Python looks in your PATH for library locations. And that’s where our venv-magic happens: if your venv is there in front of all the other paths, the OS will look there first before looking at system-wide directories like /usr/bin. Hence, anything installed in our venv is found first, and that’s how we can override system-wide packages and tools.
What’s inside a venv?
If you take a look inside the directory of your venv, you’ll see something like this on Windows:
And on Linux and MacOS:
Virtualenv directory tree
You can see that:
- The Python command is made available as both python and python3 (on Linux and MacOS), and the version is pinned to the version with which you created the venv by creating a symlink to it.
- On Windows, the Python binary is copied over to the scripts directory.
- All packages you install end up in the site-packages directory.
- We have activation scripts for multiple shell types (bash, csh, fish, PowerShell)
- Pip is available under pip and pip3, and even more specifically under the name pip3.7 because I had a Python 3.7 installation at the time of writing this.
Deactivate the Python venv
Once you have finished working on your project, it’s a good habit to deactivate its venv. By deactivating, you leave the virtual environment. Without deactivating your venv, all other Python code you execute, even if it is outside your project directory, will also run inside the venv.
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 this:
Delete a venv with Pipenv
If you used Pipenv to create the venv, it’s a lot easier. You can use the following command to delete the current venv:
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:
It will output the path to the virtual environment and all of its files and look similar to the example above. The next step is to remove that entire directory, and you’re done.
Delete a venv with Poetry
If you created the virtualenv with Poetry, you can list the available venvs with the following command:
You’ll get a list like this:
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:
Follow the course
Stop feeling like a voodoo coder and learn this stuff properly once and for all. Our Python Fundamentals course extensively explains Modules and packages, Virtual environments, and Package managers. Give it a try, I assure you that you’ll like it!
This article is part of a free Python Tutorial. You can browse the tutorial with the navigation buttons at the top and bottom of the article or use the navigation menu. Want to learn more? Here are some great follow-up reads:
- Next up: how to install packages with pip inside your venv is a better way of managing your venv and packages.
- Learn the most common Linux commands (like cd, mkdir, pwd, etcetera) : If you want to know all the details and command-line options
You learned how to create, activate, deactivate, and delete virtual environments. We also looked 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 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.
Get certified with our courses
Our premium courses offer a superior user experience with small, easy-to-digest lessons, progress tracking, quizzes to test your knowledge, and practice sessions. Each course will earn you a downloadable course certificate.
This article is part of the free Python tutorial. You can head over to the start of the tutorial here. You can navigate this tutorial using the buttons at the top and bottom of the articles. To get an overview of all articles in the tutorial, please use the fold-out menu at the top.
If you liked this article, you might also like to read the following articles:
5 Ways of Deleting Python Venv
Before you learn about deleting python venv, this article here will help you understand Python venvs first. Python venv is a tool for creating a virtual environment to isolate python projects from each other while working on them. But sometimes, those virtual environments need to be deleted.
What is the virtual environment?
A python virtual environment is a tool that creates isolated spaces for different projects to maintain the dependencies. In other words, Virtual Environment is an essential tool for python developers to create remote environments to separate the dependencies of various projects.
It is used because of its easy installation processes and easy-to-define model. It also prevents version conflicts and ensures the project works well with the version tested with your code. The Python Virtual environment also allows you to install packages locally to your projects if you are working on a shared host.
How to create a virtual environment?
Python venv is a commonly used tool for the Virtual Environment. In earlier models of Python, you need to install the venv. For example, in python 2, you can install python venv by using the following codes:
In newer versions of Python which is onwards 3.3, the venv subset is already present, so that may not require any additional downloading of the venv for the virtual environment.
Why deleting python venv is needed sometimes?
There can be many reasons to delete virtual environments. The most common reason is that the project files and virtual environment related to a particular project are no longer needed. Apart from it, there are other reasons to delete a virtual environment, ranging from a typo in the name of the virtual environment to having issues while running it. Thus the deletion of the virtual environment is required at times. This process requires only the deletion of the folder with the virtual environment, which will ultimately deactivate or delete the virtual environment. This process can be done by various methods, which are pretty easy to follow.
101 Python venv Guide: Create, Delete, Activate and Deactivate
Learn the basics of venv create, delete and activate commands to optimize your work!
You are quite efficient if you install third-party applications system-wide. After all, you get it once and then use the package from different python projects, this saves in a lot of time and space on the disk. However, some problems might arise with junk and overlapping libraries or environments.
You can avoid all of this by following our tutorial on how to activate, deactivate, delete and create Python venv environments with command examples that work on Linux, Windows and Mac.
What is Python venv and how it works
The PATH variable gets changes once you activate your environment. On Linux and macOS you can see by printing the path with echo $path. On Windows, you need to use %PATH%(in cmd.exe) or $Env:Path ( in PowerShell).
Now when you enter a command which can’t be found in the current working directory then your operating system begins to look at all paths there in the PATH variable. The same is for Python. Whenever you import any library it started to look in your PATH for library locations.
This is where the new magic of venv occurs; if it is in front of other paths, the operating system shall look first there before peeking into the system-wide directories such as /usr/bin. Therefore, anything which gets installed in venv is found first and that’s how you can override system-wide packages and tools.
On Linux and MacOS, you shall see the follow tree structure:
- On Linux and macOS Python command is made available as both Python and Python, the version to is pinned to the one with which you have created venv by creating a symlink to it.
- Python binary on windows is copied over to the scripts directory.
- Packages that are installed end up in the site-packages directory.
- There are activation scripts for different types of shells such as bash, csh, fish and PowerShell.
- You shall find pip under different names like pip and pip3, specifically under the name pip3.7 as we had version 3.7 installed.
Reason why people use Python virtual environments
As you can imagine that project Paul is written down against a specific version of Library Ninja. You may need to upgrade the library Ninja in the near future. For example, you require the latest version for another project known as IDE. You upgrade library Ninja to the latest and project IDE begins to work fine. But during this what you see is that your previous project A broke badly. After, APIs can change on major version upgrades.
Now, this is a very virtual environment that comes in handy, it is designed to fix such a problem by isolating the project from other and system-wide packages. You install packages within this environment, particularly for the project on which you are working.
The best thing is that it is easy to define and install packages specific to your project. When you use the “requirements.txt” file you can define an exact number of versions tested with code. Now, it also helps other users of the same software as the environment helps others to reproduce the same for which your software was built in the first place.
If you are working on a university or web hosting provider which is a shared host then you shall not able to install system-wide packages as you do not have administrator rights to do so. In such time, venv allows you to get anything you in your project locally.
Virtual Environment compared with Kubernetes, Docker or VMWare solution
You have other options as well to isolate your project:
- In some scenarios, you can get a second system and on it run your code. Though this would not be easy on the pocket your problem shall be solved.
- It is cheap but still needs you to install a complete OS. This is deemed to be a waste in many cases. You may need VMWare or Oracle VirtualBox and then install and fully configure an operating system.
- Then comes containerization with the likes of Kubernetes and Docker. These are quite powerful and a good alternate.
Create Python venv Environments with 3 simple commands
There are many ways to do this but it depends on the version of Python you are running.
Keep two tools in your mind; python poetry and pipenv. Both these combine functions of tools which you are about to learn: virtualenv and pip. Besides this they can add extras such as their ability to perform proper dependency resolution.
Use the following venv module:
Using this command shall help you create a virtual environment in the specific directory plus copies pip into it too. If you are not very sure on what to call the director then it is a commonly seen option and does not leave on guessing.
Other Versions of Python which are older:
Alternate which works for any version of python is using virtualenv package, but first what you need to do is install pip, for this use the following command:
Once this has been installed, create a virtual environment using:
Activate Python venv environment
To activate this on Microsoft Window all you need to do is run a script that gets installed by venv, if you have created this in a directory known myenv, the command shall be as follows:
Open PowerShell as administrator and run the below:
Activating Venv in Linux and Apple macOS
With the help of the source code command, we activate the virtual environment on Linux and macOS. If this has been created in myvenv directory then the command shall be as followed:
Now you are ready to rock and all, install packages with pip but we suggest that you keep reading to understand more.
Deleting environment with Pipenv
This is much easier, simply use the following command:
Just keep in mind to be within the project directory.
If this does not work, don’t worry remove it manually. For this simply first you need to ask pipenv where the original virtualenv is located and then use the following command:
This shall output path to the virtual environment along with its files and look similar to the examples mentioned above. After this, all you need to do is remove the entire directory and that’s it you are done.
If you have created with poetry then list the ones which are available by using the following command:
Now, if you want to remove the environment use the poetry env remove command. For this, you need to specify the exact name from the above-mentioned output, such as: poetry env remove test-paul-ninja-env.py. You can also use an IDE such as Ninja to alter and create a script to run these at a scheduled time or in a sequence to save time.
Complete Deactivate Python venv
Once you’re done with your work the best thing is to deactivate it. By doing this you basically leave the virtual environment. If you don’t do this, python code shall execute even if it is outside the project directory it shall run within venv.
The best thing is that it works on Windows, Linux and Mac setups.
Deleting a venv environment
This can be done easily but it depends on what you used to create it. We have listed some common options for you which one you would have created with Virtualenv or Python –m venv.
You first need to deactivate it, and remove the directory and its content. On systems like UNIX and PowerShell, first deactivate it as showed up then:
If you see “venv” as the directory, run the below command to delete it permanently. Be warned that there is no coming back once removed.
Other Python tutorials you will enjoy:
Python venv saves a lot of time and helps organize projects which need isolation from others. This is where you can use our venv guide to create, activate, delete and deactivate environments. Hopefully, our command examples will clarify it all to you as they are compatible with Windows 10, 11, Linux and macOS.
How to Delete virtualenv in Python : Only 2 Steps
In python, you do most of the code in a custom-created virtual environment for a specific project. It makes the other projects separate from the other. All the modules installed in the virtual environment will not have effects on the other virtual environments. Let’s say you have already created a virtual environment for python then how you can delete that virtual environment? In this entire post, you will learn how to Delete virtualenv in python with steps.
How to create a VirtualEnv in python
Before going to the deletion part let’s first know how you can create a virtual environment in python. You can create a virtual environment using the py command.
Go to your path of the directory and Open your terminal. After that run the below command to create virtualenv in python.
It will create a virtual environment for the name env. After that, you have to activate it to use this virtual environment.
Now you will see the ( env ) before the path in the terminal. Activating virtual environment
You can also know the path of the virtual environment using the below command. It will list out all the paths of the python installed in your system.
How to Delete virtualenv (Virtual Environment ) in python
The above part was for the creation of a virtual environment in python. In this section, you will know the various solution to delete virtualenv in python.
Step 1: Deactivate the virtual environment
The first step before deleting virtualenv is to deactivate the environment you have created. To do you have to use the deactivate command.
Type the below command to deactivate venv.
Now you will not see the (env) before the directory path. It means the virtual environment is deleted.
Step 2: Delete the venv directory
After deactivating the virtualenv now remove the directory that were containing all the files after the creation of the env environment.
Run the below line of code to remove the directory.
Creation of the virtualenv is very simple but the deletion can take much time if you do not know how to perform it. The above steps will help you to delete virtualenv in python.
I hope this tutorial has helped you to achieve the task that you want. If you have any queries then you can contact us for help.
- Total 0
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
We respect your privacy and take protecting it seriously
Thank you for signup. A Confirmation Email has been sent to your Email Address.