SoFunction
Updated on 2025-04-21

The use of Poetry, a powerful tool for Python project management

In Python project development, package management, dependency management and the creation of virtual environments have always been difficult problems that developers often face. Traditionally, developers usually usepipvirtualenvorcondaTo deal with these issues. However, as the complexity of Python projects increases, traditional tools often seem to be incompetent. At this time, Poetry came into being and became a more modern, concise and efficient tool.

This article will start from scratch and lead you to quickly get started with Poetry, introducing how to use Poetry to manage Python projects, including installation, configuration, dependency management, virtual environments, and packaging and releases.

What is Poetry?

Poetry is a Python project management tool that integrates dependency management, virtual environment management, packaging and publishing functions. Poetry is designed to simplify the workflow of developers, allowing you to easily manage project dependencies, ensure consistency in your environment, and quickly create and publish Python packages.

The key advantages of Poetry are:

  • Automatically create and manage virtual environments.
  • Accurate dependency resolution to avoid version conflicts.
  • useFiles simplify configuration and management.
  • Supports integrated functions such as packaging, publishing, and installation.

Installation of Poetry

First, you need to install Poetry on your system. You can use the officially recommended installation commands to install:

1. Install Poetry

Open the terminal (command line) and run the following command to install Poetry:

curl -sSL  | python3 -

Alternatively, if you are a Windows user, you can use the following PowerShell command:

(Invoke-WebRequest -Uri  -UseBasicP) | python -

After the installation is completed, you can confirm whether the installation is successful through the following command:

poetry --version

If the installation is successful, the version number of Poetry will be output.

Create a new project

Poetry provides a very concise command to initialize a new Python project, which automatically creates the necessary file structures and configurations.

2. Create a new project

Run the following command in the terminal to create a new Python project:

poetry new my_project

This will create a name calledmy_projectfolder and automatically generate a basic Python project structure. You will see the following files and folders:

my_project/
├── my_project/
│   └── __init__.py
├── 
└── tests/
    └── __init__.py
  • : This is the core configuration file of the Poetry project. All metadata about the project (such as dependencies, versions, etc.) will be written in this file.
  • my_project/__init__.py: The Python source code file for the project.
  • tests/: A folder containing the test code. Poetry creates a simple test folder for you by default.

3. Enter the project directory

cd my_project

Manage project dependencies

Poetry provides powerful dependency management capabilities, allowing you to precisely control the package versions required by the project and automatically handle dependent version conflicts. With a few simple commands, you can easily install, update and manage project dependencies.

4. Initialize project dependencies

If you already have an existing project and want to use Poetry to manage its dependencies, you can initialize the project and create it by using the following commanddocument:

poetry init -n

-nParameters indicate that interactive prompts are automatically skipped and files are created directly. This will generate aFile and provide you with a basic dependency structure.

5. Add dependencies

When you need to add a new dependency to your project, you can usepoetry addOrder. For example, if you want to addrequestsLibrary, you can run the following commands:

poetry add requests

This willrequestsLibrary added to, and Poetry will automatically resolve the dependent version according to the needs of the project.

If you need to add a specific version of the dependency, you can use a command like the following:

poetry add [email protected]

6. Install all dependencies

If you cloned a project from elsewhere and need to install all dependencies, you can usepoetry installOrder. It will be based onandFile installation all project dependencies:

poetry install

This command automatically creates and activates the virtual environment, ensuring that you install dependencies in an isolated environment, thus avoiding conflicts in global packages.

7. Installation and development dependencies

Some dependencies are only needed during development, such as testing frameworks. Poetry allows you to use--devParameters to install development dependencies:

poetry add --dev pytest

at this time,pytestIt will only be installed in the development environment and will not affect the dependencies of the production environment.

8. Use other Python versions

Poetry allows you to specify the use of a specific Python version. If you have installed multiple Python versions, you can usepoetry env useThe command specifies the path to the Python interpreter. For example:

poetry env use "path/to/your/"

This command forces Poetry to use the Python interpreter you specified to create a virtual environment.

9. Use

If your project is originally usedTo manage dependencies, and if you want to migrate to Poetry, you can use the following command to installAll dependencies in  :

poetry run pip install -r 

This will be used in Poetry's virtual environmentpipInstallAll dependencies listed in   and make sure they are compatible with other dependencies of the project.

10. Update dependencies

As the project develops, new versions may be released. You can usepoetry updateCommand to update all dependencies to the latest compatible version:

poetry update

This will be based on yourVersion requirements in the file, update all installed dependencies.

11. View project dependencies

If you want to view all dependencies of the current project, you can use:

poetry show

This lists all direct and indirect dependencies and provides versions and other details for each package.

12. Delete dependencies

If a dependency is no longer needed, you can usepoetry removeCommand to delete it:

poetry remove requests

This willrequestsfromFiles andRemove the file and uninstall it.

Through these commands, Poetry makes dependency management more efficient and convenient. Whether adding, updating, installing development dependencies, or using a specified Python version, Poetry provides flexible tools to help you manage project dependencies and virtual environments.

Virtual environment management

Poetry automatically creates and manages virtual environments for each project, which ensures that your project always runs in an isolated environment, avoiding the possible conflicts of global installation packages.

13. View the virtual environment

If you want to view the virtual environment created by Poetry, you can run the following command:

poetry env list

It lists the virtual environments used by the current project. You can also passpoetry env useTo switch to the Python version.

14. Activate the virtual environment

You can activate the virtual environment by:

poetry shell

This activates the virtual environment and goes into a new shell where you can run Python commands, test code, etc.

Run the project

Poetry allows you to run commands directly in a virtual environment without having to manually activate the virtual environment first.

15. Run commands using Poetry

If you want to run Python scripts directly, you can use the following command:

poetry run python my_project/

poetry runWill ensure that the command runs in a virtual environment managed by Poetry.

Packaging and publishing

When your project is developed, Poetry can also help you package and publish your project to PyPI or other Python package management platforms.

16. Packaging the project

First, use the following command to build the release of the project:

poetry build

This will generate.and.whlFile package, stored indist/In the folder.

17. Publish to PyPI

If you are going to publish your project to PyPI, you can use the following command:

poetry publish --repository

 pypi

Of course, before release, you need to configure PyPI authentication information, which can be passedpoetry configCommand to configure.

Summary of commonly used Poetry commands

Order Function
poetry new <project_name> Create a new Python project
poetry add <package> Add dependency package to the project
poetry install Install all dependencies of the project
poetry update Update project dependencies to the latest version
poetry shell Start the shell of the project virtual environment
poetry run <command> Run the specified command in a virtual environment
poetry build Packaging Projects
poetry publish Publish a project to PyPI
poetry env list View the virtual environment

Summarize

Poetry is a powerful Python project management tool that integrates dependency management, virtual environment, packaging and publishing functions. With a simple command-line interface and easy-to-understand configuration files, it helps developers manage projects more efficiently and avoids common troubles such as version conflicts and dependency problems.

Whether you are a Python newbie or a veteran developer, Poetry is a tool worth trying. Through this article, you have mastered how to manage your Python projects using Poetry from scratch, and it becomes easier to manage dependencies, virtual environments, package and publish.

This is the end of this article about the use of Poetry, a powerful tool for Python project management. For more related Python Poetry content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!