DEV Community

Cover image for Poetry > pip + venv? Here's why developers are switching
Leapcell
Leapcell

Posted on

Poetry > pip + venv? Here's why developers are switching

Image description

Leapcell: The Best of Serverless Web Hosting

Comprehensive Beginner's Guide to Python Package Manager Poetry: A Full - Scale Analysis from Basics to Practical Application

1. Introduction: Why Do We Need Poetry?

In the field of Python development, package management has always been a core issue in project maintenance. Although the traditional tool pip can meet basic installation needs, as the scale of projects expands, its shortcomings in dependency resolution and virtual environment management gradually become apparent. For example, when manually removing packages, it cannot automatically handle dependency relationships, which may lead to environmental chaos or dependency conflicts. As a new generation of package management tools, Poetry not only integrates virtual environment management and dependency resolution functions but also provides a clearer project configuration and release process, making it one of the current preferred solutions for Python developers.

This article will focus on the core functions of Poetry and combine specific operation examples to help readers master this powerful tool from entry to proficiency. The content covers installation and configuration, virtual environment management, dependency operations, common commands, and practical application scenarios, suitable for beginners and developers who hope to optimize project management processes.

2. Analysis of Core Concepts of Poetry

2.1 What is Poetry?

Poetry is an integrated Python package management tool, and its official positioning is "Dependency Management for Python". It not only has the installation and deletion functions of traditional package managers (such as pip), but also provides additional features:

  • Automatic virtual environment management: It has built - in virtualenv functions, so there is no need to install virtual environment tools separately.
  • Dependency relationship analysis: It can automatically handle version conflicts between packages to ensure the stability of the project environment.
  • Project configuration and release: It can uniformly manage project metadata and dependencies through pyproject.toml and support package packaging and release.

2.2 Explanation of Key Terms

  • Virtual environment management: By isolating the dependency environments of different projects, it avoids version pollution caused by global installation. Poetry defaults to automatically creating virtual environments, and the path can be configured.
  • Dependency management (Dependency Management): It not only manages directly installed packages but also tracks their underlying dependencies to ensure compatibility of all components.
  • Dependency resolution (Dependency Resolution): This is the core advantage of Poetry. It can automatically calculate the optimal dependency combination that meets version rules and solve complex conflicts that pip cannot handle.

2.3 Why Abandon pip?

The main defect of pip lies in its lack of dependency resolution ability. For example:

  • When removing packages, it cannot determine whether they are depended on by other components, which may lead to environmental collapse.
  • Manually maintaining requirements.txt is error - prone, especially when dealing with version ranges, it lacks automated support.
  • Virtual environments need to be managed separately using venv, and the process is cumbersome.

Application scenarios: pip is suitable for simple scripts or temporary tasks, while Poetry is more suitable for medium and large projects, team collaboration, or scenarios requiring strict dependency management.

3. Installation and Basic Configuration of Poetry

3.1 Selection of Installation Methods

Poetry provides two officially recommended installation methods, both of which are deployed based on independent virtual environments to ensure isolation from the project environment:

Method 1: Use the Official Installation Script (Recommended)

Applicable systems: macOS/Linux/WSL/Windows

# macOS/Linux/WSL
curl -sSL https://install.python-poetry.org | python3 -

# Windows (PowerShell)
(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | python -
Enter fullscreen mode Exit fullscreen mode

After installation, Poetry will be placed in a system - specific directory (such as ~/.local/bin on macOS), and you need to manually configure the environment variable PATH to directly call the poetry command.

Method 2: Install via pipx

Applicable systems: pipx needs to be installed in advance

pipx install poetry
Enter fullscreen mode Exit fullscreen mode

pipx will automatically create an isolated environment to install Poetry, which is suitable for users who are used to using this tool.

3.2 Configuration of Environment Variables

After installation, you need to add the Poetry executable file path to the system PATH:

macOS/Linux

Edit the shell configuration file (such as .zshrc or .bashrc) and add:

export PATH="$PATH:$HOME/.local/bin"
Enter fullscreen mode Exit fullscreen mode

Save and restart the terminal, then enter poetry --version to verify the installation.

Windows

Go to "System Properties → Advanced → Environment Variables", edit PATH in the user variables, and add the installation path (such as %APPDATA%\Python\Scripts).

3.3 Setting alias (Optional)

To simplify command input, you can add an alias in the shell configuration:

alias p='poetry'  # Simplify poetry to p
Enter fullscreen mode Exit fullscreen mode

4. From Scratch: Initializing Projects and Virtual Environments

4.1 Initializing Projects

Create a new project directory and initialize Poetry:

mkdir poetry-demo && cd poetry-demo
poetry init
Enter fullscreen mode Exit fullscreen mode

After execution, the system will guide users to enter project metadata (such as name, version, author, etc.), and most options can use default values by pressing Enter. Key steps:

  • When asked whether to define dependencies interactively, select no and install them manually later.
  • Confirm the content of the generated pyproject.toml and finally generate the project configuration file.

At this time, the project structure is:

poetry-demo/
└── pyproject.toml
Enter fullscreen mode Exit fullscreen mode

4.2 Virtual Environment Management

4.2.1 Default Behavior and Problems

Poetry defaults to storing virtual environments in the system cache directory (such as ~/Library/Caches/pypoetry/virtualenvs on macOS), and the naming rule is project name - random string - Python version. Although this method achieves isolation, it is not conducive to project migration and intuitive management.

4.2.2 Customizing the Virtual Environment Path

It is recommended to store virtual environments in the project directory to ensure a one - to - one binding with the project:

poetry config virtualenvs.in-project true  # Set to the inside of the project
poetry env remove python  # Remove the old environment (if any)
poetry env use python  # Recreate the environment, and the path is .venv under the project root directory
Enter fullscreen mode Exit fullscreen mode

At this time, the virtual environment path is ./.venv, and the startup method is:

poetry shell  # Enter the virtual environment
exit  # Exit
Enter fullscreen mode Exit fullscreen mode

5. Core Operations of Dependency Management

5.1 Installing Packages

5.1.1 Installing Main Dependencies (Production Environment)

poetry add flask  # Install Flask and automatically handle dependencies
Enter fullscreen mode Exit fullscreen mode

After installation, pyproject.toml will add Flask = "^2.1.1", and at the same time, poetry.lock will be generated to record all underlying dependencies (such as click, jinja2, etc.).

5.1.2 Installing Development Dependencies

Development tools (such as test frameworks and code formatting tools) should be installed in the dev - dependencies block to avoid polluting the production environment:

poetry add black --dev  # Old version command (Poetry <2.0)
poetry add black -G dev  # New version command (Poetry >=2.0, recommended)
Enter fullscreen mode Exit fullscreen mode

At this time, pyproject.toml will add:

[tool.poetry.dev-dependencies]
black = "^22.3.0"
Enter fullscreen mode Exit fullscreen mode

5.1.3 Specifying Version Ranges

Poetry supports flexible version rules to avoid maintenance costs caused by fixed versions:

  • ^ symbol: Allows patch and minor version updates (for example, ^4.2.9 means >=4.2.9 and <5.0.0)
  poetry add django@^4.2.9
Enter fullscreen mode Exit fullscreen mode
  • ~ symbol: Only allows patch version updates (for example, ~4.2.9 means >=4.2.9 and <4.3.0)
  poetry add django@~4.2.9
Enter fullscreen mode Exit fullscreen mode
  • Fixed version: Strictly lock the version (such as ==4.2.9)
  poetry add django==4.2.9
Enter fullscreen mode Exit fullscreen mode

5.2 Updating and Removing Packages

5.2.1 Updating Packages

poetry update  # Update all upgradable packages
poetry update flask  # Only update Flask
Enter fullscreen mode Exit fullscreen mode

The update logic is based on the version rules in pyproject.toml, and it automatically resolves dependency conflicts.

5.2.2 Removing Packages

poetry remove flask  # Remove Flask and unused underlying packages
Enter fullscreen mode Exit fullscreen mode

Key advantage: Poetry will check the dependency relationship and only remove components that are indeed no longer used. For example, if black depends on click, click will be retained when flask is removed.

5.3 Viewing the Dependency List

5.3.1 Listing All Packages

poetry show  # Display all installed packages (including dependencies)
Enter fullscreen mode Exit fullscreen mode

5.3.2 Displaying Dependency Hierarchy in Tree Structure

poetry show --tree  # Display dependency relationships in a tree diagram
Enter fullscreen mode Exit fullscreen mode

Example output:

flask 2.1.1 A simple framework for building complex web applications.
├── click >=8.0
│   └── colorama *
├── importlib-metadata >=3.6.0
│   └── zipp >=0.5
...
Enter fullscreen mode Exit fullscreen mode

6. Advanced Operations and Practical Commands

6.1 Generating requirements.txt (Compatible with Old Systems)

Although Poetry recommends using poetry.lock, requirements.txt is still needed in some scenarios (such as Docker deployment):

poetry export -f requirements.txt -o requirements.txt --without-hashes
Enter fullscreen mode Exit fullscreen mode

Parameter description:

  • -f: Specify the output format as requirements.txt
  • -o: Specify the output file name
  • --without-hashes: Remove hash values (non - mandatory, decided according to needs)

6.2 List of Common Commands

Command Description
poetry init Initialize the project and generate pyproject.toml
poetry add Install packages to production dependencies
poetry add -G dev Install packages to development dependencies
poetry install Install all dependencies according to poetry.lock
poetry update Update packages
poetry show --tree Display dependency relationships in a tree structure
poetry shell Start the virtual environment
poetry export Output requirements.txt
poetry lock Lock dependency versions and update poetry.lock

7. Common Usage Scenarios and Solutions

7.1 New Project Setup Process

  1. Create a directory and initialize: mkdir my-project && cd my-project && poetry init
  2. Configure the virtual environment inside the project: poetry config virtualenvs.in-project true
  3. Start the environment and install dependencies: poetry shell && poetry add flask

7.2 Migrating Existing Projects to Poetry

  1. Back up the original requirements.txt
  2. Initialize Poetry: poetry init --no-interaction (quickly generate basic configuration)
  3. Install dependencies one by one (recommended): poetry add package-name
    • Or batch migration (high risk): cat requirements.txt | xargs poetry add
  4. Delete the old requirements.txt and use poetry.lock to manage dependencies

7.3 Recreating the Environment Across Hosts

  1. Copy the project code: git clone repo-url && cd project-dir
  2. Install dependencies: poetry install (automatically create the environment according to poetry.lock)

7.4 Rebuilding the Virtual Environment

Directly delete the .venv directory in the project, and then execute:

poetry shell  # Automatically rebuild the environment and start it
Enter fullscreen mode Exit fullscreen mode

7.5 Best Practices for Docker Deployment

Avoid installing Poetry in the Docker image and use multi - stage construction (Multi - stage Build):

  1. First stage: Use Poetry to build dependencies
   FROM python:3.8-slim AS build
   RUN pip install poetry
   WORKDIR /app
   COPY pyproject.toml poetry.lock ./
   RUN poetry export -f requirements.txt --without-hashes -o requirements.txt
Enter fullscreen mode Exit fullscreen mode
  1. Second stage: Only install production dependencies
   FROM python:3.8-slim
   COPY --from=build /app/requirements.txt ./
   RUN pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

8. Summary: The Advantages and Learning Curve of Poetry

8.1 Core Advantages

  • Powerful dependency resolution: It can automatically handle complex version conflicts and ensure environmental stability.
  • Integrated virtual environment: No additional tools are needed, and it can be created and managed with one click.
  • Unified project configuration: It manages all metadata through pyproject.toml, which is easy for team collaboration.
  • Simplified release process: It has built - in packaging functions and supports direct release to PyPI.

8.2 Learning Suggestions

  • Start with small projects: First practice basic commands (such as add, shell, show) in simple projects.
  • Understand configuration files: Deeply study the role differences between pyproject.toml and poetry.lock.
  • Refer to official documents: Poetry documents are detailed, especially pay attention to version rules and command parameter descriptions.

8.3 Recommended Application Scenarios

  • Medium and large team projects that require strict dependency management.
  • Projects with frequent dependency updates or complex dependency relationships.
  • Projects that need cross - environment deployment (such as Docker, Kubernetes).

By mastering the core functions of Poetry, developers can significantly improve project management efficiency, reduce the risk of dependency conflicts, and simplify the deployment process. Although there is a learning cost at the beginning, in the long run, the stability and productivity improvement it brings are definitely worth trying. Start using Poetry immediately and experience the convenience of modern Python development!

Leapcell: The Best of Serverless Web Hosting

Finally, recommend a platform that is most suitable for deploying Python services: Leapcell

Image description

🚀 Build with Your Favorite Language

Develop effortlessly in JavaScript, Python, Go, or Rust.

🌍 Deploy Unlimited Projects for Free

Only pay for what you use—no requests, no charges.

⚡ Pay-as-You-Go, No Hidden Costs

No idle fees, just seamless scalability.

Image description

📖 Explore Our Documentation

🔹 Follow us on Twitter: @LeapcellHQ

Top comments (1)

Collapse
 
cwrite profile image
Christopher Wright

Great guide! Poetry definitely feels like pip and venv had a glow-up and now go to yoga together. Still, isn't dealing with pyproject.toml just moving the chaos to a new folder? Asking for my six broken virtualenvs.