如何提交Package到PyPI

PyPI官方文档

Steps

注册账号

填写配置文件~/.pypirc

[distutils]
index-servers=
    pypi
    pypitest

[pypitest]
repository = https://testpypi.python.org/pypi
username = <your user name goes here>
password = <your password goes here>

[pypi]
repository = https://pypi.python.org/pypi
username = <your user name goes here>
password = <your password goes here>

准备你的package

PyPI上的每个package需要一个setup.py文件在项目的根目录, 如果你在一个一个markdown格式的README文件, 你还需要一个setup.cfg文件. 一个LICENSE.txt文件. 假设包名为:mypackage, 目录结构如下.

├── LICENSE.txt
├── README.md
├── mypackage
│   ├── __init__.py
│   ├── bar.py
│   ├── baz.py
│   └── foo.py
├── setup.cfg
└── setup.py

setup.py

from distutils.core import setup

setup(
    name='mypackage',
    packages=['mypackage'],  # this must be the same as the name above
    version='0.1',
    description='A random test lib',
    author='Akagi201',
    author_email='akagi201@gmail.com',
    url='https://github.com/Akagi201/mypackage',  # use the URL to the github repo
    download_url='https://github.com/Akagi201/mypackage/tarball/0.1',  # I'll explain this in a second
    keywords=['testing', 'logging', 'example'],  # arbitrary keywords
    classifiers=[],
)

download_url: repo源码的下载链接, 使用git tag后, github会为你host. 使用命令: git tag 0.1 -m "Adds a tag so that we can put this on PyPI.", 使用 git push --tags origin master 提交到github

setup.cfg

告诉PyPI你的README文件的位置.

[metadata]
description-file = README.md

提交package到PyPI Test

提交package到PyPI Live

不使用setuptools, 使用更安全的twine上传

Refs