提交库到pypi
提交库到pip
一、创建包
创建一个工程,目录结构如下:
包名
├── LICENSE
├── 包名
│ └── __init__.py
├── README.md
└── setup.py
-
LICENSE
授权文件。可以从这里挑选一个你喜欢的授权。例如:
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
在这里写上库的详细描述。通常,写一下基本介绍,如何使用等等。
-
setup.py
#!python # -*- coding:utf-8 -*- from __future__ import print_function from setuptools import setup, find_packages with open("README.md", "r") as fh: long_description = fh.read() setup( name="包名", version='0.0.2', author="liushengkun", author_email="xxnull@163.com", description="a stub for pgzero", long_description=long_description, long_description_content_type="text/markdown", license="MIT", url="https://github.com/账号名/包名", packages=['包名'], install_requires=[ "pgzero <= 1.2.1" ], classifiers=[ "Topic :: Games/Entertainment ", 'Topic :: Games/Entertainment :: Puzzle Games', 'Topic :: Games/Entertainment :: Board Games', "Topic :: Software Development :: Libraries :: Python Modules", 'Programming Language :: Python :: 3 :: Only' ], )
其中,主要配置项有:
- name 是包名。发布之前请上 PyPi 搜索一下有没有同名的包,防止冲突。
- version 是版本号,更新的时候会寻找比当前版本更高的版本号,所以不要乱写。
- description是短描述,一般是一句话。
- long_description是长描述,详细的介绍。这儿可以直接从 README.md读入。
- url是你项目的地址。一般会填 github 地址。
- packages是要安装的包,也就是目录接口中的包源码目录。
- install_requires是这个包的所需的依赖。
- classifiers 是分类。根据 PyPi Classifiers 填写,至少要包含所用的 Python 版本。
二、编译与测试
-
安装所需的依赖
pip3 install --user --upgrade setuptools wheel
-
检查【可选】
检查setup.py语法是否正确:python setup.py check
-
编译
python3 setup.py sdist bdist_wheel
-
本地安装测试
cd dist pip3 install *.whl
三、注册账号
上传需要pypi的账号。
-
去这儿注册
-
在账号配置中,开启
Two factor authentication
(双因子认证)。注意:开启双因子认证可以使用
腾讯身份验证器
这个微信小程序。比较简单,去微信小程序里面找一下即可。 -
在这儿创建一个API token。
注意:这儿的token只会看到一次,需要复制下来保存好。
四、上传
-
安装所需的包
pip3 install --user --upgrade setuptools wheel pip3 install --user --upgrade twine
-
上传包
export TWINE_USERNAME=__token__ export TWINE_PASSWORD=[token] export TWINE_REPOSITORY_URL=https://upload.pypi.org/legacy/ twine upload dist/*
其中,
-
TWINE_USERNAME是
__token__
-
TWINE_PASSWORD是申请的API token
也可以使用
.pypric
配置文件,使用方法请参考官方文档。 -