debian-mirror-gitlab/doc/user/packages/pypi_repository/index.md

359 lines
12 KiB
Markdown
Raw Normal View History

2020-06-23 00:09:42 +05:30
---
stage: Package
group: Package
2021-02-22 17:27:13 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2020-06-23 00:09:42 +05:30
---
2021-03-11 19:13:27 +05:30
# PyPI packages in the Package Registry **(FREE)**
2020-04-22 19:07:51 +05:30
2021-03-11 19:13:27 +05:30
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/208747) in GitLab Premium 12.10.
> - [Moved](https://gitlab.com/gitlab-org/gitlab/-/issues/221259) to GitLab Free in 13.3.
2020-04-22 19:07:51 +05:30
2021-01-29 00:20:46 +05:30
Publish PyPI packages in your projects Package Registry. Then install the
packages whenever you need to use them as a dependency.
2020-04-22 19:07:51 +05:30
2021-01-29 00:20:46 +05:30
The Package Registry works with:
2020-04-22 19:07:51 +05:30
- [pip](https://pypi.org/project/pip/)
- [twine](https://pypi.org/project/twine/)
2021-01-29 00:20:46 +05:30
## Build a PyPI package
2020-04-22 19:07:51 +05:30
2021-01-29 00:20:46 +05:30
This section explains how to create a PyPI package.
2020-04-22 19:07:51 +05:30
2021-01-29 00:20:46 +05:30
If you already use PyPI and know how to build your own packages, go to the
[next section](#authenticate-with-the-package-registry).
2020-04-22 19:07:51 +05:30
2021-01-29 00:20:46 +05:30
### Install pip and twine
2020-04-22 19:07:51 +05:30
2021-01-29 00:20:46 +05:30
Install a recent version of [pip](https://pypi.org/project/pip/) and
[twine](https://pypi.org/project/twine/).
2020-04-22 19:07:51 +05:30
2021-01-29 00:20:46 +05:30
### Create a project
2020-04-22 19:07:51 +05:30
2021-01-29 00:20:46 +05:30
Create a test project.
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
1. Open your terminal.
1. Create a directory called `MyPyPiPackage`, and then go to that directory:
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
```shell
mkdir MyPyPiPackage && cd MyPyPiPackage
```
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
1. Create another directory and go to it:
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
```shell
mkdir mypypipackage && cd mypypipackage
```
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
1. Create the required files in this directory:
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
```shell
touch __init__.py
touch greet.py
```
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
1. Open the `greet.py` file, and then add:
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
```python
def SayHello():
print("Hello from MyPyPiPackage")
return
```
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
1. Open the `__init__.py` file, and then add:
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
```python
from .greet import SayHello
```
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
1. To test the code, in your `MyPyPiPackage` directory, start the Python prompt.
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
```shell
python
```
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
1. Run this command:
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
```python
>>> from mypypipackage import SayHello
>>> SayHello()
```
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
A message indicates that the project was set up successfully:
2020-05-24 23:13:21 +05:30
```plaintext
Python 3.8.2 (v3.8.2:7b3ab5921f, Feb 24 2020, 17:52:18)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from mypypipackage import SayHello
>>> SayHello()
Hello from MyPyPiPackage
```
### Create a package
2021-01-29 00:20:46 +05:30
After you create a project, you can create a package.
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
1. In your terminal, go to the `MyPyPiPackage` directory.
1. Create a `setup.py` file:
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
```shell
touch setup.py
```
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
This file contains all the information about the package. For more information
about this file, see [creating setup.py](https://packaging.python.org/tutorials/packaging-projects/#creating-setup-py).
Because GitLab identifies packages based on
[Python normalized names (PEP-503)](https://www.python.org/dev/peps/pep-0503/#normalized-names),
ensure your package name meets these requirements. See the [installation section](#authenticate-with-a-ci-job-token)
for details.
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
1. Open the `setup.py` file, and then add basic information:
```python
import setuptools
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
setuptools.setup(
name="mypypipackage",
version="0.0.1",
author="Example Author",
author_email="author@example.com",
description="A small example package",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
)
```
1. Save the file.
1. Execute the setup:
```shell
python3 setup.py sdist bdist_wheel
```
The output should be visible in a newly-created `dist` folder:
2020-05-24 23:13:21 +05:30
```shell
ls dist
```
2021-01-29 00:20:46 +05:30
The output should appear similar to the following:
2020-05-24 23:13:21 +05:30
```plaintext
mypypipackage-0.0.1-py3-none-any.whl mypypipackage-0.0.1.tar.gz
```
2021-01-29 00:20:46 +05:30
The package is now ready to be published to the Package Registry.
2020-04-22 19:07:51 +05:30
2021-01-29 00:20:46 +05:30
## Authenticate with the Package Registry
2020-04-22 19:07:51 +05:30
2021-01-29 00:20:46 +05:30
Before you can publish to the Package Registry, you must authenticate.
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
To do this, you can use:
2020-04-22 19:07:51 +05:30
2021-01-29 00:20:46 +05:30
- A [personal access token](../../../user/profile/personal_access_tokens.md)
with the scope set to `api`.
- A [deploy token](../../project/deploy_tokens/index.md) with the scope set to
`read_package_registry`, `write_package_registry`, or both.
- A [CI job token](#authenticate-with-a-ci-job-token).
2020-04-22 19:07:51 +05:30
2021-01-29 00:20:46 +05:30
### Authenticate with a personal access token
To authenticate with a personal access token, edit the `~/.pypirc` file and add:
2020-04-22 19:07:51 +05:30
```ini
2020-05-24 23:13:21 +05:30
[distutils]
index-servers =
gitlab
2020-04-22 19:07:51 +05:30
[gitlab]
2021-01-29 00:20:46 +05:30
repository = https://gitlab.example.com/api/v4/projects/<project_id>/packages/pypi
2021-04-17 20:07:23 +05:30
username = <your_personal_access_token_name>
password = <your_personal_access_token>
2020-04-22 19:07:51 +05:30
```
2021-01-29 00:20:46 +05:30
- Your project ID is on your project's home page.
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
### Authenticate with a deploy token
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
To authenticate with a deploy token, edit your `~/.pypirc` file and add:
2020-05-24 23:13:21 +05:30
```ini
[distutils]
index-servers =
gitlab
[gitlab]
2021-01-29 00:20:46 +05:30
repository = https://gitlab.example.com/api/v4/projects/<project_id>/packages/pypi
2020-05-24 23:13:21 +05:30
username = <deploy token username>
password = <deploy token>
```
2021-01-29 00:20:46 +05:30
Your project ID is on your project's home page.
2020-04-22 19:07:51 +05:30
2021-01-29 00:20:46 +05:30
### Authenticate with a CI job token
2020-04-22 19:07:51 +05:30
2021-01-29 00:20:46 +05:30
> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/202012) in GitLab 13.4.
To work with PyPI commands within [GitLab CI/CD](../../../ci/README.md), you
can use `CI_JOB_TOKEN` instead of a personal access token or deploy token.
For example:
```yaml
image: python:latest
run:
script:
- pip install twine
- python setup.py sdist bdist_wheel
- TWINE_PASSWORD=${CI_JOB_TOKEN} TWINE_USERNAME=gitlab-ci-token python -m twine upload --repository-url https://gitlab.example.com/api/v4/projects/${CI_PROJECT_ID}/packages/pypi dist/*
```
You can also use `CI_JOB_TOKEN` in a `~/.pypirc` file that you check in to
GitLab:
```ini
[distutils]
index-servers =
gitlab
[gitlab]
repository = https://gitlab.example.com/api/v4/projects/${env.CI_PROJECT_ID}/packages/pypi
username = gitlab-ci-token
password = ${env.CI_JOB_TOKEN}
```
## Publish a PyPI package
2021-03-08 18:12:59 +05:30
Prerequisites:
2021-01-29 00:20:46 +05:30
2021-03-08 18:12:59 +05:30
- You must [authenticate with the Package Registry](#authenticate-with-the-package-registry).
- Your [version string must be valid](#ensure-your-version-string-is-valid).
- The maximum allowed package size is 5 GB.
2021-01-29 00:20:46 +05:30
- You can't upload the same version of a package multiple times. If you try,
2021-03-08 18:12:59 +05:30
you receive the error `400 Bad Request`.
- You cannot publish PyPI packages to a group, only to a project.
You can then [publish a package by using twine](#publish-a-pypi-package-by-using-twine).
2020-11-24 15:15:51 +05:30
### Ensure your version string is valid
2021-02-22 17:27:13 +05:30
If your version string (for example, `0.0.1`) isn't valid, it gets rejected.
2021-01-29 00:20:46 +05:30
GitLab uses the following regex to validate the version string.
2020-11-24 15:15:51 +05:30
```ruby
\A(?:
v?
(?:([0-9]+)!)? (?# epoch)
([0-9]+(?:\.[0-9]+)*) (?# release segment)
([-_\.]?((a|b|c|rc|alpha|beta|pre|preview))[-_\.]?([0-9]+)?)? (?# pre-release)
((?:-([0-9]+))|(?:[-_\.]?(post|rev|r)[-_\.]?([0-9]+)?))? (?# post release)
([-_\.]?(dev)[-_\.]?([0-9]+)?)? (?# dev release)
(?:\+([a-z0-9]+(?:[-_\.][a-z0-9]+)*))? (?# local version)
)\z}xi
```
2021-01-29 00:20:46 +05:30
You can experiment with the regex and try your version strings by using this
[regular expression editor](https://rubular.com/r/FKM6d07ouoDaFV).
2020-11-24 15:15:51 +05:30
2021-01-29 00:20:46 +05:30
For more details about the regex, review this [documentation](https://www.python.org/dev/peps/pep-0440/#appendix-b-parsing-version-strings-with-regular-expressions).
2020-04-22 19:07:51 +05:30
2021-01-29 00:20:46 +05:30
### Publish a PyPI package by using twine
2020-04-22 19:07:51 +05:30
2021-01-29 00:20:46 +05:30
To publish a PyPI package, run a command like:
2020-05-24 23:13:21 +05:30
```shell
python3 -m twine upload --repository gitlab dist/*
```
2021-01-29 00:20:46 +05:30
This message indicates that the package was published successfully:
2020-05-24 23:13:21 +05:30
```plaintext
2021-01-29 00:20:46 +05:30
Uploading distributions to https://gitlab.example.com/api/v4/projects/<your_project_id>/packages/pypi
2020-05-24 23:13:21 +05:30
Uploading mypypipackage-0.0.1-py3-none-any.whl
100%|███████████████████████████████████████████████████████████████████████████████████████████| 4.58k/4.58k [00:00<00:00, 10.9kB/s]
Uploading mypypipackage-0.0.1.tar.gz
100%|███████████████████████████████████████████████████████████████████████████████████████████| 4.24k/4.24k [00:00<00:00, 11.0kB/s]
```
2021-01-29 00:20:46 +05:30
To view the published package, go to your project's **Packages & Registries**
page.
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
If you didn't use a `.pypirc` file to define your repository source, you can
publish to the repository with the authentication inline:
2020-11-24 15:15:51 +05:30
```shell
2021-01-29 00:20:46 +05:30
TWINE_PASSWORD=<personal_access_token or deploy_token> TWINE_USERNAME=<username or deploy_token_username> python3 -m twine upload --repository-url https://gitlab.example.com/api/v4/projects/<project_id>/packages/pypi dist/*
2020-11-24 15:15:51 +05:30
```
2021-01-29 00:20:46 +05:30
If you didn't follow the steps on this page, ensure your package was properly
built, and that you [created a PyPI package with `setuptools`](https://packaging.python.org/tutorials/packaging-projects/).
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
You can then upload your package by using the following command:
2020-04-22 19:07:51 +05:30
```shell
python -m twine upload --repository <source_name> dist/<package_file>
```
- `<package_file>` is your package filename, ending in `.tar.gz` or `.whl`.
2021-01-29 00:20:46 +05:30
- `<source_name>` is the [source name used during setup](#authenticate-with-the-package-registry).
2020-04-22 19:07:51 +05:30
2021-03-08 18:12:59 +05:30
### Publishing packages with the same name or version
You cannot publish a package if a package of the same name and version already exists.
You must delete the existing package first. If you attempt to publish the same package
more than once, a `404 Bad Request` error occurs.
2021-01-29 00:20:46 +05:30
## Install a PyPI package
2020-04-22 19:07:51 +05:30
2021-01-29 00:20:46 +05:30
To install the latest version of a package, use the following command:
2020-04-22 19:07:51 +05:30
```shell
2021-04-17 20:07:23 +05:30
pip install --index-url https://<personal_access_token_name>:<personal_access_token>@gitlab.example.com/api/v4/projects/<project_id>/packages/pypi/simple --no-deps <package_name>
2020-04-22 19:07:51 +05:30
```
- `<package_name>` is the package name.
2021-04-17 20:07:23 +05:30
- `<personal_access_token_name>` is a personal access token name with the `read_api` scope.
2020-05-24 23:13:21 +05:30
- `<personal_access_token>` is a personal access token with the `read_api` scope.
- `<project_id>` is the project ID.
2021-04-17 20:07:23 +05:30
In these commands, you can use `--extra-index-url` instead of `--index-url`. However, using
`--extra-index-url` makes you vulnerable to dependency confusion attacks because it checks the PyPi
repository for the package before it checks the custom repository. `--extra-index-url` adds the
provided URL as an additional registry which the client checks if the package is present.
`--index-url` tells the client to check for the package on the provided URL only.
2021-01-29 00:20:46 +05:30
If you were following the guide and want to install the
`MyPyPiPackage` package, you can run:
2020-05-24 23:13:21 +05:30
```shell
2021-04-17 20:07:23 +05:30
pip install mypypipackage --no-deps --index-url https://<personal_access_token_name>:<personal_access_token>@gitlab.example.com/api/v4/projects/<your_project_id>/packages/pypi/simple
2020-05-24 23:13:21 +05:30
```
2021-01-29 00:20:46 +05:30
This message indicates that the package was installed successfully:
2020-05-24 23:13:21 +05:30
```plaintext
2021-04-17 20:07:23 +05:30
Looking in indexes: https://<personal_access_token_name>:****@gitlab.example.com/api/v4/projects/<your_project_id>/packages/pypi/simple
2020-05-24 23:13:21 +05:30
Collecting mypypipackage
2021-01-29 00:20:46 +05:30
Downloading https://gitlab.example.com/api/v4/projects/<your_project_id>/packages/pypi/files/d53334205552a355fee8ca35a164512ef7334f33d309e60240d57073ee4386e6/mypypipackage-0.0.1-py3-none-any.whl (1.6 kB)
2020-05-24 23:13:21 +05:30
Installing collected packages: mypypipackage
Successfully installed mypypipackage-0.0.1
```
2020-11-24 15:15:51 +05:30
2021-01-29 00:20:46 +05:30
### Package names
2020-11-24 15:15:51 +05:30
2021-01-29 00:20:46 +05:30
GitLab looks for packages that use
[Python normalized names (PEP-503)](https://www.python.org/dev/peps/pep-0503/#normalized-names).
The characters `-`, `_`, and `.` are all treated the same, and repeated
characters are removed.
2020-11-24 15:15:51 +05:30
2021-01-29 00:20:46 +05:30
A `pip install` request for `my.package` looks for packages that match any of
the three characters, such as `my-package`, `my_package`, and `my....package`.