Virtual Environments
Soon you will install packages that other people wrote. If every project on your computer installs into the same Python, one project's upgrade can break another, and after a year nobody can say which project needed what. A virtual environment solves this: a private folder of packages for one project, created with a single command and thrown away just as easily.
Think of keeping a separate toolbox for each job. The plumbing kit does not get mixed up with the woodworking kit, you can take one to a friend's house without the other, and emptying one never touches the rest.
The Problem
Imagine two projects. One needs version 2 of a package; the other was written for version 3, which works differently. With one shared installation there is room for only one version, so whichever you install, one project breaks. Nothing in the error message will say "because of the other project". Separate environments make the conflict impossible.
Making One
Inside your project folder, run the venv module that comes with Python and give it a name for the new folder. By convention the name is .venv, with a dot at the front. The command creates that folder: a small structure that points at the Python you already have, with an empty place for packages. It takes a few seconds and prints nothing.
you@mac python-course % python3 -m venv .venv you@mac python-course %
PS C:\Users\you\python-course> python -m venv .venv PS C:\Users\you\python-course>
The -m means "run this module as a program", and it is how this book runs Python's own tools: python -m venv here, python -m pip in the next topic, python -m pytest in Chapter 12.
Activating It
A virtual environment is used by activating it in a terminal. On a Mac, run source .venv/bin/activate. On Windows, in PowerShell, run the script .venv\Scripts\Activate.ps1. Either way, the prompt changes to start with the environment's name in brackets, which is how you can tell at a glance that it is active.
you@mac python-course % source .venv/bin/activate (.venv) you@mac python-course % python --version Python 3.15.0
PS C:\Users\you\python-course> .venv\Scripts\Activate.ps1 (.venv) PS C:\Users\you\python-course> python --version Python 3.15.0
On Windows, the first attempt may be refused with a message that running scripts is disabled on this system. That is a PowerShell safety setting, not a problem with Python. The Python documentation gives the one-time fix: run Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser in PowerShell, answer yes if asked, and activate again. It changes the setting for your user account only.
Inside, python Is python
Look at the command in the last two blocks: python, on both systems. Inside an active environment, python means that environment's Python, on a Mac too. From here on this book writes python everywhere, and assumes an environment is active whenever a package is involved.
Activation lasts for that terminal window. Close the window, or type deactivate, and the prompt loses its brackets. Open a new terminal tomorrow and you activate again.
What Not to Do with It
The .venv folder is disposable. Never edit files inside it, never put your own code in it, and never send it to anyone or save it in a backup of your project: it can always be deleted and recreated from scratch. The list of packages the project needs is what you keep, and the next topic shows how.
- "A virtual environment is a second copy of Python I have to install." It is a small folder that points at the Python you already have, plus room for packages.
- "Once activated, it stays active." Activation lasts for that terminal window. A new terminal needs activating again.
- "The .venv folder is part of my project to keep." It is disposable. Keep the list of packages instead, and recreate the folder whenever you need it.
- "The Windows script error means Python is broken." It is PowerShell's execution policy. The documented one-time command allows scripts for your user.
- Every professional Python project runs in its own environment. Starting that way avoids the tangle before it begins.
- It is also the one place where the command is simply
python, on every system.
Knowledge Check
What problem does a virtual environment solve?
- Projects needing different package versions clash
- Python running too slowly on an older computer
- Programs that need to be written in two languages
- Files saved with the wrong text encoding
How can you tell that a virtual environment is active?
- The terminal window changes its colour
- The prompt starts with (.venv)
- A message appears every time you type python
- The .venv folder disappears from the project
What should you do with the .venv folder when sharing your project?
- Send it along, so others get the same packages
- Edit it so that it contains your own code as well
- Rename it so that it is not deleted by accident
- Leave it out; share the list of packages instead
On Windows, activation is refused because running scripts is disabled. What is the fix?
- Reinstall Python from scratch with administrator rights
- Use a different name for the .venv folder and try again
- Activate the environment from inside Python's own interactive prompt
- Allow scripts for your user with Set-ExecutionPolicy
You got correct