Every Python project depends on packages, and packages have versions. One project needs a recent version of a web framework, another project on the same machine was built against an older one, and a third only needs a small utility library. If everything is installed into one shared Python, those requirements eventually collide, and something breaks in a way that is hard to trace.
A virtual environment solves that problem by giving each project its own isolated folder containing its own Python interpreter links and its own installed packages. Nothing inside one environment affects another. Setting up a python virtual environment venv takes under a minute once you know the commands, and it is considered standard practice for any project larger than a throwaway script.
This guide covers the full routine step by step: creating the environment, activating it on Windows, macOS, and Linux, installing packages, recording them in a requirements file, and rebuilding the same setup on another machine. The workflow pairs naturally with installing libraries such as the one covered in the Python Requests examples, because those packages belong inside an environment, not in your system Python.
What a Virtual Environment Actually Is
A virtual environment is a directory, usually named .venv or venv inside your project folder, that contains a copy of the Python launcher structure, a site-packages folder for installed libraries, and activation scripts for your shell. When the environment is active, the python and pip commands in your terminal point at that folder instead of the system installation.
The isolation is practical rather than theoretical. You can delete the whole folder and the project is clean again. You can have ten projects with ten different package sets on one laptop. And when you share the project, you share a text file listing the requirements rather than the environment folder itself, so teammates on different operating systems can rebuild the same setup.
python --version python -m venv .venv
The first line simply checks which Python version your system reports before you start, a sensible sanity check. The second line is the creation command. The -m venv part tells Python to run the built-in venv module, and .venv is the name of the folder to create. The module ships with Python 3, so there is nothing extra to install for this basic approach. On some Linux systems the very first use may ask you to install a python3-venv package through the system package manager; the error message will say so clearly if that applies to you.
Step 1: Create a Project Folder and the Environment
Start by making a folder for the project and moving into it. Keeping one environment per project folder is the convention that prevents confusion later, especially when you return to a project after weeks away.
mkdir my_project cd my_project python -m venv .venv
The mkdir command creates the folder, cd moves into it, and the venv command builds the environment inside it as a hidden-style folder named .venv. The dot at the front is a common convention that keeps the folder visually separate from your own code files. After the command finishes, you will see the .venv folder containing subfolders such as Scripts on Windows or bin on macOS and Linux, plus a configuration file that records which base Python it was created from.
Do not edit files inside .venv by hand, and do not copy that folder into version control. It is large, machine-specific, and completely rebuildable from your requirements file, which is the small file you do share.
Step 2: Activate the Environment
Creating the environment is not enough. You need to activate it in each terminal session so that python and pip resolve to the environment copies. The activation command differs by operating system and shell, which is the part most beginners get stuck on.
# Windows - Command Prompt .venv\Scripts\activate.bat # Windows - PowerShell .venv\Scripts\Activate.ps1 # macOS and Linux - bash or zsh source .venv/bin/activate
On Windows Command Prompt you run the activate.bat file inside the Scripts folder. In PowerShell the corresponding script is Activate.ps1. If PowerShell refuses to run it because of execution policy, that is a Windows security setting rather than a Python problem, and using Command Prompt or the integrated terminal in your editor avoids the issue for most learners. On macOS and Linux you use the source command with the activate script in the bin folder.
Successful activation changes your terminal prompt so the environment name appears at the front, usually as (.venv). That marker is your confirmation. From this point, python runs the environment interpreter and pip installs into the environment only. You can verify this at any time by checking where the interpreter lives.
python -c "import sys; print(sys.executable)"
This one-liner prints the path of the Python executable that is currently running. If the environment is active, the path should point inside your project’s .venv folder. If it points to a system location instead, activation did not take effect in that terminal window, and you should run the activation command again there. Confirming the interpreter path is a habit worth building, much like checking interpreter and package state before running scripts that read and write files in the file handling notes.
Step 3: Install and Upgrade Packages
With the environment active, install packages with pip exactly as you normally would. The difference is the destination: packages land in the environment’s site-packages folder and are invisible to other projects and to the system Python.
python -m pip install --upgrade pip python -m pip install requests
The first command upgrades pip itself inside the environment, which avoids warnings and older-installer quirks. Using python -m pip rather than the bare pip command guarantees you are talking to the pip that belongs to the active interpreter, a small detail that prevents a whole class of installed-but-not-found confusion. The second command installs the requests library as an example. You would replace it with whatever your project needs, a framework, a data tool, or a testing library.
Installing a specific version uses double equals signs, and listing what is installed uses pip list. Pinning a version matters when a project was tested against one release and a newer release changes behaviour.
python -m pip install "requests==2.32.3" python -m pip list
The quoted version spec tells pip to install exactly that release. The list command then shows every package in the environment with its version, including dependencies that were pulled in automatically. Reviewing that list occasionally is good hygiene; you will spot packages you experimented with and no longer need, and you can remove them with pip uninstall followed by the package name.
Step 4: Record Requirements and Rebuild Anywhere
The requirements file is the bridge between machines. It is a plain text list of your project’s packages and versions. You generate it from a working environment with pip freeze, and you recreate the environment from it with pip install -r.
python -m pip freeze > requirements.txt cat requirements.txt
The freeze command prints the exact installed versions, and the redirect writes them into requirements.txt in the project folder. On Windows PowerShell the same redirect works, though checking the file afterwards with a text editor is wise because older PowerShell versions could save with a different encoding. Commit this file to version control alongside your code. It is the recipe for your environment.
# On a new machine, after creating and activating a fresh environment: python -m pip install -r requirements.txt
This single command reads the file and installs every listed package at the recorded versions. That is how a teammate, a deployment server, or your future self on a new laptop ends up with the same setup. Note the sequence: create a new environment first, activate it, then install. The environment folder itself is never copied across machines.
For larger projects, some developers keep the requirements file hand-edited with only the top-level packages they chose, letting pip resolve the exact dependency versions at install time. Both approaches are valid. Freeze gives exact reproducibility, while a hand-maintained list is easier to read and update deliberately. Many teams use freeze for the main file and review changes to it during code review, treating unexpected version jumps as something to check rather than accept blindly.
Step 5: Deactivate, and Work on Several Projects
Leaving an environment is a single command, and it works the same way on every platform.
deactivate
Running deactivate returns your terminal prompt to normal and points python and pip back at the system installation. You can reactivate the environment at any time by running the activation script again. Nothing is lost by deactivating; the folder and packages stay on disk.
Working on several projects in one day simply means activating the right environment in each terminal. Editors such as VS Code can select the environment interpreter for a workspace and activate it automatically in their integrated terminals, which removes most of the manual steps. The key mental rule is one project, one environment, one requirements file. When you start a quick experiment, give it its own folder and environment too; experiments have a habit of becoming real projects, and retrofitting isolation later is more work than starting with it, as anyone who has combined environment setup with code from the list comprehensions guide in a practice project will recognise.
Common Problems and Fixes
The Prompt Does Not Show the Environment Name
If the (.venv) marker is missing, activation did not run in that window. Terminals do not share activation state, so each new window needs its own activation command. Run it again and check sys.executable as shown earlier.
Package Installed but Import Fails
This almost always means the package was installed into a different interpreter than the one running the script. Confirm both sides use the environment: run python -m pip list to see whether the package is listed for the active interpreter, and reinstall with python -m pip if needed. Using the python -m pip form for installs consistently prevents most of these mismatches.
Wrong Python Version in the Environment
An environment inherits the version of the Python used to create it. If you need a different version, install that version on the machine, delete the old .venv folder, and recreate the environment by calling that specific interpreter, for example with a versioned command name where available. Then reinstall from your requirements file.
Should You Share the .venv Folder?
No. It contains paths specific to your machine and can be very large. Share the code and requirements.txt instead, and add .venv to your version control ignore file so it is never committed by accident. Anyone receiving the project can rebuild an identical environment in a couple of commands using the steps in this guide, which is the whole point of using a python virtual environment venv workflow in the first place. This rebuild approach is also what makes project setup instructions short and reliable in notes like the dictionary tips article, where readers are assumed to be working inside their own environment.
Conclusion
A virtual environment gives every Python project a clean, private set of packages, created with python -m venv, entered with a short activation command, and recorded in a requirements.txt file that anyone can rebuild from. Make it the first step of every new project and the version conflicts, mystery import errors, and broken system tools that come from shared installs largely disappear.
Tagged: pip requirements python projects python setup python snippets python venv python virtual environment venv



One thought on “Python Virtual Environments: venv Setup Step by Step”