# Python Repositories

> Use a CloudRepo Python repository with pip and twine: the index URL, ~/.pypirc, uploading, and installing private and public packages together.

Upload Python packages to CloudRepo with twine and install them with pip. CloudRepo serves the same index format and upload API that pip and twine use with PyPI, so each tool needs only an address and a credential.

## Before you start

- A Python repository. If you have none, see [Creating a Repository](/docs/manage/repositories.html#creating-a-repository).
- A repository token that reaches it, with **Download and publish packages** if you will upload. See [Repository Tokens: Create One and Authenticate](/docs/authenticate/repository-tokens.html). The username is the email address of the account that created the token, and the password is the token.
- The [Connection Settings](/docs/manage/repositories.html#view-connection-settings) of the repository show its URL with your names filled in.

> **Note:** Use a repository token, not a password. An organization owner’s portal password is not accepted for pip or twine. A token the owner creates works like anyone else’s.

## Upload and install

**pip**

**1. Put the upload credential in `~/.pypirc`.** twine reads the repository address and credential from this file. Use the repository URL without `/simple/`. The `@` in your email address needs no encoding here:

`~/.pypirc`

```ini
[distutils]
index-servers =
    cloudrepo


[cloudrepo]
repository = https://your-org.mycloudrepo.io/repositories/your-repo
username = you@example.com
password = YOUR_REPOSITORY_TOKEN
```

**2. Build and upload.** You need a project that builds a wheel or a source distribution, usually from a `pyproject.toml` (the [Python Packaging User Guide](https://packaging.python.org/en/latest/tutorials/packaging-projects/) covers that), and [twine](https://pypi.org/project/twine/) installed (`pip install twine`):

**Terminal**

```bash
pip wheel . --no-deps -w dist
twine upload --repository cloudrepo dist/*
```

Expected: twine finishes without an error. The file is then in the repository, and the [admin portal](https://admin.cloudrepo.io) shows it.

**3. Install.** pip finds the credential for the index’s host in `~/.netrc`, so the index URL carries none. The login is the email address of the account that created the token, and the password is the token. Keep the file readable by you alone (`chmod 600 ~/.netrc`):

`~/.netrc`

```ini
machine your-org.mycloudrepo.io
login you@example.com
password YOUR_REPOSITORY_TOKEN
```

Then set the index URL in the environment:

**Terminal**

```bash
export PIP_INDEX_URL="https://your-org.mycloudrepo.io/repositories/your-repo/simple/"
pip install my-package
```

To keep it for every install, put the same URL in `~/.config/pip/pip.conf` (`pip.ini` on Windows):

`~/.config/pip/pip.conf`

```ini
[global]
index-url = https://your-org.mycloudrepo.io/repositories/your-repo/simple/
```

> **Note:** Each file you upload to a Python repository (a wheel or a source distribution) can be up to 5 GiB (5,368,709,120 bytes), counting any signature file uploaded with it. A larger upload is refused with `413`.

The `/simple/` path, with the trailing slash, is the standard simple-API index URL that pip, Poetry and uv expect. The bare repository URL also serves the index, but use `/simple/` for installs. twine uploads to the bare repository URL, as in `~/.pypirc` above.

## Install private and public packages together

Install your private packages from CloudRepo alone. Anyone can publish a package on PyPI under your private package’s name, and an install that can read PyPI as well may install theirs instead.

pip cannot tie a package to an index. Given a second index (`--extra-index-url`, the `extra-index-url` key in `pip.conf`, or `PIP_EXTRA_INDEX_URL`), pip reads both as one and installs the highest version it finds in either. An exact `==` pin does not help: when both indexes have that version, pip can take PyPI’s copy. So install in three steps, with one index each, in this order:

**pip**

1. Public packages, from PyPI (pip’s default index).
2. Private packages, from CloudRepo and nothing else.
3. Your own project, last, without its dependencies (steps 1 and 2 installed them).

**Public packages from PyPI, then private packages from CloudRepo alone**

```bash
pip install -r requirements.txt
pip install -r requirements-private.txt
pip install --no-deps -e .
```

`requirements-private.txt` makes CloudRepo the only index for that command. pip finds the login for the host in the `~/.netrc` from step 3 above, so the file holds no credential and is safe to commit or copy into an image:

`requirements-private.txt`

```ini
--index-url https://your-org.mycloudrepo.io/repositories/your-repo/simple/
my-package==1.0.0
```

Step 2 on its own checks that CloudRepo alone serves your private packages:

**Terminal**

```bash
pip install -r requirements-private.txt
```

- Use the URL of the repository that hosts your private packages, not one that proxies PyPI.
- Nothing step 1 installs may depend on a private package, your own project included: pip would fetch that private package from PyPI in step 1. Keep private package names, and `.` or `-e .`, out of `requirements.txt`, and install your project last with `--no-deps`.
- Step 3 still builds your project in an isolated environment, which pip fills from its default index, PyPI. If `[build-system] requires` names a private package, put it in `requirements-private.txt`, put the public build requirements (such as `setuptools`) in `requirements.txt`, and run step 3 as `pip install --no-deps --no-build-isolation -e .`.
- List the public packages that your private packages depend on in `requirements.txt`. Step 2 then finds them installed. If one is missing, step 2 fails rather than looking for it on PyPI.

A requirements file with hashes protects something else. With `pip install --require-hashes -r requirements.txt`, pip installs only files whose SHA-256 you recorded, so a file published later under the same name and version is refused, whichever index serves it. Hashes do not choose the index: with two indexes the install can stop with a hash error instead of installing your copy. Record them from an install that read CloudRepo alone for your private packages, because a hash taken from PyPI’s copy protects PyPI’s copy.

uv and Poetry can tie a package to an index, so they need no second step:

- **uv:** an index with `explicit = true` and a `[tool.uv.sources]` entry for each private package. See [Pin private packages in a project](/docs/formats/uv.html#pin-private-packages-in-a-project).
- **Poetry:** a source with `priority = "explicit"` and `source = "cloudrepo"` on each private dependency. See [Add CloudRepo as an explicit source](/docs/formats/poetry.html#add-cloudrepo-as-an-explicit-source).

With both, list every private package as a dependency of your project, including the private packages that your private packages depend on. The pin applies only to the packages you list, and these tools look up any other name on PyPI.

## Python Repository Settings

### Overwrite Protection

Overwrite Protection is on by default for a Python repository: a repository that has not turned it off is protected, whenever it was created. With it on, uploading a file to a path that already holds one is refused with `409 Conflict`, and the stored file is left as it is. The same goes for a wheel or source distribution that differs only in case, in how the project’s name is spelled (`My_Package` for `my-package`), or in how the version is spelled where pip reads the two as one version (`1.0` for `1.0.0`, `1.0c1` for `1.0rc1`). A new version, such as `1.0.1` or `1.0.0+local`, uploads as before. A wheel or source distribution must also name its project: `other-1.0.tar.gz` uploaded as `my-package` is refused with `400 Bad Request`.

twine prints only the status line, `HTTPError: 409 Conflict`. Run the upload again with `--verbose` to read the explanation CloudRepo sends with it: `Attempting to Overwrite an existing file. Overwrites have been disabled for this repository.` When the file is another spelling of a stored one, the explanation names the stored file and suggests `twine upload --skip-existing` or turning Overwrite Protection off. Python has no mutable-version convention, so there is no equivalent of Maven’s `-SNAPSHOT` escape hatch here: no file is exempt. To republish, either use a new version number or turn Overwrite Protection off for that repository.

Overwrite Protection is a per-repository setting, so a repository that is meant to accept overwrites can have it turned off. Toggle it in the Repository Settings of your Python repository. Turning it off asks you to confirm your identity first. For how the setting behaves across every repository type, see [Overwrite Protection](/docs/manage/repositories.html#overwrite-protection).

![Repository Settings for python-packages: Overwrite Protection is on, and attempts to overwrite existing files will be rejected.](/docs/_astro/shots/python-settings/1-overwrite-protection-on.bcf676c9b3c4725b.png)

![Overwrite Protection is now off, and the card says existing files can be overwritten.](/docs/_astro/shots/python-settings/2-overwrite-protection-off.13d919acb058bacf.png)

### Other Settings

All [Standard Settings](/docs/manage/repositories.html#repository-settings) apply.

---

The page: https://www.cloudrepo.io/docs/formats/python.html
