Python Basics — Virtual Environments & pip
Virtual Environments & pip
Different projects need different packages — and sometimes different versions of the same package. Virtual environments solve this by giving each project its own isolated Python installation.
Why Virtual Environments Matter
Without virtual environments:
Project A needs requests 2.28
Project B needs requests 2.31
→ They conflict — one will break!
With virtual environments:
Project A → venv_a/ → requests 2.28 ✅
Project B → venv_b/ → requests 2.31 ✅
→ Each project has its own packages, no conflicts
Creating a Virtual Environment
# Navigate to your project directory
cd my-project
# Create a virtual environment
python -m venv venv
# This creates:
# venv/
# ├── bin/ (or Scripts/ on Windows)
# ├── include/
# ├── lib/
# └── pyvenv.cfg
Activating / Deactivating
# macOS / Linux
source venv/bin/activate
# Windows (Command Prompt)
venv\Scripts\activate
# Windows (PowerShell)
venv\Scripts\Activate.ps1
When activated, your prompt changes:
(venv) $ python --version
Python 3.12.0
(venv) $ which python
/Users/you/my-project/venv/bin/python
To deactivate:
(venv) $ deactivate
$ # back to normal prompt
pip — Installing Packages
pip is Python’s package installer. It downloads packages from PyPI (Python Package Index).
Install a Package
(venv) $ pip install requests
# Downloading requests-2.31.0...
# Successfully installed requests-2.31.0
(venv) $ pip install flask==2.3.0 # specific version
(venv) $ pip install "django>=4.0" # minimum version
Use the Package
import requests
response = requests.get("https://api.github.com")
print(response.status_code) # 200
print(response.json()["current_user_url"])
Uninstall a Package
(venv) $ pip uninstall requests
List Installed Packages
(venv) $ pip list
Package Version
---------- -------
pip 23.0
requests 2.31.0
certifi 2023.7.22
...
(venv) $ pip show requests
Name: requests
Version: 2.31.0
Summary: Python HTTP for Humans.
Location: /Users/you/venv/lib/python3.12/site-packages
Requires: certifi, charset-normalizer, idna, urllib3
pip freeze and requirements.txt
Saving Dependencies
# Save all installed packages and versions
(venv) $ pip freeze > requirements.txt
# requirements.txt
certifi==2023.7.22
charset-normalizer==3.3.0
idna==3.4
requests==2.31.0
urllib3==2.0.7
Installing from requirements.txt
When someone else clones your project:
# Create and activate a new virtualenv
python -m venv venv
source venv/bin/activate
# Install all dependencies
pip install -r requirements.txt
Common pip Commands
| Command | Purpose |
|---|---|
pip install package |
Install a package |
pip install package==1.0 |
Install specific version |
pip install -r requirements.txt |
Install from requirements file |
pip uninstall package |
Remove a package |
pip list |
List installed packages |
pip show package |
Show package details |
pip freeze |
Output installed packages in requirements format |
pip install --upgrade package |
Upgrade a package |
pip install --upgrade pip |
Upgrade pip itself |
pip vs pipenv vs poetry
| Tool | What It Does | Best For |
|---|---|---|
| pip + venv | Basic package management | Learning, simple projects |
| pipenv | pip + venv combined, with Pipfile | Medium projects |
| poetry | Full dependency management + packaging | Professional projects |
| conda | Package + environment manager | Data science |
pipenv Quick Look
pip install pipenv
cd my-project
pipenv install requests # creates Pipfile + virtualenv
pipenv shell # activate
pipenv install --dev pytest # dev dependency
poetry Quick Look
pip install poetry
poetry new my-project # scaffold a project
cd my-project
poetry add requests # install + add to pyproject.toml
poetry install # install all dependencies
poetry shell # activate
Best Practices
- Always use a virtual environment — never install packages globally
- Keep
requirements.txtupdated — runpip freeze > requirements.txtafter installing packages - Add
venv/to.gitignore— don’t commit the virtual environment - Pin versions in production —
requests==2.31.0, not justrequests - Separate dev dependencies — use
requirements-dev.txtfor testing tools
.gitignore Entry
# Virtual environment
venv/
.venv/
env/
# Python
__pycache__/
*.pyc
*.pyo
Summary
- Virtual environments isolate project dependencies — one venv per project
- Create with
python -m venv venv, activate withsource venv/bin/activate - pip installs packages from PyPI:
pip install package - Freeze dependencies:
pip freeze > requirements.txt - Restore dependencies:
pip install -r requirements.txt - Never install packages globally — always use a virtual environment
- For professional projects, consider poetry or pipenv