Using Poetry to manage Python projects
Introduction to Poetry
In this video, Serdar Yegulalp introduces Poetry, an all-in-one system for managing projects in Python. He explains how it is different from other command-line utilities and how it can be used to manage dependencies and virtual environments.
What is Poetry?
- Poetry is an all-in-one system for managing projects in Python.
- It manages projects for all your Python installations.
- Unlike other command-line utilities, it does not need to be installed in Python but as a separate application.
Creating a New Project with Poetry
- To create a new project with Poetry, navigate to the directory where you want your project directory to appear and type
poetry newfollowed by the name of the project.
- This will create a directory with basic project scaffolding including pi
project.toml,readme.rst,testssubdirectory, and a subdirectory with the project's name which holds the actual code.
Managing Dependencies with Poetry
- By default, a new poetry project doesn't have a virtual environment associated with it. To do that we use poetry's end views command and pass along the path to the exact python executable we want to use.
- We can add one or more dependencies using the
poetry addcommand.
- If we want to add a dependency for development purposes only, we can use the same command but include
-dflag.
- A lock file is created when dependencies are installed. This lists all the projects and their specific versions so that projects' dependencies are reproducible.
Conclusion
Poetry is an excellent tool for managing Python projects. It simplifies dependency management and virtual environment creation while providing reproducibility of dependencies across different systems.