Python virtual environments
Python lets you reuse code written by other developers. These reusable collections of code are called libraries. For example, installing FastAPI gives you ready-made functionality for building web APIs.
Installing every library into one shared Python environment can cause conflicts when different projects need different versions. Some Python distributions, including Homebrew’s, also protect their global environment from direct package installation.
Instead, create a virtual environment for each project. Starting a Python project by creating its virtual environment is standard practice.
Creating a virtual environment
Python includes a module named venv for this purpose. Create a project folder, enter it, and create an environment named .venv.
mkdir playground
cd playground
python3 -m venv .venv
mkdir playground
cd playground
py -m venv .venv
The final command creates a .venv folder containing the project’s Python environment and its own library installation directory.
Naming your environment
.venv is a widely used convention. The name is short for “virtual environment.”
On macOS and Linux, a leading dot (.) marks a file or folder as hidden in normal listings. This helps keep the environment separate from your main project files. Windows does not treat a leading dot as a hidden attribute, but the same name works there too.
Activating a virtual environment
Activate the environment to make python and pip in your terminal use the project’s environment rather than global Python.
source .venv/bin/activate
.venv\Scripts\Activate.ps1
Your prompt will normally gain a (.venv) prefix:
(.venv) playground $
Python now runs in the project’s isolated environment. Installing libraries here will not change the libraries in other projects’ environments.
Deactivating a virtual environment
When you are finished, return to the previous environment with the same command on either operating system:
deactivate
Exercises
Decide whether each statement is True or False.
- Creating a virtual environment is a common first step in a new Python project.
- After activation, the
pythoncommand still uses the global Python environment. deactivateends the active virtual environment session.
Answers and explanations
- True — Separate environments keep each project’s library versions independent.
- False — Activation makes the terminal use Python from
.venv. - True — It deactivates the environment and restores the previous command search path.