- Install packages using pip
- Create and use virtual environments
- Manage project dependencies
- Use requirements.txt files
Pip and Virtual Environments
Python's true power lies in its massive ecosystem of third-party packages. Want to work with data? There's pandas. Build a website? There's Django and Flask. Machine learning? scikit-learn and TensorFlow. Pip and virtual environments are your tools to access this treasure chest!
What is Pip?
Pip = Pip Installs Packages
It's Python's official package manager that:
- Downloads packages from PyPI (Python Package Index)
- Installs packages and their dependencies
- Manages package versions
# Install a package
pip install requests
# Install specific version
pip install requests==2.28.0
# Upgrade a package
pip install --upgrade requests
# Uninstall a package
pip uninstall requests
# See installed packages
pip list
# Show package info
pip show requests
Virtual Environments
Why Use Virtual Environments?
The Problem Without Virtual Envs
Project A needs: requests==2.25
Project B needs: requests==2.28
Without virtual environments:
Both projects share the same packages
Installing one version breaks the other project
With virtual environments:
Each project has its own isolated packages
No conflicts between projects
Creating and Using Virtual Environments
# Create virtual environment
python -m venv myenv
# Activate it (Mac/Linux)
source myenv/bin/activate
# Activate it (Windows)
myenv\Scripts\activate
# Your prompt changes:
(myenv) $
# Now pip installs to this environment only!
pip install requests
# Deactivate when done
deactivate
Requirements Files
Share your project's dependencies:
# Save current packages to file
pip freeze > requirements.txt
# File content looks like:
# requests==2.28.0
# numpy==1.23.0
# pandas==1.4.3
# Install from requirements file
pip install -r requirements.txt
Example requirements.txt
# Core dependencies
requests>=2.25.0
numpy>=1.20.0
# Development dependencies
pytest>=7.0.0
black>=22.0.0
Typical Workflow
# 1. Create project folder
mkdir my_project
cd my_project
# 2. Create virtual environment
python -m venv venv
# 3. Activate it
source venv/bin/activate # Mac/Linux
# 4. Install packages
pip install requests pandas
# 5. Save dependencies
pip freeze > requirements.txt
# 6. Work on your project...
# 7. When done
deactivate
Key Takeaways
Remember These Points
pip install package_name
Install packages from PyPI
python -m venv env_name
Create isolated environment for project
source env/bin/activate (Mac/Linux)
env\Scripts\activate (Windows)
pip freeze > requirements.txt
Save dependencies for sharing
Always use virtual environments for projects!
Beginner Level Complete!
Congratulations! You've completed the Python Beginner level!
You now know:
- Python basics, data types, and control flow
- Functions, data structures, and strings
- File handling and error management
- Modules, packages, and dependency management
You're ready for Intermediate Python where you'll learn object-oriented programming, advanced features, and real-world application development!
