TechCacheKB.com

Technical News and Knowledge Base Articles along with How to Step by Step Guides for SMB and Enterprise


Getting Started with Python Poetry on Ubuntu 24.04


Poetry is a powerful tool for dependency management and packaging in Python. It streamlines the process of creating and managing Python projects, handling dependencies, and packaging code for distribution. This guide will walk you through the installation and use of Poetry on Ubuntu 24.04, with a focus on using Poetry through the command line.

What is Poetry?

Poetry is a dependency manager and packaging tool for Python projects. It aims to simplify the management of dependencies and project configurations, making it easier to maintain reproducible environments and distribute your code.

Key Features of Poetry:

  • Dependency Management: Easily specify and manage dependencies in a single pyproject.toml file.
  • Virtual Environments: Automatically create and manage virtual environments.
  • Packaging: Build and publish your projects to PyPI or other repositories.
  • Versioning: Manage and enforce versioning of your projects and dependencies.

Installing Poetry on Ubuntu 24.04

Step 1: Install Poetry Using the Official Installer

Open your terminal and run the following command to install Poetry:

curl -sSL https://install.python-poetry.org | python3 -

Step 2: Configure the Environment

After installation, you need to configure your shell to include Poetry’s bin directory in your PATH. Add the following line to your shell configuration file (e.g., ~/.bashrc, ~/.zshrc):

export PATH="$HOME/.local/bin:$PATH"

Apply the changes by sourcing the file:

source ~/.bashrc

Step 3: Verify the Installation

Ensure Poetry is installed correctly by checking its version:

poetry --version

You should see output similar to:

Poetry version x.y.z

Using Poetry: Basic Commands

Creating a New Python Project

To create a new Python project with Poetry, use the following command:

poetry new my_project

This command creates a new directory called my_project with a standard Python project structure:

my_project
├── pyproject.toml
├── README.rst
├── my_project
│   └── __init__.py
└── tests
    └── __init__.py

Navigating to the Project Directory

Navigate to the project directory:

cd my_project

Adding Dependencies

To add a dependency to your project, use the poetry add command followed by the package name:

poetry add requests

This command updates the pyproject.toml file and installs the specified package in a virtual environment managed by Poetry.

Installing Dependencies

If you have an existing pyproject.toml file with dependencies specified, you can install them using:

poetry install

Running Commands in the Virtual Environment

To run commands within the virtual environment managed by Poetry, use the poetry run command:

poetry run python my_project/main.py

You can also spawn a new shell within the virtual environment:

poetry shell

Exit the shell with:

exit

Building and Publishing Your Project

To build your project, use the poetry build command:

poetry build

This command generates distribution files (e.g., .tar.gz and .whl) in the dist directory.

To publish your project to PyPI or another repository, use:

poetry publish

You can specify the repository with the --repository option if you are not using the default PyPI.

Managing Dependencies

Listing Dependencies

To list all the dependencies of your project, use:

poetry show

Updating Dependencies

To update all dependencies to their latest compatible versions, use:

poetry update

To update a specific dependency, use:

poetry update <package_name>

Removing Dependencies

To remove a dependency from your project, use:

poetry remove <package_name>

Additional Tips and Information

Lock File

Poetry maintains a poetry.lock file to ensure consistent installations across different environments. This file records the exact versions of dependencies installed.

Development Dependencies

To add dependencies needed only for development, use the --dev flag:

poetry add --dev pytest

Configuring Poetry

You can configure Poetry settings using the poetry config command. For example, to set the default virtual environment path, use:

poetry config virtualenvs.path ~/.cache/pypoetry/virtualenvs

Using .env Files

Poetry supports the use of .env files for environment variable management. Create a .env file in your project root and add your environment variables. Poetry will automatically load these variables when running commands within the virtual environment.

Conclusion

Poetry is a robust tool that simplifies the management of Python projects, from dependency management to packaging and distribution. By following this guide, you should now have a solid understanding of how to install and use Poetry on Ubuntu 24.04. With its comprehensive set of features and intuitive commands, Poetry can significantly enhance your Python development workflow.