ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# GitLab PyPi Repository > 原文:[https://docs.gitlab.com/ee/user/packages/pypi_repository/](https://docs.gitlab.com/ee/user/packages/pypi_repository/) * [Setting up your development environment](#setting-up-your-development-environment) * [Enabling the PyPi Repository](#enabling-the-pypi-repository) * [Getting started](#getting-started) * [Create a project](#create-a-project) * [Create a package](#create-a-package) * [Adding the GitLab PyPi Repository as a source](#adding-the-gitlab-pypi-repository-as-a-source) * [Authenticating with a personal access token](#authenticating-with-a-personal-access-token) * [Authenticating with a deploy token](#authenticating-with-a-deploy-token) * [Uploading packages](#uploading-packages) * [Upload packages with Twine](#upload-packages-with-twine) * [Install packages](#install-packages) # GitLab PyPi Repository[](#gitlab-pypi-repository-premium "Permalink") [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/208747) in [GitLab Premium](https://about.gitlab.com/pricing/) 12.10. 使用 GitLab PyPi 信息库,每个项目都可以有自己的空间来存储 PyPi 软件包. GitLab PyPi 存储库可用于: * [pip](https://s0pypi0org.icopy.site/project/pip/) * [twine](https://s0pypi0org.icopy.site/project/twine/) ## Setting up your development environment[](#setting-up-your-development-environment "Permalink") 您将需要最新版本的[pip](https://s0pypi0org.icopy.site/project/pip/)和[twine](https://s0pypi0org.icopy.site/project/twine/) . ## Enabling the PyPi Repository[](#enabling-the-pypi-repository "Permalink") **注意:**仅当您的 GitLab 管理员[启用了对 Package Registry 的支持时,](../../../administration/packages/index.html)此选项才可用. 启用 PyPi 储存库后,默认情况下它将可用于所有新项目. 要为现有项目启用它,或者要禁用它: 1. 导航到项目的**"设置">"常规">"可见性,项目功能,权限"** . 2. 找到"软件包"功能并启用或禁用它. 3. 单击**保存更改**以使更改生效. 然后,您应该能够在左侧栏中看到" **程序包和注册表"**部分. ## Getting started[](#getting-started "Permalink") 本节将介绍如何创建一个新的示例 PyPi 包进行上传. 这是测试**GitLab PyPi Registry**的快速入门. 如果您已经了解如何构建和发布自己的软件包,请继续[下一节](#adding-the-gitlab-pypi-repository-as-a-source) . ### Create a project[](#create-a-project "Permalink") 了解如何创建完整的 Python 项目不在本指南的范围内,但是您可以创建一个小包来测试注册表. 首先创建一个名为`MyPyPiPackage`的新目录: ``` mkdir MyPyPiPackage && cd MyPyPiPackage ``` 创建此目录后,在其中创建另一个目录: ``` mkdir mypypipackage && cd mypypipackage ``` 在此目录中创建两个新文件以设置基本项目: ``` touch __init__.py touch greet.py ``` 在`greet.py` ,添加以下代码: ``` def SayHello(): print("Hello from MyPyPiPackage") return ``` 在`__init__.py`文件中,添加以下内容: ``` from .greet import SayHello ``` 现在,我们的项目基础已经完成,我们可以测试代码是否运行. 在顶部`MyPyPiPackage`目录中启动 Python 提示符. 然后运行: ``` >>> from mypypipackage import SayHello >>> SayHello() ``` 您应该看到类似于以下内容的输出: ``` 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[](#create-a-package "Permalink") 在您的`MyPyPiPackage`目录中,我们需要创建一个`setup.py`文件. 运行以下命令: ``` touch setup.py ``` 该文件包含有关我们软件包的所有信息. 有关此文件的更多信息,请参见[创建 setup.py](https://packaging.python.org/tutorials/packaging-projects/#creating-setup-py) . 对于本指南,我们不需要大量填写此文件,只需将以下内容添加到您的`setup.py` : ``` import setuptools 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', ) ``` 保存文件,然后执行如下设置: ``` python3 setup.py sdist bdist_wheel ``` 如果成功,您应该能够在新创建的`dist`文件夹中看到输出. 跑: ``` ls dist ``` 并确认您的输出与以下内容匹配: ``` mypypipackage-0.0.1-py3-none-any.whl mypypipackage-0.0.1.tar.gz ``` 现在,我们的软件包已全部设置好,可以上传到**GitLab PyPi 软件包注册中心** . 在此之前,我们接下来需要设置身份验证. ## Adding the GitLab PyPi Repository as a source[](#adding-the-gitlab-pypi-repository-as-a-source "Permalink") ### Authenticating with a personal access token[](#authenticating-with-a-personal-access-token "Permalink") 您将需要以下内容: * 个人访问令牌. 您可以生成范围设置为`api`的[个人访问令牌](../../../user/profile/personal_access_tokens.html) ,以进行存储库身份验证. * 您的来源的合适名称. * 您的项目 ID,可以在项目的主页上找到. 编辑`~/.pypirc`文件并添加以下内容: ``` [distutils] index-servers = gitlab [gitlab] repository = https://gitlab.com/api/v4/projects/<project_id>/packages/pypi username = __token__ password = <your personal access token> ``` ### Authenticating with a deploy token[](#authenticating-with-a-deploy-token "Permalink") 您将需要以下内容: * 部署令牌. 您可以生成[令牌部署](./../../project/deploy_tokens/index.html)与`read_package_registry`和/或`write_package_registry`范围为存储库认证. * 您的来源的合适名称. * 您的项目 ID,可以在项目的主页上找到. 编辑`~/.pypirc`文件并添加以下内容: ``` [distutils] index-servers = gitlab [gitlab] repository = https://gitlab.com/api/v4/projects/<project_id>/packages/pypi username = <deploy token username> password = <deploy token> ``` ## Uploading packages[](#uploading-packages "Permalink") 上载套件时,请注意: * 允许的最大大小为 50 MB. * 如果您多次上传具有相同版本的相同程序包,则每次连续上传都将保存为单独的文件. 安装软件包时,GitLab 将提供最新文件. ### Upload packages with Twine[](#upload-packages-with-twine "Permalink") 如果您按照上面的指南进行操作,则`MyPyPiPackage`软件包应已准备好上载. 运行以下命令: ``` python3 -m twine upload --repository gitlab dist/* ``` 如果成功,您应该看到以下内容: ``` Uploading distributions to https://gitlab.com/api/v4/projects/<your_project_id>/packages/pypi 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] ``` 这表明该软件包已成功上传. 然后,您可以导航到项目的" **程序包和注册表"**页面,并查看上载的程序包. 如果您没有遵循上述指南,则需要确保已正确构建[软件包,并使用 setuptools 创建了 PyPi 软件包](https://packaging.python.org/tutorials/packaging-projects/) . 然后,您可以使用以下命令上传软件包: ``` python -m twine upload --repository <source_name> dist/<package_file> ``` Where: * `<package_file>` is your package filename, ending in `.tar.gz` or `.whl`. * `<source_name>`是[安装过程中使用](#adding-the-gitlab-pypi-repository-as-a-source)的[源名称](#adding-the-gitlab-pypi-repository-as-a-source) . ## Install packages[](#install-packages "Permalink") 使用以下命令安装最新版本的软件包: ``` pip install --index-url https://__token__:<personal_access_token>@gitlab.com/api/v4/projects/<project_id>/packages/pypi/simple --no-deps <package_name> ``` Where: * `<package_name>`是软件包名称. * `<personal_access_token>`是具有`read_api`范围的个人访问令牌. * `<project_id>`是项目 ID. 如果您遵循上面的指南,并且想测试安装`MyPyPiPackage`软件包,则可以运行以下命令: ``` pip install mypypipackage --no-deps --index-url https://__token__:<personal_access_token>@gitlab.com/api/v4/projects/<your_project_id>/packages/pypi/simple ``` 这将导致以下结果: ``` Looking in indexes: https://__token__:****@gitlab.com/api/v4/projects/<your_project_id>/packages/pypi/simple Collecting mypypipackage Downloading https://gitlab.com/api/v4/projects/<your_project_id>/packages/pypi/files/d53334205552a355fee8ca35a164512ef7334f33d309e60240d57073ee4386e6/mypypipackage-0.0.1-py3-none-any.whl (1.6 kB) Installing collected packages: mypypipackage Successfully installed mypypipackage-0.0.1 ```