Skip to content

Publish and install private Python packages

Publish wheels and source distributions to a private Python index with twine. Install them with pip, uv or poetry and a read-only download key.

Overview

A PyPI registry is a private Python package index. You publish wheels and source distributions to it with twine. Your developers, CI systems and recipients install them with pip, uv or poetry.

This guide shows how to create a PyPI registry, publish a release, configure each client and manage releases. It also explains the URL layout, the credential checks and the index behaviour, so that you can find the cause of a failure. The guide is for Python developers and release engineers.

The examples use the registry slug acme, the project acme-sdk and the custom domain pypi.acme.com. Replace them with your values.

Before you start

  • To create a registry and to show the PyPI format, you must have the Admin role.
  • Your plan must have a free private registry slot. Starter has 1 private registry and Growth has 3. Scale and Platform have no registry limit.
  • You must have a hostname that you own for the registry, for example pypi.acme.com. The create form requires it. You verify it later with DNS.
  • On the release machine, you must have Python 3 with the build and twine packages.
  • On each install machine, you must have pip, uv or poetry.

How it works

A private namespace with no public fallback

PyPI registries on PrivateNPM.com are private only. A registry contains only the projects that your organization publishes to it. The registry never gets an absent project from pypi.org.

This design stops dependency confusion. In that attack, a person publishes a public package with the same name as your private package. A client that reads both indexes can then install the public package. Your PyPI registry has no public names, so a public package cannot replace a private project.

Project names are unique in each registry, not across all registries. The registry normalizes names as PEP 503 specifies: it changes letters to lowercase and changes each run of -, _ and . to one -.

URL layout

Each PyPI registry has a slug. The slug is the first path segment on the shared PyPI host, pypi.privatenpm.com. After you verify a custom domain, the same registry also answers on that domain without the slug.

EndpointShared PyPI hostCustom domain
Simple indexhttps://pypi.privatenpm.com/acme/simple/https://pypi.acme.com/simple/
Project page…/acme/simple/acme-sdk/…/simple/acme-sdk/
Upload (legacy API)https://pypi.privatenpm.com/acme/legacy/https://pypi.acme.com/legacy/
Distribution files…/acme/files/……/files/…
JSON API…/acme/pypi/acme-sdk/json…/pypi/acme-sdk/json

The registry creates the slug from the display name and adds a short suffix. You can change the slug on the page of the registry.

Credentials

Every request needs a credential. The registry reads HTTP Basic authentication with the username __token__ and a key as the password. The registry also accepts the key as a Bearer token. The username must be exactly __token__.

  • A publisher key (prt_…) can upload. It acts with the permissions of the member who created it. The role of that member must also allow uploads.
  • A download key (pdk_…) can only read the index and download files. It can never upload.
  • Each key works with one registry only.
ResponseCause
401 UnauthorizedThe request has no credential, the username is not __token__, or the key is unknown, revoked or expired.
403 ForbiddenThe key is valid, but it cannot do the action. For example, the key is for another registry, or it is a download key on the upload endpoint.
404 Not FoundThe project does not exist, or the key cannot read it. The registry does not tell a restricted key that a project exists.

The simple index

pip, uv and poetry read the simple repository API. The registry serves the HTML format of PEP 503 and the JSON format of PEP 691. A client gets JSON when it sends the header Accept: application/vnd.pypi.simple.v1+json.

The root index lists only the projects that the key can read. Each project page lists the live files of the project. Each file link includes:

  • A #sha256= hash, so that pip and uv verify the bytes.
  • The Requires-Python value, when the upload included it.
  • A yank marker for a yanked release, as PEP 592 specifies.
  • A hash of the core metadata, when the wheel has it. Clients can then read dependencies without a download of the full wheel (PEP 658).

If a client requests a name that is not normalized, the registry answers with a 301 redirect to the normalized project page.

Upload rules

twine uploads to the legacy endpoint with one multipart request for each file. The registry examines each upload:

  • The request must contain the sha256_digest field, and the digest must match the bytes. twine sends this field.
  • The filename must be a wheel or a source distribution name that matches the project name and version.
  • The first upload of a new project needs a publisher key with access to all packages in the registry.
  • If a live file has the same filename and SHA-256 digest, the registry accepts the upload and changes nothing.
  • If the filename exists with different bytes, the registry answers 400 "File already exists". PyPI never reuses a filename.

Index cache and ETags

Each project has a revision number. Each upload, yank, unyank and delete increases the revision. The registry keeps the file list of each project revision in a cache. It does not build the list again until the revision changes.

The project page has a weak ETag that contains the revision, and the header Cache-Control: private, no-cache. A client that sends If-None-Match with the current ETag gets 304 Not Modified and no body. After a change, the ETag changes and the client gets the new list. Distribution files never change, so they have Cache-Control: private, max-age=31536000, immutable.

Create the registry

Show the PyPI format

New organizations do not show PyPI in the navigation. The format switch changes only the menus. Registries, keys and installs work when the switch is off.

  1. Open Organization → General.
  2. In Registry formats, find PyPI.
  3. Click Turn on. The badge changes to "On", and PyPI projects appears in the Registry menu.

Create a PyPI registry

  1. Open Registry → Registries.
  2. Click New private registry.
  3. In Display name, type a name such as "Acme Python".
  4. In Format, select PyPI.
  5. If the form shows Package storage region, select a region. You cannot change the region later.
  6. In Registry hostname, type the hostname for the custom domain, for example pypi.acme.com.
  7. Click Create and verify domain. The domain page opens with the DNS records.

The registry works on the shared PyPI host immediately. To verify the custom domain, follow the domain verification guide. The same TXT record and CNAME steps apply to PyPI hostnames.

Change the slug

  1. On the registry card, click Manage registry.
  2. In Slug, type the new slug, for example acme. Use lowercase letters, digits and single hyphens.
  3. Click Save.

CAUTION: Change the slug before you give the URL to anyone. A new slug changes every URL on the shared PyPI host, and old URLs do not work.

Create the keys

Use a publisher key for the release job and a separate download key for each installer. Then you can revoke one installer without a change to your release job.

The publisher key form of a PyPI registry shows only PyPI permissions. The form shows a permission only when your role allows it.

PermissionAPI valueResult
Read projectspypi:project:readThe key can read the index and download files.
Upload releasespypi:project:publishThe key can upload files with twine.
Delete releasespypi:project:deletetwine and pip do not use this permission. A release job does not need it.
  1. On the registry card, click Manage keys.
  2. Click New publisher key. In Name, type the name of the release job.
  3. In Publisher permissions, select Read projects and Upload releases.
  4. In Package access, select All packages. A restricted key cannot create a new project.
  5. Click Create publisher key. Copy the secret into the secret store of your CI system.
  6. Click New download key. Select the recipient and type a Key name.
  7. In Access rule, select All packages in this registry or Selected projects.
  8. If you selected Selected projects, select each project that the recipient can install.
  9. Click Create download key. Copy the secret. The page also shows a pip command for the key.

A publisher key can also serve one project only. Clear All packages and select the project in Restrict to a project. That key can upload new releases of that project only.

NOTE: A project appears in Selected projects and Restrict to a project after its first upload. The management API accepts the same project IDs in packageIds.

Publish with twine

Before you start, make sure that the name and version in pyproject.toml are correct. A filename that exists cannot be uploaded again.

  1. Build the distributions: python -m build. The dist/ directory contains a wheel and a source distribution.
  2. Open Registry → PyPI projects. In Registry, select your PyPI registry.
  3. Click Setup. The page shows the snippets with the slug of the registry.
  4. Copy the .pypirc example into ~/.pypirc.
  5. Replace <key> with the publisher key, or remove the password line and use TWINE_PASSWORD.
  6. Run python -m twine upload --repository private dist/*. twine shows one upload line for each file and exits with status 0.
  7. Open Registry → PyPI projects. The list shows the project. The page of the project shows the release and the SHA-256 digest of each file.

The .pypirc file from the Setup page has this form:

[distutils]
index-servers =
    private

[private]
repository = https://pypi.privatenpm.com/acme/legacy/
username = __token__
password = <key>

On a machine with no .pypirc, give the same values in environment variables:

export TWINE_REPOSITORY_URL=https://pypi.privatenpm.com/acme/legacy/
export TWINE_USERNAME=__token__
export TWINE_PASSWORD=prt_...
python -m twine upload --non-interactive dist/*

Install with pip, uv or poetry

The examples read the download key from the environment variable ACME_PYPI_TOKEN. The Setup page of the registry shows the same commands with the real host and slug.

pip

pip install acme-sdk --index-url "https://__token__:${ACME_PYPI_TOKEN}@pypi.privatenpm.com/acme/simple/"

To use the registry for every pip command, set PIP_INDEX_URL to the same URL, or put index-url in pip.conf. The pip client can also read the username and password for the host from a .netrc file. Then the URL contains no secret.

uv

The Setup page shows an index for pyproject.toml with the key in the URL. Do not commit that form. Put the URL without credentials in the file and give the credentials to uv in environment variables:

[[tool.uv.index]]
name = "private"
url = "https://pypi.privatenpm.com/acme/simple/"
default = true
export UV_INDEX_PRIVATE_USERNAME=__token__
export UV_INDEX_PRIVATE_PASSWORD="$ACME_PYPI_TOKEN"
uv sync

poetry

Add the source without credentials, then give the credentials to poetry:

poetry source add private https://pypi.privatenpm.com/acme/simple/ --priority=primary
poetry config http-basic.private __token__ "$ACME_PYPI_TOKEN"
poetry add acme-sdk

In CI, poetry also reads POETRY_HTTP_BASIC_PRIVATE_USERNAME and POETRY_HTTP_BASIC_PRIVATE_PASSWORD.

Index URL or extra index URL

The Setup examples make the private registry the only index. pip uses --index-url, uv uses default = true and poetry uses --priority=primary. The client then does not read pypi.org. A dependency that exists only on pypi.org does not install.

pip also has --extra-index-url. With that option, pip reads all indexes and selects the highest version of a name from any index. It does not prefer your registry.

WARNING: Do not add your private registry and pypi.org together with --extra-index-url. A public package with a private name and a higher version replaces your package.

If a project needs private and public packages, give each private package its own index. In uv, mark the private index explicit = true and map each private package to it:

[[tool.uv.index]]
name = "private"
url = "https://pypi.privatenpm.com/acme/simple/"
explicit = true

[tool.uv.sources]
acme-sdk = { index = "private" }

In poetry, add the source with --priority=explicit and add each private package with --source private. pip cannot map one package to one index. With pip, use hash-checking mode (--require-hashes) and a lock file, so that pip refuses a file with an unexpected hash.

Publish and install in CI

This GitHub Actions workflow builds and publishes a release when you push a tag such as v1.4.0. Store the publisher key as the repository secret ACME_PYPI_PUBLISH_TOKEN.

name: Release
on:
  push:
    tags: ["v*"]
jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"
      - run: python -m pip install build twine
      - run: python -m build
      - run: python -m twine upload --non-interactive dist/*
        env:
          TWINE_REPOSITORY_URL: https://pypi.privatenpm.com/acme/legacy/
          TWINE_USERNAME: __token__
          TWINE_PASSWORD: ${{ secrets.ACME_PYPI_PUBLISH_TOKEN }}

A job that only installs uses a download key in the secret ACME_PYPI_TOKEN:

      - run: pip install acme-sdk
        env:
          PIP_INDEX_URL: https://__token__:${{ secrets.ACME_PYPI_TOKEN }}@pypi.privatenpm.com/acme/simple/

Yank and delete releases

Open Registry → PyPI projects and click the project. The Releases section has the actions for each release.

ActionResultPermission
Yank, with an optional Yank reasonThe files stay available. The index marks them as yanked, and pip and uv skip them unless a requirement pins that exact version.Publish
UnyankThe release is normal again.Publish
Delete fileThe file leaves the index. The filename stays reserved and cannot be uploaded again.Delete
Delete releaseEvery file of the release becomes a tombstone. The filenames stay reserved.Delete
Delete projectThe project name becomes a tombstone and cannot be published again in this registry. The registry refuses the action while files remain, unless you select Force: delete the remaining files too.Delete

A tombstone keeps a name or a filename reserved after a delete. Nobody can publish different bytes with that name later. Members can yank and unyank. Only admins can delete.

Verify the result

Do these steps on a machine with no pip configuration, or in a CI job.

  1. Read the project page: curl -i -u "__token__:$ACME_PYPI_TOKEN" https://pypi.privatenpm.com/acme/simple/acme-sdk/. The status is 200, and the body has one link for each file.
  2. Copy the value of the ETag header from that response.
  3. Send the request again with the header If-None-Match and that value. The status is 304.
  4. Create a clean environment: python -m venv /tmp/pypi-check.
  5. Install the project with /tmp/pypi-check/bin/pip and the --index-url from the pip example. pip shows "Successfully installed acme-sdk".
  6. Run the same command with a wrong key. pip asks for credentials or fails with a 401 error.

Troubleshoot

SymptomCauseCorrection
pip asks for a username and password, or shows 401.The URL has no credentials, the username is not __token__, or the key is revoked or expired.Examine the URL and the variable. Use __token__ exactly.
403 Forbidden on any request.The key is for another registry, or the permissions of the key or of its member do not allow the action.Create a key for this registry. Examine the role of the member who created the publisher key.
twine shows 403 on upload.twine sent a download key, the publisher key does not have Upload releases, or a restricted publisher key tried to create a new project.Use a publisher key with Upload releases and access to all packages.
The key form or the API refuses a permission that "belongs to another registry format".The request contains an npm or Files permission. A PyPI key accepts only PyPI permissions.Send only the pypi:project:… values from the permission table.
twine shows 400 "File already exists".A file with that name exists with different bytes, or a deleted file reserved the name.Increase the version and build again.
twine shows 400 "invalid upload".The filename does not match the name and version, the digest does not match, or the version is not a PEP 440 version.Build again with a standard build backend and upload with twine.
Upload refused because the name was used before.A deleted project reserved the name in this registry.Publish the project with a new name.
Upload refused with a plan message.The plan allows no more packages, the trial needs a payment method, or no plan is active.Open Organization → Plan & usage and change the plan.
pip says that no version satisfies the requirement.The key cannot read the project, all versions are yanked, or Requires-Python excludes your Python.Read the project page with curl and the same key.
A public dependency does not install.The registry is private only and is the only index.Map private packages to an explicit index, as the section above shows.
Every request returns 404.The slug in the URL is wrong or old, or the slug belongs to a registry of another format.Copy the URL again from the Setup page of the registry.

Security and maintenance

  • Keep keys out of pyproject.toml, requirements.txt and committed pip.conf files. Use environment variables, a .netrc file or your CI secret store.
  • Give each recipient and each CI system its own download key. Then you can revoke one of them alone.
  • Give keys an expiry date, and rotate the publisher key on a schedule. A rotation shows the new secret one time.
  • Revoke a key on the registry keys page when a person leaves or a secret leaks.
  • Examine Registry → Activity to see downloads by credential and rejected requests.
  • Use the audit log or the webhook events pypi_project.published, pypi_project.updated and pypi_project.deleted to follow changes.

READY WHEN YOU ARE

Put private packages to work.