Python Virtual Environments and Package Management Tools: Creation, Usage, and Comparison
This article explains why Python virtual environments are needed, how to create and manage them with venv, and compares the main package management tools pip, pipenv, and poetry, offering guidance on selecting the appropriate tool for different project scenarios.
In Python development, virtual environments and package managers are essential for isolating project dependencies, avoiding version conflicts, and ensuring portability.
1. Why need virtual environments?
Third‑party libraries such as numpy , requests , Django are usually installed globally, which can cause conflicts when different projects require different versions (e.g., Project A needs Django 3.2, Project B needs Django 4.0).
Advantages of virtual environments
Isolation of dependencies, no pollution of the global environment, and easier collaboration through shared requirements.txt or pyproject.toml .
2. Creating and managing Python virtual environments
(1) Using venv to create a virtual environment
# Create a virtual environment (creates a folder named venv)
python -m venv venv
# Activate on Linux/macOS
source venv/bin/activate
# Activate on Windows
venv\Scripts\activateAfter activation the prompt shows (venv) .
(2) Deactivating a virtual environment
deactivate(3) Deleting a virtual environment
Simply remove the venv folder:
rm -rf venv # Linux/macOS
del /s /q venv # Windows3. Python package management tools
(1) pip – the default package manager
Common commands:
# Install a package
pip install package_name
# Install a specific version
pip install package_name==1.0.0
# Upgrade a package
pip install --upgrade package_name
# Uninstall a package
pip uninstall package_name
# List installed packages
pip list
# Export dependencies
pip freeze > requirements.txt
# Install from requirements.txt
pip install -r requirements.txt(2) pipenv – higher‑level tool
Install with pip install pipenv . Basic usage includes creating a virtual environment and installing dependencies ( pipenv install package_name ), running scripts ( pipenv run python script.py ), locking dependencies, and uninstalling packages.
(3) poetry – modern project management
Install with pip install poetry . Typical commands: poetry new project_name , poetry add package_name , poetry install , export dependencies, and run scripts with poetry run python script.py .
4. Choosing a package management tool
Tool
Suitable Scenarios
Advantages
Disadvantages
pip + venvSimple projects, beginners
Built‑in, no extra installation
Manual dependency management
pipenvMedium‑size projects
Automatic virtual‑env handling, lock files
Slower performance
poetryLarge projects, team collaboration
Powerful dependency management, publishing support
Steeper learning curve
Recommendations: beginners start with pip + venv , then try pipenv or poetry ; teams should adopt poetry for better dependency governance.
5. Summary
Virtual environments are essential for Python development to avoid dependency conflicts. pip is the basic package manager for simple projects, while pipenv and poetry provide more advanced management for complex or collaborative projects.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.