Monday, December 20, 2021

Dependency Management With Python Poetry

When your Python project relies on external packages, you need to make sure you’re using the right version of each package. After an update, a package might not work as it did before the update. A dependency manager like Python Poetry helps you specify, install, and resolve external packages in your projects. This way, you can be sure that you always work with the right dependency version on every machine.

In this tutorial, you’ll learn how to:

  • Start a new Poetry project
  • Add Poetry to an existing project
  • Use the pyproject.toml file
  • Pin dependencies
  • Install dependencies with poetry.lock
  • Execute basic Poetry CLI commands

Using Poetry will help you start new projects, maintain existing ones, and master dependency management. You’ll be prepared to work with pyproject.toml files, which will be the standard for defining build requirements in Python projects.

To complete this tutorial and get the most out of it, you should have a basic understanding of virtual environments, modules and packages, and pip.

While this tutorial focuses on dependency management, Poetry can also help you with building and packaging projects. If you want to share your work, then you can even publish your Poetry project to the Python Packaging Index (PyPI).

Take Care of Prerequisites

Before diving into the nitty-gritty of Python Poetry, you’ll take care of some prerequisites. First, you’ll read a short overview of terminology you’ll encounter in this tutorial. Next, you’ll install Poetry itself.

Relevant Terminology

If you’ve ever used an import statement in one of your Python scripts, then you’ve worked with modules. Some of these modules might have been Python files you wrote on your own. Others could have been built-in modules, like datetime. However, sometimes what Python provides isn’t enough. That’s when you might turn to external, packaged modules. When your Python code relies on external modules, you can say that these packages are dependencies of your project.

You can find packages that aren’t part of the Python standard library in PyPI. Before seeing how this works, you need to install Poetry on your system.

Python Poetry Installation

To use Poetry in your command line, you should install it system-wide. If you just want to try it out, then you can install it into a virtual environment using pip. But you should try this method with caution because Poetry will install its own dependencies, which can conflict with other packages you’re using in your project.

The recommended way to install Poetry is by using the official install-poetry script. You can either download and run this Python file manually or select your operating system below to use the appropriate command:

PS C:\> (Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py -UseBasicParsing).Content | python -

If you’re on Windows, then you can use the Invoke-Webrequest cmdlet with the -UseBasicParsing option to download the content of a requested URL to the standard output stream (stdout). With the pipe character (|), you’re handing over the output to the standard input stream (stdin) of python. In this case, you’re piping the content of install-poetry.py to your Python interpreter.

$ curl https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py | python3 -

With curl, you’re outputting the content of a requested URL to the standard output stream (stdout). By using a Unix pipeline with the pipe character (|), you’re handing over the output to the standard input stream (stdin) of python3. In this case, you’re piping the content of install-poetry.py to your Python interpreter.

In the output, you should see a message that the installation is complete. You can run poetry --version in your terminal to see if poetry works. This command will display your current Poetry version. If you want to update Poetry, then you can run poetry self update.

Get Started With Python Poetry

With Poetry installed, it’s time to see how Poetry works. In this section, you’ll learn how to start a fresh Poetry project and how to add Poetry to an existing project. You’ll also see the project structure and inspect the pyproject.toml file.

Create a New Poetry Project

You can create a new Poetry project by using the new command and a project name as an argument. In this tutorial, the project is called rp-poetry. Create the project, and then move into the newly created directory:

$ poetry new rp-poetry
$ cd rp-poetry

By running poetry new rp-poetry, you create a new folder named rp-poetry/. When you look inside the folder, you’ll see a structure:

rp-poetry/
│
├── rp_poetry/
│   └── __init__.py
│
├── tests/
│   ├── __init__.py
│   └── test_rp_poetry.py
│
├── README.rst
└── pyproject.toml

Read the full article at https://realpython.com/dependency-management-python-poetry/ »


[ Improve Your Python With ๐Ÿ Python Tricks ๐Ÿ’Œ – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]



from Real Python
read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...