In this article, we will look at Poetry, a tool for dependency management and packaging in Python, and how to publish a package built in Poetry to a private Python package manager hosted in Packagr
What is Poetry?
Poetry describes itself as “Python packaging and dependency management made easy”. It combines the functionality of the standard python packaging tools setuptools
and twine
, allowing you to build and publish your Python package in a single command, while also managing your python package dependencies.
By default, Poetry publishes built packages to the default public Python package repository, PyPI. However, it is also possible to point poetry at your own private Python package server instead.
Provisioning a private Package repo
There are several open-source solutions for provisioning your own, personal PyPI server — for example, this can be done using pypiserver or devpi. However, configuring these services takes time and effort, and it costs money to deploy them. Instead, we’ll use Packagr, a cloud-hosted python package server that allows you to provision your own private Python package repository. It also supports NPM packages and even Docker registries and is very easy to set up. You can get started by creating a free trial account on Packagr — when you’ve created your account, you’ll see this:
When your account is created, Packagr automatically generates a private repository URL for you. To find it click on Python packages in the left-hand sidebar, then look for your unique Repository URL towards the top of the page:
The URL will be in the format https://api.packagr.app/abcdef
. Make a note of your repository URL before continuing — you will need it later.
Installing Poetry
Use this one-liner to install Poetry on your local machine:
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
You may also need to configure your shell with the following command to complete the installation:
source $HOME/.poetry/env
Check your installation worked with the following command:
poetry -v
Creating a package in Poetry
Now that poetry is installed you can create a poetry project like this:
poetry new private-package
This will create a subfolder called private-package
, containing a package skeleton.
And that’s it! We have created an empty Python package. We’re going to leave it blank for the sake of this simple tutorial.
Pointing Poetry at your private repository
We can now configure Poetry to publish our private-package
package to our Packagr private repository by using the below command, replacing the URL with our unique Packagr repo URL:
poetry config repositories.packagr https://api.packagr.app/abcdef
Next, we can use the below command to store our Packagr credentials locally, meaning that we don’t have to enter them every time we want to publish the package:
poetry config http-basic.packagr packagr-username@example.com password
Publishing the package to our private repository
We’re now ready to publish! To do so, simply use this command:
poetry publish -r packagr
If all goes well, your private Python package will be uploaded successfully. You can confirm this by refreshing your Packagr UI:
Installing a private package with Poetry
Now that we have published our Python package to a private repository, we can install it in other poetry projects. To do this, you’ll need to add this section to your pyproject.toml
(inserting your private repository URL):
[[tool.poetry.source]]
name = "packagr"
url = "https://api.packagr.app/abcdef"
secondary = true
The secondary = true
bit is not essential here - however, if you have a project which has a lot of dependencies in PyPI as well, then resolving packages will take much less time.
You can now do the following to install python packages locally:
poetry add private-package --source packagr
Top comments (0)