Python Virtual Environments and pip
A virtual environment is a private folder of packages for one project, so different projects never fight over versions.
The Problem
Suppose Ali builds a shop website that needs version 2 of a package. Later he starts a school project that needs version 3. If both projects use the same Python on his laptop, upgrading for one project can break the other.
A virtual environment (often called a venv) solves this. Each project gets its own copy of Python's package folder. Packages installed inside it stay inside it.
| Without venv | With venv |
|---|---|
| All projects share one set of packages | Every project has its own packages |
| Upgrading one project can break another | Projects are independent |
| Hard to know what a project needs | requirements.txt lists it exactly |
| You may need admin rights to install | Installs go into the project folder |
Step 1: Create the Environment
Open a terminal in your project folder and run:
Example
cd shop-project
python -m venv .venvThis creates a folder called .venv with its own python and pip. The name .venv is a common choice, and most editors detect it automatically.
On macOS and Linux, type python3 instead of python if python is not found.
Step 2: Activate It
Activating tells your terminal to use the environment's Python and pip.
| System | Command |
|---|---|
| Windows (Command Prompt) | .venv\Scripts\activate |
| Windows (PowerShell) | .venv\Scripts\Activate.ps1 |
| macOS / Linux | source .venv/bin/activate |
After activation, the prompt shows the environment name in brackets:
(.venv) C:\Users\Ali\shop-project>
You can check which Python is now in use. On Linux it looks like this:
Example
which python
python --version/home/ali/shop-project/.venv/bin/python Python 3.13.16
On Windows use where python instead of which python.
Step 3: Install Packages
With the environment active, pip install puts packages only into .venv.
Example
pip install requests
pip listSuccessfully installed certifi-2026.7.22 charset_normalizer-3.5.2 idna-3.20 requests-2.34.2 urllib3-2.8.0 Package Version ------------------ --------- certifi 2026.7.22 charset-normalizer 3.5.2 idna 3.20 pip 26.2.1 requests 2.34.2 urllib3 2.8.0
requests needed four other packages (its dependencies), and pip installed them automatically.
More pip Commands
| Command | What it does |
|---|---|
pip install requests==2.34.2 | Installs an exact version |
pip install "requests>=2.30" | Installs a version 2.30 or newer |
pip install --upgrade requests | Updates to the newest version |
pip uninstall requests | Removes a package |
pip show requests | Shows version, summary and dependencies |
pip list --outdated | Lists packages that have newer versions |
python -m pip install ... | Runs pip for this exact Python (safest) |
Step 4: Save Your Requirements
pip freeze prints every installed package with its exact version. Save it in a file so others can build the same environment.
Example
pip freeze > requirements.txtThe file requirements.txt now contains:
certifi==2026.7.22 charset-normalizer==3.5.2 idna==3.20 requests==2.34.2 urllib3==2.8.0
When your friend Sara downloads the project, she creates her own venv and installs everything in one line:
Example
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtStep 5: Deactivate
When you finish working, leave the environment:
Example
deactivateThe brackets disappear from the prompt, and python points to the main system Python again.
Do Not Share the .venv Folder
The .venv folder can be hundreds of megabytes and only works on the computer that made it. Never email it or upload it to GitHub. Share requirements.txt instead. Add this line to your .gitignore file:
.venv/
A Typical Project Layout
shop-project/
.venv/ (not shared)
.gitignore
requirements.txt
main.py
shop.py
test_shop.pyA Faster Modern Tool: uv
In 2026 many teams use uv, a very fast tool that creates the environment and manages packages for you. It keeps the list of packages in a file called pyproject.toml.
Example
pip install uv
uv init shop-project
cd shop-project
uv add requests
uv run main.pyuv add creates .venv if needed and installs the package in a moment:
Prepared 6 packages in 70ms Installed 6 packages in 2ms + certifi==2026.7.22 + charset-normalizer==3.5.2 + idna==3.20 + requests==2.34.2 + urllib3==2.8.0
uv run runs your file inside the project's environment, so you do not even need to activate it. Learn plain venv and pip first, because every Python setup understands them, then try uv on your next project.
Make it a habit: one project, one virtual environment. Create it before you install the first package.
Exercise
Exercise
Which command saves the exact list of installed packages to a file?
