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

Python virtual environments allow you to install Python packages in a location isolated from the rest of your system instead of installing them system-wide. Let’s look at how to use Python venv’s, short for Python virtual environments, 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
  • How to check which Python your venv is using
  • Different ways to delete or remove a venv
  • How a venv works internally

Python venv cheat sheet

These are the commands for the most common venv tasks. The rest of this article explains each one.

TaskWindowsmacOS and Linux
Create a venvpython -m venv .venvpython3 -m venv .venv
Activate (start) itcmd: .venv\Scripts\activate.bat
PowerShell: .venv\Scripts\Activate.ps1
source .venv/bin/activate
Exit (deactivate) itdeactivatedeactivate
Check which Python runsGet-Command pythonwhich python
Delete itcmd: rmdir /s .venv
PowerShell: rm -r .venv
rm -r .venv

Starting a venv takes two steps: you create it once per project, and you activate it again every time you open a new terminal. Activation only lasts as long as that terminal session. If you use uv, you can skip activation altogether: uv run runs your command inside the project’s .venv for you.

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.

If you’d rather see this problem happen than read about it, the free lesson One Python, many projects walks you through it in your browser.

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.

On many systems, a venv is no longer optional. Recent versions of Ubuntu, Debian, Fedora, and Homebrew’s Python refuse a plain pip install outside a virtual environment and print error: externally-managed-environment. The pip article explains that error; the short answer is: create a venv.

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 was a bit expensive, though!
  2. 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.
  3. 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

Every Python version that is still supported comes with the venv module, so there’s nothing to install. Go to your project directory and run:

python -m venv .venvCode language: Shell Session (shell)

This creates a virtual environment in a directory called .venv and puts a copy of pip inside it. You can pick any name, but .venv is the common choice these days: the dot hides the directory in most file listings, and tools like VS Code and uv look for that name automatically. You’ll also see venv without the dot in older tutorials. It works exactly the same.

A few platform notes:

  • On macOS and most Linux distributions, the command may be called python3 instead of python. Use python3 -m venv .venv in that case.
  • On Ubuntu and Debian, the venv module is a separate package. If you get an error saying ensurepip is not available, run sudo apt install python3-venv first.
  • The venv uses the Python you created it with. If you have several versions installed, pick one explicitly: python3.14 -m venv .venv on macOS and Linux, or py -3.14 -m venv .venv on Windows.

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.

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 .venv, the command would be:

# In cmd.exe
.venv\Scripts\activate.bat
# In PowerShell
.venv\Scripts\Activate.ps1Code language: Shell Session (shell)

If PowerShell answers that running scripts is disabled on this system, allow scripts for your own account once with Set-ExecutionPolicy -Scope CurrentUser RemoteSigned and try again.

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 .venv directory, the command would be:

$ source .venv/bin/activate
(.venv) $Code language: Shell Session (shell)

Your prompt now starts with (.venv), on all operating systems. That’s your reminder that the venv is active. 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.

Want to try creating and activating a venv without touching your own machine first? The free lesson on creating and activating .venv gives you a terminal in the browser to practice in.

Which Python is my venv using?

The most common venv problem is a package that is installed, but Python says it can’t find it. Nine times out of ten, the python you’re running isn’t the one inside your venv. You can check that in two ways.

First, ask your shell which python it finds. On Linux and macOS, use which:

(.venv) $ which python
/home/erik/projects/myproject/.venv/bin/pythonCode language: Shell Session (shell)

On Windows, use where python in cmd.exe or Get-Command python in PowerShell. The first result should point into your project’s .venv\Scripts directory.

Second, ask Python itself. This works the same on every OS, and inside editors and notebooks where you don’t have a shell prompt:

(.venv) $ python -c "import sys; print(sys.prefix)"
/home/erik/projects/myproject/.venvCode language: Shell Session (shell)

If sys.prefix points to your venv, you’re in. If it points somewhere like /usr or C:\Users\you\AppData\Local\Python, you’re running the system Python: activate the venv again, or select the venv’s interpreter in your editor. Running python -m pip instead of plain pip also helps, because it guarantees that pip installs into the same Python you just checked.

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, your OS looks for it in all the directories listed in the PATH variable, in order, and runs the first match. Because the venv’s directory is at the front, typing python or pip starts the copies inside your venv instead of the system-wide ones.

Python then does the rest. When the venv’s python starts, it finds the pyvenv.cfg file next to it and sets sys.prefix to the venv directory. From then on, imports look in the venv’s own site-packages directory, and anything you install with pip lands there too. That’s why the venv’s python works even without activation: .venv/bin/python my_script.py uses the venv’s packages just the same. Activation only saves you the typing.

What’s inside a venv?

If you take a look inside the directory of your venv, you’ll see a folder structure like this on Windows:

.venv ├── Include ├── Lib │ └── site-packages ├── pyvenv.cfg └── Scripts ├── activate ├── activate.bat ├── Activate.ps1 ├── deactivate.bat ├── pip.exe ├── pip3.exe ├── pip3.14.exe ├── python.exe └── pythonw.exe

And on Linux and MacOS, for a venv created with Python 3.14:

.venv ├── bin │ ├── Activate.ps1 │ ├── activate │ ├── activate.csh │ ├── activate.fish │ ├── pip │ ├── pip3 │ ├── pip3.14 │ ├── python -> python3.14 │ ├── python3 -> python3.14 │ └── python3.14 -> /usr/bin/python3.14 ├── include ├── lib │ └── python3.14 │ └── site-packages └── pyvenv.cfg

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, a small python.exe is placed in the Scripts directory that starts the Python you created the venv with.
  • 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 also under a name with the exact Python version, like pip3.14.
  • The pyvenv.cfg file records which Python the venv was made from. It’s the file Python looks for to know it’s running inside a venv.

Deactivate (exit) 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. Closing the terminal window also ends the venv, because activation never outlives the terminal session.

deactivate: command not found

If your shell answers deactivate: command not found, or PowerShell says the term ‘deactivate’ is not recognized, there is no active venv in this terminal. The deactivate command isn’t installed anywhere on your system: the activate script creates it when you activate a venv and removes it again when you deactivate. The usual reasons:

  • The venv isn’t active. If your prompt doesn’t start with (.venv), there’s nothing to leave, and you’re already done.
  • You activated it in another terminal. Every terminal window or tab has its own environment. Deactivate in the one where you activated it, or simply close that one.
  • You ran the activate script instead of sourcing it. On macOS and Linux, bash .venv/bin/activate or ./.venv/bin/activate runs the script in a separate process that ends right away, so nothing gets activated in your own shell. Use source .venv/bin/activate (or the short form . .venv/bin/activate).
  • It isn’t a venv. A conda environment is left with conda deactivate. A shell started with poetry shell or pipenv shell is left with exit.

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 python -m venv, virtualenv, or uv

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

If you want to delete this venv, 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:

deactivate
# If your virtual environment is in a directory called '.venv':
rm -r .venvCode language: Shell Session (shell)

In cmd.exe, the command is rmdir /s .venv. Deleting a venv is safe as long as you recorded what it contained, for example in a requirements.txt file. You can rebuild it in a minute.

Delete a venv with Poetry or Pipenv

Poetry keeps its virtual environments outside your project directory by default. Inside the project directory, list them and remove the one you want, or all of them at once:

poetry env list
poetry env remove <name from the list>
# or remove every environment of this project:
poetry env remove --allCode language: Shell Session (shell)

With Pipenv, run pipenv --rm from the project directory.

Other tools that create virtual environments

python -m venv is all you need, and it’s what every other tool builds on. Once you understand it, you may like one of these:

  • uv is a fast, modern tool that creates venvs (uv venv, which uses .venv by default) and installs packages into them (uv pip install). It can also install Python itself. The free lesson Meet uv after the pieces make sense maps each uv command to the venv and pip commands from this article, and our uv tutorial covers the rest: projects, lock files, Python versions, and tools.
  • Poetry combines the venv with dependency management and packaging.
  • Pipenv does something similar and is still used in some projects.
  • The virtualenv package is the original tool that venv grew out of. It still exists and has a few extra options, but for most projects venv does the job.

Continue learning

Want to work through this step by step? Follow our free lesson on creating and activating a Python virtual environment.

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:

Conclusion

You learned how to create, activate, deactivate, and delete virtual environments, and how to check which Python you’re running. 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, have a look at uv or Poetry. These tools combine the management of your virtual environment with proper package and dependency management.

Related articles

3 thoughts on “Python venv: How To Create, Activate, Deactivate, And Delete”

    • Venvs can be created anywhere, so there’s no way to get a list of all of them. Unless you use something like poetry, which has the poetry env list command to list all the available venv’s.

Leave a Comment