Using Packagr with poetry

Poetry is an open-source tool that helps you manage package dependencies within your Python project and provides 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, and works well in conjunction with Packagr.

Installing Poetry

The poetry package is installed system-wide and only has to be done once. It can be installed by entering 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. Note that you only need to do that once - the next time you restart your machine, this variable will be set automatically:

source $HOME/.poetry/env

Linking to your Packagr account

You'll need your unique Repository URL for this

You can add your repository to poetry using the following command, substituting in your own repository URL:

poetry config repositories.packagr https://api.packagr.app/<my-hash-id>/

poetry also allows you to store your credentials against your repository, so you don't need to enter them every time you want to upload a package. Here's the command you'll need to use - make sure you change the email and password for your own:

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

Your credentials are now stored - you only need to define your repository and credentials once per machine, unless your credentials change.

Creating a project in Poetry

Let's create a simple hello-world example using poetry. The following command will create a new poetry project which you can add your code to:

poetry new hello-world

This creates a folder called hello-world with the following folder structure:

hello-world/
  hello_world/
    __init__.py
  tests/
  pyproject.toml
  README.rst

This basic folder structure is all you need to start publishing your code — let's edit the file called __init__.py in the hello_world folder to look like the following:

def hello(*args, **kwargs):
    return 'Hello, world!'

We can also add the following lines to the pyproject.toml file, which allows you to run the code as a binary:

[tool.poetry.scripts]
hello = "hello_world:hello"

Building and publishing to Packagr

Now we can build and publish our code with the following command

poetry publish -r packagr --build

This will deploy your code straight to Packagr.

Adding a package from Packagr as a dependency in a poetry project

It's also possible to add dependencies from Packagr to a poetry project. To do this, first create a second project with poetry new

poetry new my-other-project

Next, edit pyproject.toml to include your package repository (substituting your repository URL)

[[tool.poetry.source]]
name = "packagr"
url = "https://api.packagr.app/<my-hash-id>/"

Note that you do not need to add credentials here if you already did this globally as described earlier in this page

Now, we can add our hello-world package as a dependency in out my-other-project project with the following command

poetry add hello-world