pip
pip installs packages from PyPI (pypi.org):
pip install requestspip install requests==2.32.3— exact versionpip uninstall requestspip list,pip show requests
On some systems use python -m pip to be sure you install into the right Python.
Why virtual environments?
Different projects need different package versions. A virtual environment gives each project its own isolated set of packages.
venv
- Create:
python -m venv .venv - Activate:
- Windows:
.venv\Scripts\activate - macOS / Linux:
source .venv/bin/activate
- Windows:
- Your prompt shows
(.venv)— nowpip installgoes into this project only deactivateto leave
requirements.txt
- Save:
pip freeze > requirements.txt - Install on another computer:
pip install -r requirements.txt
Add .venv/ to .gitignore. Newer tools such as uv and Poetry manage environments and dependencies even faster.
Example
Example — Set up a project
Bash
mkdir weather-app && cd weather-app
# Create and activate a virtual environment
python -m venv .venv
.venv\Scripts\activate # Windows
# source .venv/bin/activate # macOS / Linux
(.venv) pip install requests python-dotenv
(.venv) pip list
# Save exact versions
(.venv) pip freeze > requirements.txt
# Later, on another machine:
pip install -r requirements.txt
(.venv) deactivate
Packages install only inside .venv for this project. requirements.txt lists them (e.g. requests==2.32.3) so anyone can recreate the same setup.