Python developers often use various tools to manage dependencies and virtual environments. Tools like Anaconda, venv, Pipenv, and Poetry simplify project isolation and dependency management. Each tool has advantages and challenges, with the choice depending on project needs and the developer's preferences.

1. venv (Python's built-in virtual environment tool)
Basic Commands
Create a new virtual environment (myenv):
python3 -m venv myenv
activate the virtual environment:
- Windows
myenv\Scripts\activate
- macOS/Linux
source myenv/bin/activate
Deactivate the environment
deactivate
Install a package (after activation):
pip install <package>
Freeze dependencies to a file:
pip freeze > requirements.txt
Install dependencies from requirements.txt
pip install -r requirements.txt
Tricks:
Isolated Development: Use
python -m venv
Fast environment deletion: If you ever need to delete the virtual environment, simply delete the folder (myenv).
2. conda (Anaconda package and environment manager)
Conda is a powerful environment and package manager, primarily used in data science and machine learning.
Basic Commands
Create a new environment:
conda create --name myenv python=3.9
This creates an environment named myenv with Python 3.9.
Activate the environment:
conda activate myenv
Deactivate the environment:
conda deactivate
Install a package:
conda install <package>
List all environments:
conda env list
Export environment to a file:
conda env export > environment.yml
Create environment from environment.yml
conda env create -f environment.yml
Tricks:
Mixing pip and conda: Conda allows you to install packages via pip within its environments. Install packages from by running:
pip install <package>
3. Pipenv
Pipenv integrates pip and virtualenv, providing an automatic virtual environment and dependency manager.
Basic Commands
Create a new virtual environment:
pipenv install
This creates a virtual environment for the current project and installs the dependencies.
Install a package:
pipenv install <package>
Install a dev dependency:
pipenv install <package> --dev
Activate the environment:
pipenv shell
Run a command in the environment without activating it:
pipenv run <command>
Lock dependencies:
pipenv lock
Install dependencies from Pipfile.lock
pipenv sync
Tricks:
Pre-existing requirements.txt: if you already have a requirements.txt file, you can convert it to a Pipfile using:
pipenv install -r requirements.txt
Dependency graph:
pipenv graph
4. Poetry
Poetry focuses on simplified dependency management and packaging, aiming to handle everything related to a Python project.
Basic Commands
Create a new Poetry project:
poetry new myproject
This command creates a new project structure with a pyproject.toml file.
Install dependencies in the current project:
poetry install
Add a package to the project:
poetry add <package>
Add a dev dependency:
poetry add <package> --group dev
Activate the virtual environment:
poetry shell
Run commands without activating the environment:
poetry run <command>
Lock dependencies
poetry lock
Publish the package
poetry publish
This command uploads the package to the PyPI repository.
Tricks:
Isolation by default: Poetry automatically manages virtual environments, so you don’t need to worry about manually creating or activating them.
Reproducible builds: By keeping all dependencies pinned in pyproject.toml and poetry.lock, you ensure that environments are consistent across installations.
Comparing the Tools
Tool | Pros | Cons |
---|---|---|
venv | Lightweight, built-in, easy to use | Requires manual dependency management |
conda | Ideal for data science, cross-platform | Larger install size, slower than others |
pipenv | Combines pip and virtualenv, ease of use | Can be slow with large dependency graphs |
Poetry | Simplified dependency management, packaging | Steeper learning curve for beginners |
In summary
- venv is ideal for small projects (where simplicity is key) and beginners.
- conda is best for data science and machine learning projects with cross-platform support and specialized packages.
- pipenv is useful for users who want pip and virtualenv combined, with security checks.
- Poetry is the go-to for modern Python projects, focusing on packaging and simplifying dependency management.