0%

在PyPI上发布自己的Python库

项目目录结构

例如,在此演示的发布库的项目结构如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
MinglogUtils
├─LICENSE
├─README.md
├─setup.py
├─CVUtils
│ ├─CVUtils.py
│ ├─test.py
│ ├─__init__.py
│ ├─files
│ │ ├─province.txt
│ │ └─WordAndNum.txt
│ └─__pycache__
│ ├─CVUtils.cpython-38.pyc
│ └─__init__.cpython-38.pyc
└─SpiderUtils
├─SpiderUtils.py
└─__init__.py

其中:

LICENSE:证书。

README.md:库说明文档,用于在PyPI官网展示。

setup.py:上传的核心代码文件,用于配置打包后的库。

CVUtilsSpiderUtils:自定义模块,在其内部必须包含__init__初始化方法,方便后续打包时自动识别对应的模块。

LICENSE

一般使用MIT证书即可,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
MIT License

Copyright (c) [year] [fullname]

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.md

库的说明文档,markdown格式。用于在PYPI官网展示,如下图所示。

image-20230905170717874

setup.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from setuptools import setup, find_packages

setup(
name="MingLogUtils", # 设置库的名字,必须是和其他人有区别,后续使用pip install去下载的时候就是使用这个名字
version="1.0.0", # 设置库的版本
packages=find_packages(), # 设置自动获取模块
# 依赖库
install_requires=[
'numpy',
'matplotlib',
'pillow',
'opencv-python',
'aiofiles',
'scipy'
],
package_data={'CVUtils': ['files\*.txt',]}, # 定义每个模块的数据文件,以当前模块作为根路径
author="MingLog", # 作者名称
author_email="736704198@qq.com", # 作者Email
description="自定义相关工具库", # 库的简短介绍语句
long_description=open("README.md", encoding='UTF-8').read(), # 读取README.md
long_description_content_type="text/markdown", # 设置README.md的格式
license="MIT", # 设置证书
url="http://minglog.hzbmmc.com", # 设置库的官网url地址
# 库的描述信息
classifiers=[
"Development Status :: 3 - Alpha",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
],
)

打包库

下载相关工具:

1
pip install setuptools wheel twine

开始打包:

1
python setup.py sdist bdist_wheel

上传到PyPI

要想上传到PyPI,首先需要去官网注册一个账号。

然后使用twine工具上传构建好的库到PyPI

1
twine upload dist/*

根据提示输入你的PyPI用户名和密码,上传成功后,你的库就会出现在PyPI网站上。

image-20230905171931156

-------------本文结束感谢您的阅读-------------