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服務(wù)器,該服務(wù)器使用了團(tuán)隊(duì)已經(jīng)擁有的憑據(jù)(例如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.)
我不想創(chuàng)建本地服務(wù)器。 對我們來說,這將使使用基于云的構(gòu)建服務(wù)器成為不可能,這是另一個可能出錯的動向。 細(xì)粒度的安全性和速度也存在潛在的問題。 (我們擁有一支遍布全球的團(tuán)隊(duì),因此通過CDN提供內(nèi)容會很有幫助。)
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.
我不想強(qiáng)迫團(tuán)隊(duì)與其他提供商創(chuàng)建帳戶。 他們已經(jīng)有Active Directory和GitHub帳戶。 這對他們來說很煩人,給我?guī)砹斯芾碡?fù)擔(dān)。
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.
可悲的是,我找不到這樣的服務(wù)。 GemFury非常出色,但不支持GitHub授權(quán)(在團(tuán)隊(duì)/組織級別),而Packagr根本不支持GitHub授權(quán)。 MyGet也很出色,它確實(shí)允許我使用GitHub授權(quán),但不托管Python包。 Azure DevOps看起來很有前途,但目前處于私人Beta版 。
Happily, this is possible using cloud Git repositories such as GitHub, GitLab and BitBucket.
幸運(yùn)的是,這可以通過使用GitHub,GitLab和BitBucket等云Git存儲庫來實(shí)現(xiàn)。
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).
我已經(jīng)在GitHub( python_world )上托管了一個Python軟件包,您可以使用以下命令進(jìn)行安裝(在運(yùn)行此命令并將代碼安裝到您的計(jì)算機(jī)之前,請確保我信任我)。
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提供了從頭,分支,標(biāo)簽或提交安裝的選項(xiàng)。 我通常標(biāo)記每個發(fā)行版并從這些標(biāo)記安裝。 有關(guān)完整的詳細(xì)信息,請參見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.
該存儲庫是公共的,但是只要您具有許可,它就可以與私有存儲庫相同。 沒有特殊的魔術(shù)(這是一個香草的Python包), Setup.py正常執(zhí)行大部分工作。
If you are new to creating Python Packages, the Packaging Python Projects tutorial is worth a quick read.
如果您不熟悉創(chuàng)建Python軟件包,那么值得快速閱讀Packaging Python Projects教程 。
Setuptools還可以從Git安裝依賴項(xiàng) (Setuptools can also install dependencies from Git)
Setuptools is how most people create Python packages.
Setuptools是大多數(shù)人創(chuàng)建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 。 (我確定您可以看到前進(jìn)的方向。)
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的相關(guān)位如下。 install_requires指定python_world是必需的依賴項(xiàng),并告訴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.
您可以使用以下命令安裝此軟件包。 還將下載相關(guān)的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 ,這很可惜,因?yàn)檫@意味著pip無法執(zhí)行任何依賴項(xiàng)管理(例如,如果有多個依賴項(xiàng),請制定一個可接受的版本)。 但是,到本文結(jié)尾,我們將不再需要特定的鏈接。
Python環(huán)境 (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.
眾所周知,在沒有環(huán)境的情況下使用Python的每個人都知道,環(huán)境可以節(jié)省很多挫敗感和時間。 因此,我們需要支持這些。
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.
我創(chuàng)建了一個回購( 使用問候世界 ),它定義python_hello作為一個依賴requirements.txt為VIRTUALENV ,并environment.yml為conda 。
If you download the repo, you can install the dependencies into a virtualenv with the following command.
如果下載存儲庫,則可以使用以下命令將依賴項(xiàng)安裝到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.
到目前為止,我們已經(jīng)能夠從我們的私有Git存儲庫中安裝軟件包。 這些軟件包可以依次定義對其他私有存儲庫的依賴關(guān)系。 仍然沒有PyPi服務(wù)器。
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.
我們可以在這一點(diǎn)上停止。 但是,定義依賴項(xiàng)的語法有點(diǎn)神秘。 對于團(tuán)隊(duì)來說,很難發(fā)現(xiàn)可用的軟件包,并且我們要鏈接到依賴軟件包的特定版本,而不是讓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.
為了解決這個問題,我們可以設(shè)置一個符合Pep 503的PyPi索引。 該規(guī)范非常簡單,我剛剛手工創(chuàng)建了索引。 如果這太麻煩了,我可以從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創(chuàng)建了這個PyPi索引 。 GitLab和BitBucket具有等效的功能。 您可以看到源代碼非常簡單。 GitHub Pages站點(diǎn)始終是公共的(索引中可能沒有敏感信息)。 但是,如果您需要將它們設(shè)為私有,則可以使用PrivateHub之類的服務(wù)。
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).
要注意的一件事是規(guī)范的名稱標(biāo)準(zhǔn)化 。 這要求在python-hello/index.html提供python_hello包信息(注意從下劃線變?yōu)槠普厶?。
Now that we have a PyPi server, we can install packages using the command below.
現(xiàn)在我們有了一個PyPi服務(wù)器,我們可以使用下面的命令安裝軟件包。
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.
為了讓您可以在環(huán)境中正常工作,我創(chuàng)建了另一個存儲庫( use_hello_world_from_server ),該python_hello使用此PyPi索引而不是直接的GitHub鏈接定義了python_hello依賴項(xiàng)。 如果要在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.py中install_requires中的直接Git鏈接(因?yàn)镾etuptools可以從我們的服務(wù)器中找到它)。
結(jié)論 (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服務(wù)器是可行的選擇。 如果您已經(jīng)在使用一個,則意味著您可以重用已經(jīng)擁有的憑據(jù)和權(quán)限。 它可以與Cloud構(gòu)建服務(wù)器一起使用,并且很可能通過CDN提供,因此在全球范圍內(nèi)將很快。 它需要比托管服務(wù)器更多的知識來進(jìn)行設(shè)置,但可能與在本地托管您自己的服務(wù)器相同或更少。
提示和技巧 (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.
在本地提供索引可以幫助解決問題(例如名稱標(biāo)準(zhǔn)化)。 很容易看到正在發(fā)出什么請求。 您可以為此使用內(nèi)置的python HTTP服務(wù)器( python -m Http.Server -8000 )。 這使我發(fā)現(xiàn)pip search使用了post請求,因此不適用于GitHub頁面。
You can run python setup.py -install to check your pip packages locally, before pushing them to Git.
您可以運(yùn)行python setup.py -install在將它們推送到Git之前在本地檢查您的pip軟件包。
翻譯自: https://www.freecodecamp.org/news/how-to-use-github-as-a-pypi-server-1c3b0d07db2/
github和pypi
總結(jié)
以上是生活随笔為你收集整理的github和pypi_如何将GitHub用作PyPi服务器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到喜欢的人三次意味着什么
- 下一篇: 石头剪刀布游戏web_Web开发教程-剪