Poetry
Poetry is a dependency manager and build tool for Python that makes package management a breeze. It allows you to easily define your project dependencies and manage them in a consistent and reproducible way. In addition, it provides a simple way to create and manage virtual environments for your projects, and has powerful support for packaging and publishing your Python packages.
Installing Poetry
The easiest way to install Poetry is using the official installation script. Simply run the following command in your terminal:
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
You can also install Poetry using pip
, but it's not recommended:
pip install poetry
Using Poetry
Once you have Poetry installed, you can start using it to manage your Python projects. Here's a quick tutorial on how to get started:
1. Creating a new project
To create a new project with Poetry, simply navigate to the directory where you want your project to be and run:
poetry new my_project
This will create a new project directory with the basic structure for a Python package.
2. Adding dependencies
To add a new dependency to your project, simply run:
poetry add my_dependency
This will download and install the latest version of the dependency and add it to your pyproject.toml
file.
You can also specify the version of the dependency you want to install:
poetry add my_dependency==1.2.3
3. Creating a virtual environment
To create a new virtual environment for your project, simply run:
poetry env use python3.9
This will create a new virtual environment using Python 3.9. You can then activate the environment by running:
poetry shell
4. Exporting requirements for deployments
To export a requirements.txt
file from Poetry, you can use the poetry export command. This command creates a requirements.txt
file with all the dependencies defined in the poetry.lock
file.
To export the requirements.txt
file, navigate to the root directory of your project in your terminal and run the following command:
poetry export --format=requirements.txt --output=requirements.txt --without-hashes
This will create a requirements.txt
file in the same directory with all the dependencies listed. The --format=requirements.txt
option specifies the format of the output, --output=requirements.txt
specifies the name of the output file, and --without-hashes
specifies that the output file should not include hashes for each dependency.
Once you have exported the requirements.txt
file, you can use it to install the dependencies using pip
. To install the dependencies, run the following command:
pip install -r requirements.txt
This will install all the dependencies listed in the requirements.txt
file.
pip disclaimer
At Figuro, we discourage the use of pip
as a package manager in favor of poetry
. While pip
is a popular and widely used tool for managing Python packages, it can have some limitations and drawbacks when it comes to managing complex project dependencies. Poetry
provides a more comprehensive and streamlined approach to package management, with features such as dependency resolution, version locking, and virtual environments. By using poetry
, we can ensure that our projects have consistent and reliable dependencies, and avoid potential issues that may arise from using pip
.