Using Packagr with poetry

By Christopher Davies

Poetry is an intuitive way of managing dependencies in your Python packages via a simple CLI. Poetry makes it extremely simple to link your Python projects to private repositories, which makes it a very good fit for working with Packagr. I’m by no means an expert in Poetry, having only just discovered it myself, but in this post, I’ll attempt to show you how to get up and running quickly

Let’s start by installing Poetry — the installation is system-wide and only has to be done once. Just type the below command into a terminal window

curl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python

Once done, you’ll need to enter the following command to configure your shell correctly (apparently this is done for you the next time you restart your machine):

source $HOME/.poetry/env

If you haven’t already done so, you’ll need to sign up for Packagr, and grab your personal repository URL from the app:

Getting your repository URL from Packagr

Once you’ve got this URL, you can add it as a repository in Poetry:

poetry config repositories.packagr https://api.packagr.app/63cdQSDO/

Next, you’ll want to set the credentials for this repository. Once again, this is pretty trivial (make sure you change the email and password for your own):

poetry config http-basic.packagr chris@packagr.app password

We can now publish to Packagr using Poetry — no need to add credentials every time you want to push your code and no need to mess about with pip.conf

I mentioned earlier that the CLI is intuitive — to create a project in Poetry, just use this command:

poetry add packagr-project

This creates a folder called packagr-project with the following folder structure:

The poetry project folder structure

This creates a folder for your code, packagr_project, a tests folder for your unit tests, and a poetry config file called pyproject.toml, which we’ll take a look at later

This basic folder structure is all you need to start publishing your code — in fact, without writing a single line of code, you can publish this project to Packagr right now with the following command:

poetry build
poetry publish -r packagr

This should push your empty project straight up to Packagr:

I mentioned the config file, pyproject.toml, earlier on — let’s take a look at the content of that now:

[tool.poetry]
name = "packagr-project"
version = "0.1.0"
description = ""
authors = ["Chris Davies <chris@packagr.app>"]

[tool.poetry.dependencies]
python = "^3.6"

[tool.poetry.dev-dependencies]
pytest = "^3.0"

[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"

All your dependencies are defined in this file, as well as a bunch of other metadata that you’d normally see in your setup.py file if you’re using setuptools.

To add a dependency to your project, you can use the poetry add command

poetry add requests

This installs requests, and updates the config file, which should now look something like this:

[tool.poetry.dependencies]
python = "^3.6"
requests = "^2.21"

If we run poetry publish again, we can even see this gets appended to the package metadata in Packagr:

Requests added to the package metadata

We can also install dependencies from Packagr — we first need to add our repository to the config file — again, you’ll need your Packagr repository URL for this:

[[tool.poetry.source]]
name = "packagr"
url = "https://api.packagr.app/63cdQSDO/"

We already set our credentials for this repository earlier on, so there’s no need to do that again.

We can now add packages from our Packagr repository as follows — In this case, I’ve used a completely random package from one of my other tutorials. There is no need to specify the repository, as Poetry finds the right package for you

poetry add hello-world-server
Using version ^0.1.0 for hello-world-server
Updating dependencies
Resolving dependencies... (4.2s)
Package operations: 2 installs, 0 updates, 0 removals
Writing lock file
- Installing tornado (6.0.1)
- Installing hello-world-server (0.1.0)

You now have a basic overview of how to work with Poetry and Packagr. I’m not going to go into any more detail about Poetry as I’m still learning myself, but you may want to look at the excellent Poetry docs to learn more