# 打包提交pip模块

# 准备工作

# 创建目录结构

./pushgateway_client/
├── LICENSE
├── README.md
├── pyproject.toml
├── setup.cfg
├── src/
│   └── pushgateway_client/
│       ├── __init__.py
│       └── client.py
└── tests/
  • tests/是测试文件的占位符,暂时将其留空。

# 创建pyproject.toml

pyproject.toml告诉构建工具(如pip和build)构建项目需要什么。本模块使用setuptools,所以打开pyproject.toml输入如下内容:

[build-system]
requires = [
    "setuptools>=42",
    "wheel>=0.36.2",
    "requests>=2.26.0"
]
build-backend = "setuptools.build_meta"
  • build-system.requires给出构建包所需的包列表。在此处列出某些内容只会使其在构建期间可用,而不是在安装后可用。
  • build-system.build-backend是将用于执行构建的 Python 对象的名称。

# 创建setup.cfg

setup.cfg是setuptools的配置文件。它告诉 setuptools 关于您的包(例如名称和版本)以及要包含哪些代码文件。

打开setup.cfg并输入以下内容。更改name 以包含您的用户名;这可确保您拥有唯一的包名称,并且您的包不会与其他人按照本教程上传的包发生冲突。

[metadata]
name = pushgateway_client
version = 0.0.9
author = Tay
author_email = tay3223@qq.com
description = Python专用的pushgateway客户端,可以向Pushgateway中推送指标数据,支持多标签设定
long_description = file: README.md
long_description_content_type = text/markdown
url = https://blog.taycc.com/pages/opensource/pushgateway_client.html
project_urls =
    使用文档 = https://blog.taycc.com/pages/opensource/pushgateway_client.html
classifiers =
    Programming Language :: Python :: 3
    License :: OSI Approved :: MIT License
    Operating System :: OS Independent

[options]
package_dir =
    = src
packages = find:
python_requires = >=3.6

[options.packages.find]
where = src

# 创建README.md

创建README.md文件以便在PyPi的模块主页中进行项目说明

# Example Package

This is a simple example package. You can use
[Github-flavored Markdown](https://guides.github.com/features/mastering-markdown/)
to write your content.

# 创建LICENSE(许可证)

上传到 Python Package Index 的每个包都包含许可证很重要。这会告诉安装您的软件包的用户他们可以使用您的软件包的条款。如需帮助选择许可证,请参阅 https://choosealicense.com/。选择许可证后,打开 LICENSE并输入许可证文本。例如,如果您选择了 MIT 许可证:

Copyright (c) 2018 The Python Packaging Authority

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.

# 编写代码

准备工作完成后,您就可以开发自己的代码了,本地测试OK之后,就可以发布到pip官方源仓库中。
~
~
~
开发过程,略...

# 打包上传

[pypi]
  username = __token__                      # 这个大家填写的都是一样的,都是填写__token__
  password = pypi-AgEIcHlwaS5vcmcCJDF...    # 这是作者自己的token,后面这里我就省略掉了,大家替换上自己的token即可
  • 第四步:当开发完自己的代码创建好~/.pypirc文件后,接着就需要在pyproject.toml文件的同级目录中执行如下命令
pip install --upgrade pip
pip install --upgrade build twine
python3 -m build
twine upload dist/*

到此为止Python模块打包上传就算完成了,大家可以去自己账号上查看pypi包。

# 访问自己的模块

pip install pushgateway-client

如果国内下载较慢可以临时加速一下

# pypi官网(原版拉取)
pip install pushgateway-client --upgrade -i https://pypi.org/project   

# 阿里云源(加速拉取)
pip install pushgateway-client --upgrade -i http://mirrors.aliyun.com/pypi/simple

# 清华大学源(加速拉取)    
pip install pushgateway-client --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple




(全文到此结束)