Python course · Module 6: Modules and environments · Lesson 30 of 63
Quick answer: A Python virtual environment (venv) is a folder with its own Python and its own installed packages, made for one project. Create it inside your project folder with python3 -m venv .venv (on Windows: py -m venv .venv). Activate it with source .venv/bin/activate on macOS and Linux, or .venv\Scripts\activate on Windows, and then install packages with python -m pip install. Type deactivate to leave, and delete the .venv folder to remove it.
In this lesson you will learn:
- why you need a virtual environment and what the
externally-managed-environmenterror means - how to create a venv with
python -m venv .venv - how to activate it on Windows (Command Prompt and PowerShell), macOS and Linux, and check which Python runs
- how to install packages inside it, save them in
requirements.txtand rebuild the venv - how to keep
.venvout of Git, deactivate it and delete it
Before you start: finish Lesson 29, pip install: How to Install Python Packages with pip. You also need a terminal, as in Lesson 3. All commands were run on Ubuntu 24.04 (Linux) with Python 3.14.6. The newest release on python.org is Python 3.15.2, and the commands are the same there.
Table of Contents
Why you need a virtual environment
When you install a package with pip, it goes into one folder that belongs to one Python installation. If all your projects share that folder, they also share every package version. Project A may need an old version of a library and project B a new one, but only one version can be installed at a time.
A virtual environment fixes this. The venv documentation says each virtual environment has its “own independent set of Python packages”, and it is isolated “so that only those explicitly installed in the virtual environment are available”. One project, one .venv folder, no conflicts.
Some systems refuse to let pip change their own Python at all. This is Ubuntu 24.04’s own Python 3.12 on our test machine:
$ /usr/bin/python3.12 -m pip install requests
error: externally-managed-environment
× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
python3-xyz, where xyz is the package you are trying to
install.
If you wish to install a non-Debian-packaged Python package,
create a virtual environment using python3 -m venv path/to/venv.
Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
sure you have python3-full installed.
If you wish to install a non-Debian packaged Python application,
it may be easiest to use pipx install xyz, which will manage a
virtual environment for you. Make sure you have pipx installed.
See /usr/share/doc/python3.12/README.venv for more information.
note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.
Nothing was installed. This rule comes from PEP 668, written to stop “conflicts between OS package managers and Python-specific package management tools like pip”. The system’s own programs use that Python, so it is protected. The message gives the answer: create a virtual environment. Do not use --break-system-packages; as the note says, it can break your system.
Create a virtual environment with python -m venv
Make a project folder, go into it and run the venv module with -m:
$ mkdir myproject
$ cd myproject
$ python3 --version
Python 3.14.6
$ python3 -c "import sys; print(sys.prefix != sys.base_prefix)"
False
$ python3 -m venv .venv
$ ls -A .venv
.gitignore bin include lib lib64 pyvenv.cfg
python3 -m venv .venvcreates a new folder called.venv. It prints nothing when it works..venvis just a folder name. The Python Packaging User Guide says you can “just create this in your project and call it.venv“. On macOS and Linux, a name that starts with a dot is hidden, so usels -Ato see it.- The second command prints
False: we are not in a virtual environment yet. The venv docs say that checkingsys.prefix != sys.base_prefixis enough to tell.
On Windows, the same command is py -m venv .venv (or python -m venv .venv). If python3 or py does not work at all, go back to Lesson 2.
What is inside? pyvenv.cfg records which Python the venv was made from (the docs call it the home key). lib will hold the packages you install. bin holds the programs:
$ ls .venv/bin
Activate.ps1 activate activate.csh activate.fish pip pip3 pip3.14 python python3 python3.14 𝜋thon
There is a python, a pip and one activate script for each kind of shell. 𝜋thon is just one more link to the same python3 (a venv made with Python 3.15.2 did not have it); ignore it. On Windows, this folder is called Scripts.
Activate the virtual environment
Activating means: “for this terminal window, use the Python in .venv“. The venv docs explain that the activate script puts the venv’s bin (or Scripts) folder at the front of your PATH, “so that running python will invoke the environment’s Python interpreter”. The command depends on your system.
macOS and Linux (bash or zsh)
$ source .venv/bin/activate
(.venv) $ which python
/home/learner/python-course/myproject/.venv/bin/python
(.venv) $ python --version
Python 3.14.6
(.venv) $ python -c "import sys; print(sys.prefix != sys.base_prefix)"
True
- The prompt now starts with
(.venv). That is your sign that the venv is active. which pythonshows the full path of thepythoncommand. It ends with.venv/bin/python, so it is the venv’s Python.- Inside the venv you can type
python, even on systems where onlypython3exists outside it. - The check now prints
True.
Windows Command Prompt (cmd.exe)
We could not run Windows on our test machine, so these commands come from the official venv docs and the Packaging User Guide, with no output shown:
py -m venv .venv
.venv\Scripts\activate.bat
where python
where python is the Windows version of which python. The guide says the path should end with .venv\Scripts\python.
Windows PowerShell
py -m venv .venv
.venv\Scripts\Activate.ps1
PowerShell may refuse to run Activate.ps1 if its execution policy does not allow scripts. The venv docs say you can allow it for your own user with this command, run once:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Then run .venv\Scripts\Activate.ps1 again.
Install packages inside the venv with pip
With the venv active, install packages exactly as in Lesson 29. We use python -m pip, which always means “the pip of this Python”:
(.venv) $ python -m pip install requests
Collecting requests
Downloading requests-2.34.2-py3-none-any.whl.metadata (4.8 kB)
Collecting charset_normalizer<4,>=2 (from requests)
Downloading charset_normalizer-3.5.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.metadata (46 kB)
Collecting idna<4,>=2.5 (from requests)
Downloading idna-3.20-py3-none-any.whl.metadata (7.2 kB)
Collecting urllib3<3,>=1.26 (from requests)
Downloading urllib3-2.8.0-py3-none-any.whl.metadata (7.4 kB)
Collecting certifi>=2023.5.7 (from requests)
Downloading certifi-2026.10.23-py3-none-any.whl.metadata (2.5 kB)
Downloading requests-2.34.2-py3-none-any.whl (73 kB)
Downloading charset_normalizer-3.5.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (255 kB)
Downloading idna-3.20-py3-none-any.whl (69 kB)
Downloading urllib3-2.8.0-py3-none-any.whl (135 kB)
Downloading certifi-2026.10.23-py3-none-any.whl (135 kB)
Installing collected packages: urllib3, idna, charset_normalizer, certifi, requests
Successfully installed certifi-2026.10.23 charset_normalizer-3.5.2 idna-3.20 requests-2.34.2 urllib3-2.8.0
(.venv) $ python -m pip list
Package Version
------------------ ----------
certifi 2026.10.23
charset-normalizer 3.5.2
idna 3.20
pip 26.1.2
requests 2.34.2
urllib3 2.8.0
requests needs four other packages, so pip installed five. A new venv starts empty, so pip list shows only these and pip itself. You may get newer versions than ours.
Now save this small program as app.py in myproject:
import sys
import requests
print("Python:", sys.executable)
print("requests version:", requests.__version__)
(.venv) $ python app.py
Python: /home/learner/python-course/myproject/.venv/bin/python
requests version: 2.34.2
sys.executable is the path of the Python that runs the program. It is the one in .venv, and the import works (Lesson 27 explains import).
Deactivate the virtual environment
Type deactivate to go back to your normal Python. Then run the same program again:
(.venv) $ deactivate
$ python3 app.py
Traceback (most recent call last):
File "/home/learner/python-course/myproject/app.py", line 3, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
$ .venv/bin/python app.py
Python: /home/learner/python-course/myproject/.venv/bin/python
requests version: 2.34.2

- Without
(.venv),python3is the normal Python again. It has never seenrequests, so the import fails: the venv keeps your packages separate. .venv/bin/python app.pyworks without activating. The docs say you do not “need” to activate a virtual environment: you can give the full path to its Python. On Windows that path is.venv\Scripts\python.
Save and rebuild the venv with requirements.txt
You do not copy or share a venv. The venv docs say it should be “simple to delete and recreate it from scratch”, and that it is “not considered as movable or copyable”. What you share is a list of packages. pip freeze prints the exact versions installed, and > saves that list in a file:
$ source .venv/bin/activate
(.venv) $ python -m pip freeze > requirements.txt
(.venv) $ cat requirements.txt
certifi==2026.10.23
charset-normalizer==3.5.2
idna==3.20
requests==2.34.2
urllib3==2.8.0
Now delete the venv completely, make a new one and install everything from the file with -r:
(.venv) $ deactivate
$ rm -rf .venv
$ python3 -m venv .venv
$ source .venv/bin/activate
(.venv) $ python -m pip install -r requirements.txt
Collecting certifi==2026.10.23 (from -r requirements.txt (line 1))
Using cached certifi-2026.10.23-py3-none-any.whl.metadata (2.5 kB)
Collecting charset-normalizer==3.5.2 (from -r requirements.txt (line 2))
Using cached charset_normalizer-3.5.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl.metadata (46 kB)
Collecting idna==3.20 (from -r requirements.txt (line 3))
Using cached idna-3.20-py3-none-any.whl.metadata (7.2 kB)
Collecting requests==2.34.2 (from -r requirements.txt (line 4))
Using cached requests-2.34.2-py3-none-any.whl.metadata (4.8 kB)
Collecting urllib3==2.8.0 (from -r requirements.txt (line 5))
Using cached urllib3-2.8.0-py3-none-any.whl.metadata (7.4 kB)
Using cached certifi-2026.10.23-py3-none-any.whl (135 kB)
Using cached charset_normalizer-3.5.2-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl (255 kB)
Using cached idna-3.20-py3-none-any.whl (69 kB)
Using cached requests-2.34.2-py3-none-any.whl (73 kB)
Using cached urllib3-2.8.0-py3-none-any.whl (135 kB)
Installing collected packages: urllib3, idna, charset-normalizer, certifi, requests
Successfully installed certifi-2026.10.23 charset-normalizer-3.5.2 idna-3.20 requests-2.34.2 urllib3-2.8.0
(.venv) $ python app.py
Python: /home/learner/python-course/myproject/.venv/bin/python
requests version: 2.34.2
The new venv has exactly the same versions, and app.py works again. “Using cached” means pip reused the files it downloaded earlier. A teammate runs the same three steps: create, activate, install with -r.
Keep .venv out of Git
Never put .venv in Git. Ours already held 17 MB in 1,061 files, and it only works on the computer and in the folder where it was made. Since Python 3.13, venv creates a .gitignore file inside the venv that tells Git to ignore the whole folder:
(.venv) $ cat .venv/.gitignore
# Created by venv; see https://docs.python.org/3/library/venv.html
*
(.venv) $ git init -q
(.venv) $ git status --short
?? app.py
?? requirements.txt
The * line means “ignore everything in this folder”, so git status lists only app.py and requirements.txt. The Packaging User Guide still says you “should exclude your virtual environment directory” with .gitignore, so it is a good habit to also add the line .venv/ to your project’s own .gitignore file. That covers older Pythons and venvs made by other tools.
Delete a virtual environment
There is no uninstall command. Deactivate it, then delete the folder:
- macOS and Linux:
rm -rf .venv - Windows Command Prompt:
rmdir /s /q .venv(rmdir docs), or delete the folder in File Explorer
Your code and requirements.txt are outside .venv, so they stay safe. Never save your own code inside .venv. If you move or rename the project folder, the venv docs say to recreate the venv in the new place, because it stores full paths.
Try it yourself
Exercise 1: Am I in a venv?
Write in_venv.py that prints Inside a virtual environment: and sys.prefix when it runs in a venv, and Not in a virtual environment otherwise. Use if/else from Lesson 11. Run it with python3 before activating, and with python after activating.
Exercise 2: A second project
Make a new folder textstats next to myproject, with its own venv. Install the humanize package and run a program that prints humanize.naturalsize(3_000_000) and humanize.intcomma(1234567). Then run python -m pip list. Is requests there?
Exercise 3: Pin it
In textstats, save your installed packages to requirements.txt and show the file.
Solutions
Solution 1
import sys
if sys.prefix != sys.base_prefix:
print("Inside a virtual environment:", sys.prefix)
else:
print("Not in a virtual environment")
$ python3 in_venv.py
Not in a virtual environment
$ source .venv/bin/activate
(.venv) $ python in_venv.py
Inside a virtual environment: /home/learner/python-course/myproject/.venv
Inside the venv, sys.prefix is the .venv folder, so it differs from sys.base_prefix.
Solution 2
import humanize
print(humanize.naturalsize(3_000_000))
print(humanize.intcomma(1234567))
$ mkdir textstats
$ cd textstats
$ python3 -m venv .venv
$ source .venv/bin/activate
(.venv) $ python -m pip install humanize
Collecting humanize
Downloading humanize-4.16.0-py3-none-any.whl.metadata (8.0 kB)
Downloading humanize-4.16.0-py3-none-any.whl (137 kB)
Installing collected packages: humanize
Successfully installed humanize-4.16.0
(.venv) $ python sizes.py
3.0 MB
1,234,567
(.venv) $ python -m pip list
Package Version
-------- -------
humanize 4.16.0
pip 26.1.2
We saved the program as sizes.py. requests is not there: each venv has only its own packages.
Solution 3
(.venv) $ python -m pip freeze > requirements.txt
(.venv) $ cat requirements.txt
humanize==4.16.0
humanize needs no other packages, so the file has one line.
Common mistakes
- Forgetting to activate.
python3 app.pyuses the normal Python and fails withModuleNotFoundError: No module named 'requests'. Look for(.venv)in the prompt, or run.venv/bin/python app.py. - Running the activate script without
source. On macOS and Linux,.venv/bin/activatealone givesbash: .venv/bin/activate: Permission denied. Typesource .venv/bin/activate. - Using the wrong folder or name. If you are not in the project folder, or the venv is called
.venvbut you typevenv, you getbash: venv/bin/activate: No such file or directory. Check withls -A. - Committing
.venvor copying it to another computer. Sharerequirements.txtinstead and rebuild the venv there. - PowerShell blocks
Activate.ps1. Run theSet-ExecutionPolicycommand from the venv docs once, as shown above.
Frequently asked questions
Do I need to activate a virtual environment?
No. Activation is only a shortcut so that python and pip mean the venv’s versions. The venv docs say you can instead give the full path to the venv’s Python, like .venv/bin/python app.py (macOS, Linux) or .venv\Scripts\python app.py (Windows).
Should I commit my .venv folder to Git?
No. The venv docs say a virtual environment is “not checked into source control systems such as Git”. Commit requirements.txt (or pyproject.toml, Lesson 31) and let everyone rebuild their own venv.
How do I know if I am in a virtual environment?
Look for (.venv) at the start of your prompt, run which python (macOS, Linux) or where python (Windows) and check that the path contains .venv, or run python -c "import sys; print(sys.prefix != sys.base_prefix)". It prints True inside a venv.
Summary
- A virtual environment is a
.venvfolder with its own Python and packages for one project; create it withpython3 -m venv .venv(Windows:py -m venv .venv). - Activate it with
source .venv/bin/activate(macOS, Linux),.venv\Scripts\activate.bat(Command Prompt) or.venv\Scripts\Activate.ps1(PowerShell); leave withdeactivate. - Inside it, install with
python -m pip install; outside it, those packages are not available. - Save versions with
python -m pip freeze > requirements.txtand rebuild withpython -m pip install -r requirements.txt. - Keep
.venvout of Git, never put your code in it, and delete it whenever you like.
Next lesson
Next: Lesson 31, pyproject.toml Explained: uv vs pip vs Poetry.
Sources
- venv: Creation of virtual environments (Python docs)
- Install packages in a virtual environment using pip and venv (Python Packaging User Guide)
- PEP 668: Marking Python base environments as “externally managed”
- rmdir (Microsoft Learn)
- Python downloads page (python.org)





