打包和釋出Python程式碼?


Python提供了一種非常簡單的建立或釋出包的方法。

Python中的包管理可以透過不同的工具實現:

  • Pip- 它仍然是首選工具之一,因為它幾乎消除了軟體包在作業系統中的任何手動安裝和更新。它管理完整的包列表及其對應的版本號,這有助於在不同的獨立環境中精確複製整個包組。

  • Python包索引(PPI)是一個公共的包儲存庫,其中包含使用者提交的包,可以使用pip安裝,例如:pip install package_name。

以下是關於如何上傳包的分步過程。

步驟1:準備好要上傳的包

我假設你已經準備好要釋出的包。如果你沒有,請按照以下步驟建立Python包或模組,好訊息是它非常簡單:

  • 建立一個包含你的程式碼的Python檔案,將其命名為myfirstPackage.py或myPackageName.py。這是一個模組,一個包含資料的檔案(myfirstPackage.py)。我們可以匯入它或做任何我們想做的事情。

  • 將其製作成一個包
Just add an empty __init__.py file to it.

echo >> __init__.py

or use touch command

touch __init_.py


$dir
Volume in drive C has no label.
Volume Serial Number is 8CD6-8D39

Directory of c:\Python\Python361\firstPackage

08-04-2019 05.44 PM <DIR> .
08-04-2019 05.44 PM <DIR> ..
08-04-2019 02.25 PM 47 myFirstPackage.py
08-04-2019 05.44 PM 13 __init__.py

你可以看到上面兩個檔案都在firstPackage目錄中。

就是這樣,擁有一個包含這兩個檔案(__init__.py和myfirstPackage.py)的目錄被稱為一個包(myHelloModule)。

打包你的專案

首先,克隆示例專案,並將其命名為你的模組名:

git clone https://github.com/pypa/sampleproject firstPackage

重要的檔案包括:

  • Setup.py – 它允許我們指定專案的配置,並執行打包命令:例如,嘗試這個命令:python setup.py --help

  • Setup.cfg是一個INI檔案,包含setup.py命令的選項預設值。

  • README.rst使用reStructuredText描述了專案的目標。

將你的模組複製到這個新資料夾中,並刪除現有的“sample”模組。

└───firstPackage
│ LICENSE.txt
│ MANIFEST.in
│ myFirstPackage.py
│ README.md
│ setup.cfg
│ setup.py
│ tox.ini
│ __init__.py

配置名稱、版本、描述

編輯setup.py以包含有關你的Python包的基本資訊:

setup.py

import setuptools

with open("README.md", "r") as fh:
long_description = fh.read()

setuptools.setup(
   name="firstPackage",
   version="0.0.1",
   author="Rajesh Joshi",
   author_email="callraj.joshi@gmail.com",
   description="my First Package",
   long_description=long_description,
   long_description_content_type="text/markdown",
   url="https://github.com/pypa/sampleproject",
   packages=setuptools.find_packages(),
   classifiers=[
      "Programming Language :: Python :: 3",
      "License :: OSI Approved :: MIT License",
      "Operating System :: OS Independent",
   ],
)

你的許可證檔案將類似於:

MIT License

Copyright (c) [2019] [firstPackage]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

.對於README

## firstPackage
This is a sample package to learn the steps of creating and publishing package.

實際的打包步驟

在環境中安裝或更新setuptools和wheel包。

>pip install wheel twine setuptools –upgrade

首先,建立一個原始碼分發版。這種型別的“分發版”(即包)在pip安裝時需要一個構建步驟。

>python setup.py sdist

現在我們要安裝一個“wheel”(一個已構建的包),它比原始碼分發版安裝速度更快。

>python setup.py bdist_wheel

希望包應該已經構建完成,你可以在firstpackage資料夾中的dist目錄中看到包的壓縮檔案,位於setup.py檔案旁邊。

上傳你的包

現在在你的選擇位置建立一個新的虛擬環境並激活它,如下所示:

c:\Users\rajesh>virtualenv myPackage
Using base prefix 'c:\python\python361'
New python executable in c:\Users\rajesh\myPackage\Scripts\python.exe
Installing setuptools, pip, wheel...done.

c:\Users\rajesh\myPackage>.\Scripts\activate

(myPackage) c:\Users\rajesh\myPackage>

將上面建立的zip檔案複製到你的新環境中。

>pip install firstPackage-0.0.1.tar.gz

要驗證你的包是否已安裝在你的活動環境中,只需執行pip list即可顯示當前環境中所有包的列表。

>pip list
Package      Version
------------ -------
firstPackage  0.0.1
pip           19.0.3
setuptools    41.0.0
wheel         0.33.1

現在是時候將包釋出到PyPI了,這樣它就可以公開訪問了。

首先轉到setup.py所在的路徑,然後安裝或更新twin包。

>pip install --upgrade twine

最後,透過twin將你的包釋出到PyPI,

>twine upload dist/*
Enter your username: callraj.joshi
Enter your password:
Uploading distributions to https://upload.pypi.org/legacy/
Uploading firstPackage-0.0.1-py2.py3-none-any.whl
…

上面我們只需要輸入使用者名稱和密碼,然後它就開始上傳我們的包了。

更新於:2019年7月30日

190次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告