Python Poetry: Simplify Your Dependency Management
Introduction:
Managing dependencies for Python projects can be a daunting task, especially when dealing with multiple projects, different versions of the same dependencies, and conflicts between them. However, Python Poetry can help you manage your dependencies efficiently and save you a lot of time and effort. In this article, we will learn how to use Poetry to create and manage Python projects.
Installing Python Poetry
Python Poetry can be installed in various ways. However, in this article, we will cover three of the most common methods:
Installing Poetry via pip
If you already have Python installed on your system, you can install Poetry via pip, the Python package manager. To do this, simply run the following command:
pip install poetry
Installing Poetry via Homebrew
If you are using macOS, you can also install Poetry via Homebrew, the package manager for macOS. To do this, first, install Homebrew if you haven’t already done so:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Then, run the following command to install Poetry:
brew install poetry
Installing Poetry via a pre-built binary
If you prefer not to use a package manager, you can also download a pre-built binary of Poetry for your operating system. To do this, go to the Poetry releases page on GitHub:
https://github.com/python-poetry/poetry/releases
Download the appropriate binary for your system (e.g., poetry-1.2.0-linux-x86_64.tar.gz
for Linux, or poetry-1.2.0-macosx-arm64.tar.gz
for macOS with the Apple Silicon M1 chip), extract the contents of the archive, and move the poetry
executable to a directory in your $PATH
.
Creating a New Project
Now that we have installed Poetry let’s create a new project with it. Navigate to the directory where you want to create the project and run the following command:
poetry new project_name
This command will create a new directory named project_name
, containing a basic project structure, including a pyproject.toml
file that contains the project metadata and configuration information.
Adding Dependencies
Let’s add a few dependencies to our newly created project. In the pyproject.toml
file, under the [tool.poetry.dependencies]
section, add the following lines:
[tool.poetry.dependencies]
requests = "^2.25.1"
numpy = "^1.22.1"
pandas = "^1.3.5"
These dependencies are just examples; you can replace them with any dependencies your project requires. The ^
character before the version number indicates that any compatible version of the dependency can be installed.
Installing Dependencies
To install the dependencies of your project, run the following command:
poetry install
Poetry will download and install the dependencies listed in the pyproject.toml
file into a virtual environment that is specific to the project.
Writing Code
Let’s create a simple Python script that uses the requests
, numpy
, and pandas
libraries. Create a new file called main.py
and add the following code:
import requests
import numpy as np
import pandas as pd
response = requests.get("https://jsonplaceholder.typicode.com/todos/1")
print(response.json())
data = np.array([[1, 2], [3, 4]])
print(data)
df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
print(df)
In this code, we imported the requests
, numpy
, and pandas
libraries, made a GET request to a JSON API using requests
, printed the response from the API, created a NumPy array data
, printed the array, created a Pandas DataFrame df
, and printed the DataFrame.
Running Code
To run the main.py
script, we can use the following command:
poetry run python main.py
This command activates the virtual environment created by Poetry and runs the script with the installed dependencies.
Creating a Lock File
When we run the poetry install
command, Poetry creates a poetry.lock
file that specifies the exact versions of the dependencies installed in the virtual environment. This file ensures that the project always uses the same versions of the dependencies, even if the pyproject.toml
file is modified.
Building and Packaging the Project
To build the project for distribution, run the following command:
poetry build
This command creates a distributable package of the project in the dist
directory. The package includes the project's source code, the pyproject.toml
file, and the poetry.lock
file.
Publishing the Project
If you want to share your project with the world, you can publish it to a package repository like PyPI. To do this, you will need to create an account on PyPI and configure Poetry to use your account credentials.
Once you have done that, run the following command:
poetry publish
This command uploads the package to PyPI and makes it available to others.
Conclusion
Python Poetry is an excellent tool for managing dependencies and packaging Python projects. With Poetry, you can easily specify your project’s dependencies, install them in a virtual environment, and build a distributable package of your project. By automating these tasks, Poetry saves you time and effort and helps you focus on writing great code.