283.md 9.6 KB
Newer Older
Lab机器人's avatar
readme  
Lab机器人 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
# 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 
```