記錄第一次製做pypi包的過程

準備工做

1.建立一個項目文件夾

mkdir dada_openapi_python
cd dada_openapi_python

2.建立包文件夾

在裏面在建立一個 dada_openapi_client 的文件夾,這個文件夾的名稱我故意建立的和上層目錄不同,以避免誤會,這個文件夾其實就是包名稱了html

mkdir dada_openapi_client
cd dada_openapi_client

3.編寫包代碼

根據各自的業務場景來,我下面列舉一個我編寫的
dada_clientpython

製做PyPI包

如今項目邏輯已經完成,那麼開始作 PyPI 的包了git

1.建立setup.py文件

dada_openapi_python文件夾中,建立配置文件setup.py,並填寫配置,下面貼出個人配置github

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/1/6 15:41
# @Author  : Weiqiang.long
# @Site    : 
# @File    : setup.py
# @Software: PyCharm
# @Description:

import setuptools

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

setuptools.setup(
    name = "dada_openapi_client",
    version = "1.0.3",
    author = "Weiqiang.long",
    description = "達達簽名數據封裝",
    long_description = long_description,
    long_description_content_type="text/markdown",
    url = "https://github.com/longweiqiang/dada_openapi_python",
    packages = setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ]

)

上面配置中的每一個字段具體含義,可參照官網文檔的2.8項說明segmentfault

打包

dada_openapi_python文件夾運行此命令

python setup.py sdist bdist_wheel

上傳

python -m twine upload dist/*
  • 成功上傳以下:
Uploading distributions to https://upload.pypi.org/legacy/
Uploading dada_openapi_client-1.0.3-py3-none-any.whl
100%|████████████████████████████████████████████████████
███████████████| 7.88k/7.88k [00:00<00:00, 10.6kB/s]
Uploading dada_openapi_client-1.0.3.tar.gz
100%|████████████████████████████████████████████████████
███████████████| 6.23k/6.23k [00:01<00:00, 4.43kB/s]

可能遇到的問題

Upload failed (403): Invalid or non-existent authentication information.

錯誤的用戶驗證信息,你須要建立一個用戶驗證文件 ~/.pypircapi

建立用戶驗證文件 ~/.pypirc

在本身的用戶目錄下新建一個空白文件命名爲.pypirc,內容以下:bash

[distutils]index-servers=pypi

[pypi]repository = https://upload.pypi.org/legacy/
username = XXX
password = XXX

Upload failed (403): You are not allowed to edit 'xxx' package information

你須要先註冊你的包才能夠開始上傳markdown

Server response (401): Incomplete registration; check your email

你的PyPI帳戶還沒完成郵箱驗證,你須要去註冊郵箱找到一封驗證郵件完成驗證後再重試失敗的步驟。url

Server response (400): Invalid classifier "Topic :: Software Development :: Utilities"

你的setup.py文件中的classifier信息有誤,請按官網的正確分類書寫classifier.code

error: No dist file created in earlier command

你還沒打包就開始了上傳命令

Upload failed (400): File already exists

文件已經存在了,你每一次上次都應該更新版本號。

參考文檔:

https://packaging.python.org/tutorials/packaging-projects/ https://segmentfault.com/a/1190000008663126 http://xiaoh.me/2015/12/11/python-egg/

相關文章
相關標籤/搜索