How to add a private repository using poetry?
Asked Answered
M

2

6

Using Artifactory (https://cloud.google.com/artifact-registry) I intend to add a dependency with poetry (https://python-poetry.org/docs/repositories/).

I can install with command: pip install --index-url https://us-central1-python.pkg.dev/<PROJECT_ID>/<SOME_LIB_REPO>/simple/ <PACKAGE_NAME> (auth using keyrings.google-artifactregistry-auth)

I intend to use POETRY to manage my dependencies. I don't know if using poetry adds this dependence. With poetry add --source <PACKAGE_NAME> https://us-central1-python.pkg.dev/<PROJECT_ID>/<SOME_LIB_REPO> I'm getting this error:

poetry update
Updating dependencies
Resolving dependencies... (0.9s)

  RepositoryError

  401 Client Error: Unauthorized for url: https://us-central1-python.pkg.dev/<PROJECT_ID>/<SOME_LIB_REPO>/simple/pytest/

My pyproject.toml

...

[[tool.poetry.source]]
name = "SOME_LIB"
url = " https://us-central1-python.pkg.dev/<PROJECT_ID>/<SOME_LIB_REPO>/simple/"
secondary = true

Here there is details how to config with PIP/VIRTUALENV: https://cloud.google.com/artifact-registry/docs/python/authentication but not have details about Poetry.

Do you have any tips about it?

Mccammon answered 4/11, 2022 at 21:57 Comment(1)
Poetry documentation has a section about credentials: python-poetry.org/docs/repositories/#configuring-credentialsTheatre
R
2

Your RepositoryError of

401 Client Error: Unauthorized for url: https://us-central1-python.pkg.dev/<PROJECT_ID>/<SOME_LIB_REPO>/simple/pytest/

clearly indicates that you lack authorization for the URL mentioned. So you will need to make sure you properly get and use the authorization, that is, you will have a user with credentials who actually has the right to access it.

Once you have that, you will need to authenticate, so the server you are sending the request to will be able to determine who you are and therefore to grant you proper access. Read: https://python-poetry.org/docs/repositories/#configuring-credentials

The page provides the

poetry config http-basic.foo <username> <password>

example, explaining that you can set up some credentials for a specific repository. You can specify only the username, but then whenever you try to access the URL, you will be prompted for the password.

You can also publish by adding --username and --password to the command. You can also specify environment variables, like

export POETRY_PYPI_TOKEN_PYPI=my-token
export POETRY_HTTP_BASIC_PYPI_USERNAME=<username>
export POETRY_HTTP_BASIC_PYPI_PASSWORD=<password>

See https://python-poetry.org/docs/configuration/#using-environment-variables

Anyways, the error tells you that you were not properly authenticated via the credentials that are needed, so either you do not have credentials, they are incorrect or you lack privilege.

Roundelay answered 24/2 at 12:17 Comment(0)
P
0

First, you need to create a "supplemental" source. This is, the registry that will be queried after PyPI (the default). In the example below we call the new source foo:

poetry source add --priority=supplemental foo https://us-central1-python.pkg.dev/<PROJECT_ID>/<SOME_LIB_REPO>/simple/

This will append the following block to your pyproject.toml:

[[tool.poetry.source]]
name = "foo"
url = "https://us-central1-python.pkg.dev/<PROJECT_ID>/<SOME_LIB_REPO>/simple/"
priority = "supplemental"

Second, add the GCP authentication credentials to poetry:

poetry self add keyrings.google-artifactregistry-auth

Lastly, specify the source when adding the new dependency to the pyproject.toml:

poetry add --source foo private-package

For more information see the poetry documentation.

Plethoric answered 24/2 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.