记录第一次制做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/

相关文章
相关标签/搜索