Poetry is a comprehensive dependency management and packaging tool for python projects. It aims to simplify the entire workflow of managing project dependencies, creating virtual environments, and packaging for distribution.
Key features and functionalitites of Poetry:
- Dependency Management: Poetry allows users to declare project dependencies within a pyproject.toml file. It then manages the installation, updating, and resolution of these dependencies, including handling potential version conflicts.
- Virtual Environments: Poetry automatically creates and manages isolated virtual environments for each project. This ensures that project dependencies are kept separate from the system’s Python installation and from other projects, preventing conflicts and promoting reproducible environments.
- Dependency Locking: Poetry generates a poetry.lock file, which records the exact versions of all direct and transitive dependencies installed in the project. This lock file ensures that the project’s environment can be precisely reproduced across different machines or at different times.
- Packaging and Publishing: Poetry streamlines the process of packaging Python projects into distributable formats. It handles versioning, metadata, and the building of packages, making it easy to publish projects to package indexes like PyPI.
- Project Management: Poetry provides a set of command-line interface (CLI) commands for various project management tasks, including creating new projects, adding or removing dependencies, installing dependencies from the lock file, and publishing packages.
- Centralized Configuration: Poetry utilizes the pyproject.toml file to consolidate project metadata, dependencies, and build system settings into a single, standardized location, replacing the need for separate files like setup.py, requirements.txt, and setup.cfg.
- Development vs. Production Dependencies: Poetry allows for the clear separation of development dependencies (e.g., testing frameworks, linters) from production dependencies, ensuring that only necessary packages are installed in deployment environments.
Installation (Isolated)
Use the official installer script for your operating system:
- Linux, macOs, or Windows(WSL):
curl -sSL https://install.python-poetry.org | python3 -
Windows (PowerShell):
(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | py -
Alternative Installation (using pipx)
If you have $\text{pipx}$ installed, it’s another great way to isolate command-line applications:
pipx install poetry
Creating the pyproject.toml File
The pyproject.toml file is the central configuration file for a Poetry project, replacing files like requirements.txt, setup.py, and setup.cfg.
Automatic Creation (Recommended)
Poetry will automatically create the pyproject.toml file for you using one of two primary commands:
- For a New Project:
poetry new
This command sets up an entirely new project structure and creates the pyproject.toml with basic metadata.
poetry new my-fastapi-project
cd my-fastapi-project
This creates a directory with a standard structure:
my-fastapi-project/
├── pyproject.toml <-- The config file
├── README.md
├── my_fastapi_project/
│ └── __init__.py
└── tests/
└── __init__.py
- For an Existing Project:
poetry init
If you already have a project directory (like the one you installed FastAPI in with pip), you use poetry init:
cd /path/to/your/existing/project
poetry init
This command will start an interactive process where Poetry asks you questions about your project:
- Package name
- Version
- Description
- Author
- License
- Required Python version
- Project dependencies (you can search and add them here)
Once the questions are answered, Poetry generates the pyproject.toml file in your current directory.
Manual vs. Automatic Updates
The initial creation is automatic or semi-automatic poetry init. Going forward, you should rarely edit the pyproject.toml file manually for dependencies.
To add a dependency (like FastAPI):
poetry add fastapi
Poetry automatically finds a suitable version, installs it, and updates the pyproject.toml file and generates a poetry.lock file.
To remove a dependency:
poetry remove fastapi
Poetry uninstalls it and updates the pyproject.toml file.
You would only edit the pyproject.toml file manually if you need to adjust project metadata (like the author or description) or manually define advanced dependency groups.
Hope, it will help you to understand the potery management tools and setup it in our system.