github和pypi_如何将GitHub用作PyPi服务器

github和pypi

I was looking for a hosted private PyPi Python Package server, that used credentials that the team already has (such as GitHub).

我正在寻找一个托管的私有PyPi Python Package服务器,该服务器使用了团队已经拥有的凭据(例如GitHub)。

I didn’t want to create an on-premises server. For us, it would make it impossible to use cloud-based build servers, and it is another moving part that can go wrong. There are also potential issues with fine-grained security and speed. (We have a worldwide team, so serving the content via a CDN would be helpful.)

我不想创建本地服务器。 对我们来说,这将使使用基于云的构建服务器成为不可能,这是另一个可能出错的动向。 细粒度的安全性和速度也存在潜在的问题。 (我们拥有一支遍布全球的团队,因此通过CDN提供内容会很有帮助。)

I didn’t want to force the team to create accounts with another provider. They already have Active Directory and GitHub accounts. It is an annoyance for them and creates a governance burden for me.

我不想强迫团队与其他提供商创建帐户。 他们已经有Active Directory和GitHub帐户。 这对他们来说很烦人,给我带来了管理负担。

Sadly, I couldn’t find such a service. GemFury is excellent but doesn't support GitHub authorization (at the team / organisation level) and Packagr doesn’t support GitHub authorisation at all. MyGet is also excellent, it does allow me to use GitHub authorization, but doesn’t host Python packages. Azure DevOps has something that looks promising, but it’s in private beta at the moment.

可悲的是,我找不到这样的服务。 GemFury非常出色,但不支持GitHub授权(在团队/组织级别),而Packagr根本不支持GitHub授权。 MyGet也很出色,它确实允许我使用GitHub授权,但不托管Python包。 Azure DevOps看起来很有前途,但目前处于私人Beta版

Happily, this is possible using cloud Git repositories such as GitHub, GitLab and BitBucket.

幸运的是,这可以通过使用GitHub,GitLab和BitBucket等云Git存储库来实现。

Pip可以从Git安装软件包 (Pip can install packages from Git)

I have hosted a Python package on GitHub (python_world), which you can install with the following command (make sure you trust me before running this command and installing my code on your computer).

我已经在GitHub( python_world )上托管了一个Python软件包,您可以使用以下命令进行安装(在运行此命令并将代码安装到您的计算机之前,请确保我信任我)。

pip install git+https://github.com/ceddlyburge/python_world#egg=python_world

pip install git+https://github.com/ceddlyburge/python_world#egg=python_world

Pip provides options to install from head, from a branch, from a tag or from a commit. I usually tag each release and install from these tags. See the pip install documentation for full details.

Pip提供了从头,分支,标签或提交安装的选项。 我通常标记每个发行版并从这些标记安装。 有关完整的详细信息,请参见pip安装文档

This repository is public, but it works just the same with a private repo, as long as you have permission. There is no special magic (it's a vanilla Python package) and Setup.py does most of the work as normal.

该存储库是公共的,但是只要您具有许可,它就可以与私有存储库相同。 没有特殊的魔术(这是一个香草的Python包), Setup.py正常执行大部分工作。

If you are new to creating Python Packages, the Packaging Python Projects tutorial is worth a quick read.

如果您不熟悉创建Python软件包,那么值得快速阅读Packaging Python Projects教程

Setuptools还可以从Git安装依赖项 (Setuptools can also install dependencies from Git)

Setuptools is how most people create Python packages.

Setuptools是大多数人创建Python软件包的方式。

I have hosted another package on GitHub python_hello, which depends on python_world. (I’m sure you can see where this is going.)

我在GitHub python_hello上托管了另一个软件包,该软件包取决于python_world 。 (我确定您可以看到前进的方向。)

The relevant bits from setup.py are below. install_requires specifies that python_world is a required dependency and tells Setuptools where to find it.

setup.py的相关位如下。 install_requires指定python_world是必需的依赖项,并告诉Setuptools在哪里找到它。

install_requires=[
	'python_world@git+https://github.com/ceddlyburge/python_world#egg=python_world-0.0.1',
]

You can install this package using the command below. It will also download the dependent python_world package.

您可以使用以下命令安装此软件包。 还将下载相关的python_world软件包。

pip install git+https://github.com/ceddlyburge/python_hello#egg=python_hello

pip install git+https://github.com/ceddlyburge/python_hello#egg=python_hello

This links to a specific version of python_world, which is a shame as it means that pip can't do any dependency management (such as working out an acceptable version if multiple things are reliant on it). However, by the end of this article, we will have removed the need for the specific link.

这链接到特定版本的python_world ,这很可惜,因为这意味着pip无法执行任何依赖项管理(例如,如果有多个依赖项,请制定一个可接受的版本)。 但是,到本文结尾,我们将不再需要特定的链接。

Python环境 (Python environments)

As everyone who has used Python without an environment knows, environments save a lot of frustration and wasted time. So we need to support those.

众所周知,在没有环境的情况下使用Python的每个人都知道,环境可以节省很多挫败感和时间。 因此,我们需要支持这些。

I have created a repo (use-hello-world) that defines python_hello as a dependency in requirements.txt for Virtualenv, and environment.yml for Conda.

我创建了一个回购( 使用问候世界 ),它定义python_hello作为一个依赖requirements.txtVIRTUALENV ,并environment.ymlconda

If you download the repo, you can install the dependencies into a virtualenv with the following command.

如果下载存储库,则可以使用以下命令将依赖项安装到virtualenv中。

pip install -r requirements.txt

pip install -r requirements.txt

If you are using conda you can use this command:

如果您使用的是conda,则可以使用以下命令:

conda env create -n use-hello-world

conda env create -n use-hello-world

PyPi索引 (PyPi Index)

So far we are able to install packages from our private Git repositories. These packages can, in turn, define dependencies to other private repositories. There still isn’t a PyPi server in sight.

到目前为止,我们已经能够从我们的私有Git存储库中安装软件包。 这些软件包可以依次定义对其他私有存储库的依赖关系。 仍然没有PyPi服务器。

We could stop at this point. However, the syntax for defining dependencies is a bit mysterious. It would be difficult for the team to discover which packages are available, and we are linking to specific versions of dependent packages, instead of letting pip manage it.

我们可以在这一点上停止。 但是,定义依赖项的语法有点神秘。 对于团队来说,很难发现可用的软件包,并且我们要链接到依赖软件包的特定版本,而不是让pip管理它。

To fix this we can set up a PyPi index that conforms to Pep 503. This specification is quite simple, and I have just created the index by hand. If this becomes too cumbersome I can generate it from the GitHub API.

为了解决这个问题,我们可以设置一个符合Pep 503的PyPi索引。 该规范非常简单,我刚刚手工创建了索引。 如果这太麻烦了,我可以从GitHub API生成它。

I created this PyPi Index using GitHub Pages. There are equivalent things for GitLab and BitBucket. You can see that the source code is very simple. GitHub Pages sites are always public (and there is probably no sensitive information in your index). However, if you need them to be private you can use a service such as PrivateHub.

我使用GitHub Pages创建了这个PyPi索引 。 GitLab和BitBucket具有等效的功能。 您可以看到源代码非常简单。 GitHub Pages站点始终是公共的(索引中可能没有敏感信息)。 但是,如果您需要将它们设为私有,则可以使用PrivateHub之类的服务。

One thing to look out for is the name normalisation of the specification. This requires the python_hello package information to be present at python-hello/index.html (note the change from an underscore to a dash).

要注意的一件事是规范名称标准化 。 这要求在python-hello/index.html提供python_hello包信息(注意从下划线变为破折号)。

Now that we have a PyPi server, we can install packages using the command below.

现在我们有了一个PyPi服务器,我们可以使用下面的命令安装软件包。

pip install python_hello --extra-index-url https://ceddlyburge.github.io/python-package-server/

pip install python_hello --extra-index-url https://ceddlyburge.github.io/python-package-server/

So that you can see this working with environments, I have created another repo (use_hello_world_from_server) that defines the python_hello dependency using this PyPi index instead of direct GitHub Links. If you are trying it with Conda, version >4.4 is required.

为了让您可以在环境中正常工作,我创建了另一个存储库( use_hello_world_from_server ),该python_hello使用此PyPi索引而不是直接的GitHub链接定义了python_hello依赖项。 如果要在Conda上尝试,则需要版本> 4.4。

At this point, we can go back and remove the direct Git link in install_requires in setup.py of python_hello (as Setuptools will be able to find it from our server).

此时,我们可以返回并删除python_hello的setup.pyinstall_requires中的直接Git链接(因为Setuptools可以从我们的服务器中找到它)。

结论 (Conclusions)

Using a cloud-hosted Git provider as a PyPi server is a viable option. If you are already using one, that means that you can reuse the credentials and permissions that you already have. It will work with Cloud build servers and is likely to be provided via a CDN, so will be fast worldwide. It requires more knowledge to set up than a hosted server, but probably the same or less than hosting your own server on premises.

将云托管的Git提供程序用作PyPi服务器是可行的选择。 如果您已经在使用一个,则意味着您可以重用已经拥有的凭据和权限。 它可以与Cloud构建服务器一起使用,并且很可能通过CDN提供,因此在全球范围内将很快。 它需要比托管服务器更多的知识来进行设置,但可能与在本地托管您自己的服务器相同或更少。

提示和技巧 (Hints and tips)

Serving the index locally can help to troubleshoot problems (such as name normalization). It’s easy to see what requests are being made. You can use the inbuilt python HTTP server for this (python -m Http.Server -8000). This led me to find out that pip search uses postrequests, so won’t work with GitHub pages.

在本地提供索引可以帮助解决问题(例如名称标准化)。 很容易看到正在发出什么请求。 您可以为此使用内置的python HTTP服务器( python -m Http.Server -8000 )。 这使我发现pip search使用了post请求,因此不适用于GitHub页面。

You can run python setup.py -install to check your pip packages locally, before pushing them to Git.

您可以运行python setup.py -install在将它们推送到Git之前在本地检查您的pip软件包。

翻译自: https://www.freecodecamp.org/news/how-to-use-github-as-a-pypi-server-1c3b0d07db2/

github和pypi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值