PIP Package Manager
PIP (Pip Installs Packages) is Python's package manager - your gateway to over 400,000 packages on PyPI (Python Package Index). From web frameworks like Flask and Django to data science libraries like NumPy and Pandas, pip lets you easily install, upgrade, and manage third-party packages. Understanding pip and virtual environments is essential for any Python developer working on real-world projects!
Essential PIP Commands
PIP commands run in your terminal (Command Prompt, PowerShell, or Terminal), not in Python.
The most common commands are pip install to add packages, pip uninstall
to remove them, and pip list to see what's installed. You can also specify exact
versions for reproducible builds.
Click Run to execute your code
pip3 for
Python 3 packages. On newer systems with only Python 3, pip and pip3
are usually the same. To be safe, use python -m pip install package which always
uses the pip for the current Python interpreter.
Managing Dependencies with requirements.txt
The requirements.txt file lists all packages your project needs. It's the
standard way to share dependencies - anyone can recreate your environment by running
pip install -r requirements.txt. Use pip freeze to generate this
file from your current environment.
Click Run to execute your code
==1.2.3 - Exact version (most reproducible)>=1.2.0 - Minimum version (allows updates)~=1.2.0 - Compatible release (>=1.2.0, <1.3.0)>=1.2,<2.0 - Version range (flexible but bounded)
Virtual Environments
Virtual environments solve a critical problem: different projects needing different package versions. Each virtual environment is an isolated Python installation with its own packages. This is a must-know skill for professional Python development - always use virtual environments for your projects!
Click Run to execute your code
venv/ or .venv/ to your
.gitignore file. Virtual environments are large, machine-specific, and can be
recreated from requirements.txt. Only commit the requirements.txt file, not the entire
virtual environment folder.
Using Installed Packages
Once installed, packages are imported just like standard library modules. Python's ecosystem includes packages for nearly everything: web development, data science, machine learning, API clients, automation, and more. Learning to find and use the right packages is a superpower!
Click Run to execute your code
Common Mistakes
1. Installing globally without virtual environment
# Wrong - installs globally, can cause conflicts
pip install flask
# Better - always use virtual environment
python -m venv venv
source venv/bin/activate # or venv\Scripts\activate on Windows
pip install flask
# Now flask is isolated to this project
2. Not pinning versions in production
# requirements.txt - Bad for production
requests
flask
numpy
# requirements.txt - Good for production
requests==2.28.0
flask==2.2.2
numpy==1.24.0
# Generate pinned requirements:
pip freeze > requirements.txt
3. Forgetting to activate virtual environment
# Wrong - installs to wrong Python!
python -m venv venv
pip install requests # Still using global pip!
# Correct - activate first!
python -m venv venv
source venv/bin/activate # IMPORTANT!
pip install requests # Now goes to venv
# Check which pip you're using:
which pip # Should show path inside venv
4. Committing virtual environment to git
# .gitignore - Add these lines!
venv/
.venv/
env/
.env/
__pycache__/
*.pyc
# Only commit requirements.txt
# Others can recreate the environment with:
pip install -r requirements.txt
5. Using pip install in Python code
# Wrong - don't install packages from Python
import pip
pip.main(['install', 'requests']) # Bad practice!
# Correct - install from terminal
# $ pip install requests
# If you must install programmatically:
import subprocess
import sys
subprocess.check_call([sys.executable, "-m", "pip", "install", "requests"])
Exercise: Project Setup
Task: Write the commands for a proper project setup workflow.
Requirements:
- Create a virtual environment named 'venv'
- Activate the virtual environment
- Install requests and flask packages
- Save dependencies to requirements.txt
Click Run to execute your code
Show Solution
# Project setup commands (run in terminal, not Python)
print("=== Project Setup Commands ===")
print()
print("# 1. Create virtual environment")
print("python -m venv venv")
print()
print("# 2. Activate virtual environment")
print("# Windows:")
print("venv\\Scripts\\activate")
print("# macOS/Linux:")
print("source venv/bin/activate")
print()
print("# 3. Install packages")
print("pip install requests flask")
print()
print("# 4. Save dependencies")
print("pip freeze > requirements.txt")
print()
print("# Bonus: Verify installation")
print("pip list")
print("python -c \"import requests, flask; print('Success!')\"")
print()
print("# When done working:")
print("deactivate")
Summary
- Install:
pip install package_name - Specific version:
pip install package==1.2.3 - Upgrade:
pip install --upgrade package - Uninstall:
pip uninstall package - List packages:
pip listorpip freeze - requirements.txt:
pip install -r requirements.txt - Create venv:
python -m venv venv - Activate:
source venv/bin/activate(Linux/Mac) orvenv\Scripts\activate(Windows) - Deactivate:
deactivate - Best practice: Always use virtual environments for projects
What's Next?
Congratulations on completing the Modules & Packages module! You now know how to work with Python's standard library modules and install third-party packages. Next, we'll learn about File Handling - reading from and writing to files, which is essential for data processing, configuration, and persistence!
Enjoying these tutorials?