Wiki source code of Pushing PyPI Packages to Gitea
Last modified by DevOps-as-a-Service Operator on 2025/02/05 11:33
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | This step-by-step guide outlines how to push your Python packages to a Gitea registry using Twine. | ||
2 | |||
3 | {{toc/}} | ||
4 | |||
5 | = Requirements = | ||
6 | |||
7 | * To work with the PyPI package registry, you need to use the package [[pip>>url:https://pypi.org/project/pip/||shape="rect"]] to consume and [[twine>>url:https://pypi.org/project/twine/||shape="rect"]] to publish packages. | ||
8 | * To push packages, a user with read-write permissions is required. This user can either be a technical user or a human user. | ||
9 | * For PyPI packages, authentication can be achieved using either a password or a token. However, for npm packages, pushing to the registry is only possible with a token. | ||
10 | |||
11 | == (% style="color:#0d0d0d" %)Generating a Token(%%) == | ||
12 | |||
13 | (% style="color:#0d0d0d" %)To push packages to Gitea, you must have an access token with read and write permissions. The [[doc:Gitea.Creating Access Tokens for Pushing Packages to Gitea.WebHome]] documentation guides you on how to create such a token. | ||
14 | |||
15 | ---- | ||
16 | |||
17 | = Guide = | ||
18 | |||
19 | == Configuration == | ||
20 | |||
21 | For configuring {{code language="none"}}.pypirc{{/code}} in Windows or Linux: | ||
22 | |||
23 | 1. **Open/Create {{code language="none"}}.pypirc{{/code}}** file: | ||
24 | In Windows, navigate to {{code language="none"}}C:\Users\YourUsername{{/code}} or in Linux to ~~, and create or open the .pypirc file. | ||
25 | |||
26 | 1. **Edit .pypirc Content**: | ||
27 | Add the {{code language="none"}}[distutils]{{/code}} section to declare gitea as an index server. | ||
28 | In the {{code language="none"}}[gitea]{{/code}} section specify your Gitea repository URL, replacing the placeholders {{code language="none"}}{pkey}{{/code}}, {{code language="none"}}{username}{{/code}} and {{code language="none"}}{password}{{/code}} with actual values.((( | ||
29 | {{code language="python"}} | ||
30 | [distutils] | ||
31 | index-servers = gitea | ||
32 | |||
33 | [gitea] | ||
34 | repository = https://gitea-CUSTOMER.devops.t-systems.net/api/packages/{pkey}/pypi | ||
35 | username = {username} | ||
36 | password = {password} | ||
37 | {{/code}} | ||
38 | |||
39 | |=Placeholder|=Description | ||
40 | |pkey|Project Key in DevOps Portal, refers to Organization in Gitea | ||
41 | |username|Your Gitea username or the name of the technical user | ||
42 | |password|((( | ||
43 | Your password or access token. | ||
44 | |||
45 | A token has to be used if 2FA is configured for the user. | ||
46 | Refer to [[Creating Access Tokens for Pushing Packages to Gitea>>doc:Gitea.Creating Access Tokens for Pushing Packages to Gitea.WebHome]] for instructions. | ||
47 | ))) | ||
48 | |||
49 | {{warning}} | ||
50 | A package always belongs to an organization, not a repository. To link an (already uploaded) package to a repository, open the settings page on that package and choose a repository to link this package to. The entire package will be linked, not just a single version. | ||
51 | {{/warning}} | ||
52 | ))) | ||
53 | 1. **Save .pypirc**: | ||
54 | Ensure no file extension like .txt when saving. In Linux, use a text editor like nano or vim; in Windows, Notepad can be used, ensuring to select "All Files" and type the filename as .pypirc. | ||
55 | |||
56 | == Publish a package == | ||
57 | |||
58 | To publish a package with Twine, first identify the path where your package files (.tar.gz and .whl) are located, typically in the dist/ directory created after building your package. | ||
59 | |||
60 | |||
61 | (% class="box infomessage" %) | ||
62 | ((( | ||
63 | python3 -m twine upload ~-~-repository gitea /path/to/files/* | ||
64 | ))) | ||
65 | |||
66 | (% class="box warningmessage" %) | ||
67 | ((( | ||
68 | Replacing /path/to/files/* with the actual path to your package files. | ||
69 | ))) | ||
70 | |||
71 | {{warning}} | ||
72 | If a package of the same name and version already exists, you'll need to delete the existing one before publishing the new version. | ||
73 | {{/warning}} | ||
74 | |||
75 | == Result == | ||
76 | |||
77 | Once published, the package will be accessible within the**// "Packages" //**section of the organization in Gitea. | ||
78 | The following picture displays how the// "requests"// package appears in Gitea after being pushed to the package registry. | ||
79 | |||
80 | [[image:image-2024-2-9_9-18-18.png||data-xwiki-image-style-border="true"]] |