Introduction to Package Management
Package management is essential for maintaining a clean and efficient Python development environment. Various tools help developers install, update, and manage packages effectively.
1. pip (Primary Package Installer)
Basic Operations
## Install a package
pip install package_name
## Uninstall a package
pip uninstall package_name
## Upgrade a package
pip install --upgrade package_name
venv (Standard Library)
## Create virtual environment
python3 -m venv myenv
## Activate virtual environment
source myenv/bin/activate
## Deactivate
deactivate
Tool |
Pros |
Cons |
Use Case |
venv |
Built-in, Lightweight |
Limited Features |
Simple Projects |
virtualenv |
More Flexible |
External Dependency |
Complex Environments |
conda |
Cross-platform |
Heavy |
Data Science |
3. Advanced Package Management
Poetry
## Install Poetry
curl -sSL https://install.python-poetry.org | python3 -
## Create new project
poetry new myproject
## Add dependency
poetry add requests
4. Dependency Management
graph TD
A[Dependency Management] --> B[requirements.txt]
A --> C[pyproject.toml]
A --> D[setup.py]
Generating Requirements
## Export current environment packages
pip freeze > requirements.txt
## Install from requirements
pip install -r requirements.txt
5. Package Repositories
Alternative Package Sources
## Install from specific index
pip install --index-url https://pypi.org/simple package_name
Best Practices at LabEx
- Use virtual environments
- Maintain clear dependency tracking
- Regularly update packages
- Use reproducible environment configurations
Security Considerations
Package Verification
## Check package hash
pip install --no-cache-dir package_name
Troubleshooting Package Management
graph TD
A[Package Management Issue] --> B{Dependency Conflict?}
B -->|Yes| C[Use Virtual Environment]
B -->|No| D{Network Issue?}
D -->|Yes| E[Check Proxy/Firewall]
D -->|No| F[Reinstall pip]
Emerging Trends
- Increased use of dependency resolvers
- Focus on reproducible environments
- Enhanced security scanning
- Simplified package management workflows