溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么用Python將庫(kù)打包發(fā)布到pypi

發(fā)布時(shí)間:2021-04-14 10:00:23 來源:億速云 閱讀:195 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下怎么用Python將庫(kù)打包發(fā)布到pypi,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

1. 注冊(cè)pypi賬號(hào)并創(chuàng)建token

首先訪問https://pypi.org/ 并注冊(cè)賬號(hào)
然后跳轉(zhuǎn)到賬號(hào)設(shè)置

怎么用Python將庫(kù)打包發(fā)布到pypi

然后選擇API token->Add API token

怎么用Python將庫(kù)打包發(fā)布到pypi

輸入token name并在Scope中選擇Entire account(第一次需要選擇Entire account)

怎么用Python將庫(kù)打包發(fā)布到pypi

然后在本地,修改.pypirc文件
輸入的內(nèi)容為:

[pypi]
username = __token__
password = {token}

只需要修改{token}為自己的token即可

2. 編寫setup.py和setup.cfg

setup.cfg的內(nèi)容為

[metadata]
license_files = LICENSE.txt

LICENSE.txt是license文件,需要自行編寫
setup.py在根目錄下,一個(gè)示例為

from setuptools import setup
import compileall
from os import path
# 讀取readme文件,這樣可以直接顯示在主頁(yè)上
this_directory = path.abspath(path.dirname(__file__))
with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f:
    long_description = f.read()

compileall.compile_dir("src")

setup(
    name='my-python',
    version='1.0.2',
    packages=['src',
              'src.main',
              'src.main.config'],
    url='https://github.com/hTangle',
    license='Apache 2.0',
    author='hTangle',
    author_email='',
    description='',
    keywords='',
    python_requires='>=3.4, <4',
    long_description=long_description,
    long_description_content_type='text/markdown',
    install_requires=['requests']
)

具體的字段含義如下:

name: 包名

version: 版本號(hào),支持如下形式

1.2.0.dev1  # Development release
1.2.0a1     # Alpha Release
1.2.0b1     # Beta Release
1.2.0rc1    # Release Candidate
1.2.0       # Final Release
1.2.0.post1 # Post Release
15.10       # Date based release
23          # Serial release

description: 包描述,會(huì)放在如下圖所示的位置處

怎么用Python將庫(kù)打包發(fā)布到pypi

url: 包的鏈接,可以使用github鏈接,pypi會(huì)自動(dòng)獲取到倉(cāng)庫(kù)的信息,示例如下:


怎么用Python將庫(kù)打包發(fā)布到pypi

author: 作者

license: 許可證

classifiers: 分類,示例如下:

classifiers=[
    # How mature is this project? Common values are
    #   3 - Alpha
    #   4 - Beta
    #   5 - Production/Stable
    'Development Status :: 3 - Alpha',

    # Indicate who your project is intended for
    'Intended Audience :: Developers',
    'Topic :: Software Development :: Build Tools',

    # Pick your license as you wish (should match "license" above)
    'License :: OSI Approved :: MIT License',

    # Specify the Python versions you support here. In particular, ensure
    # that you indicate whether you support Python 2, Python 3 or both.
    'Programming Language :: Python :: 2',
    'Programming Language :: Python :: 2.7',
    'Programming Language :: Python :: 3',
    'Programming Language :: Python :: 3.6',
    'Programming Language :: Python :: 3.7',
    'Programming Language :: Python :: 3.8',
    'Programming Language :: Python :: 3.9',
],

keywords: 關(guān)鍵字,和論文的關(guān)鍵字類似

project_urls: 一些項(xiàng)目的其他鏈接,示例如下

project_urls={
    'Documentation': 'https://packaging.python.org/tutorials/distributing-packages/',
    'Funding': 'https://donate.pypi.org',
    'Say Thanks!': 'http://saythanks.io/to/example',
    'Source': 'https://github.com/pypa/sampleproject/',
    'Tracker': 'https://github.com/pypa/sampleproject/issues',
},

packages: 需要打包的目錄,需要以根目錄為起點(diǎn),可以使用

find_packages自動(dòng)查找包,注意不要漏寫

install_requires: 包依賴的其他包

python_requires: python的版本需求

package_data: 需要的額外的文件,例如包強(qiáng)依賴一個(gè)本地文件,可以使用如下

package_data={
    'sample': ['package_data.dat'],
},

3. 打包

打包命令為

python setup.py cmd

cmd可以取值為

bdist_wheel : create a wheel distribution

bdist_egg : create an “egg” distribution

sdist : create a source distribution (tarball, zip file, etc.)

bdist : create a built (binary) distribution

bdist_dumb : create a “dumb” built distribution

bdist_rpm : create an RPM distribution

bdist_wininst : create an executable installer for MS Windows

打包為tar.gz

python setup.py sdist

打包好的文件再dist目錄下

4. 上傳

可以首先使用twine對(duì)包進(jìn)行檢查

twine check dist/*

輸出如下

怎么用Python將庫(kù)打包發(fā)布到pypi

再運(yùn)行上傳命令

twine upload dist/*

以上是“怎么用Python將庫(kù)打包發(fā)布到pypi”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI