How-to: 提交python软件到PyPI

PyPI -「Python Package Index」,官方介绍:

The Python Package Index is a repository of software for the Python
programming language. There are currently 102159 packages here.

使用python时,经常用到pip或easy_install安装软件包,因而一直对如何使用pip来管理自己的软件好奇.

刚好项目上有需要将腾讯公有云容器服务一些操作进行封装、简化,适合我们的部署方式.

下面就以该项目为例,将软件打包上传到PyPI, 然后通过pip安装使用.

软件包准备

  1. qcloud_ccs的目录结构:
.
├── qcloud_ccs
│   └── __init__.py
├── README.md
└── setup.py
  1. qcloud_ccs代码内容:
$ cat qcloud_ccs/__init__.py 
#!/usr/bin/env python3
# encoding=utf-8

def test():
    print(u'吃瓜群众')

def main():
    test()
  1. 安装描述文件
$ cat setup.py 
#!/usr/bin/env python3
# coding: utf-8

from setuptools import setup

setup(
    name='qcloud_ccs',
    version='1.0.2',
    author='wanglin',
    author_email='wanglin@dbca.cn',
    url='https://newops.cn/qcloud_ccs',
    description=u'以qcloud 容器服务sdk为基础. 1, 将机器创建/服务启停及更新等封装为接口;2.将机器和服务配置抽离为配置文件',
    packages=['qcloud_ccs'],
    install_requires=[],
    entry_points={
        'console_scripts': [
            'qcloud_ccs=qcloud_ccs:test'
        ]
    }
)
  • name: 软件名
  • version: 版本号
  • 'qcloud_ccs=qcloud_ccs:test': 将qcloud_ccs:test封装为cmd命令,名称为qcloud_ccs

PyPI 注册并上传

帐号注册

首先在PyPI帐号注册地址申请一个帐号

帐号配置

$ cat ~/.pypirc 
[distutils]
index-servers =
    pypi

[pypi]
repository=https://pypi.python.org/pypi
username=xxxx
password=xxxx

后面上传时如何出现:403: Invalid or non-existent authentication information, 请检查用户名、密码,注意密码不能用引号、百分号、注释符号等

软件打包、上传

$ python setup.py sdist upload         
running sdist
running egg_info
writing qcloud_ccs.egg-info/PKG-INFO
writing dependency_links to qcloud_ccs.egg-info/dependency_links.txt
writing entry points to qcloud_ccs.egg-info/entry_points.txt
writing top-level names to qcloud_ccs.egg-info/top_level.txt
reading manifest file 'qcloud_ccs.egg-info/SOURCES.txt'
writing manifest file 'qcloud_ccs.egg-info/SOURCES.txt'
running check
creating qcloud_ccs-1.0.2
creating qcloud_ccs-1.0.2/qcloud_ccs
creating qcloud_ccs-1.0.2/qcloud_ccs.egg-info
copying files to qcloud_ccs-1.0.2...
copying README.md -> qcloud_ccs-1.0.2
copying setup.py -> qcloud_ccs-1.0.2
copying qcloud_ccs/__init__.py -> qcloud_ccs-1.0.2/qcloud_ccs
copying qcloud_ccs.egg-info/PKG-INFO -> qcloud_ccs-1.0.2/qcloud_ccs.egg-info
copying qcloud_ccs.egg-info/SOURCES.txt -> qcloud_ccs-1.0.2/qcloud_ccs.egg-info
copying qcloud_ccs.egg-info/dependency_links.txt -> qcloud_ccs-1.0.2/qcloud_ccs.egg-info
copying qcloud_ccs.egg-info/entry_points.txt -> qcloud_ccs-1.0.2/qcloud_ccs.egg-info
copying qcloud_ccs.egg-info/top_level.txt -> qcloud_ccs-1.0.2/qcloud_ccs.egg-info
Writing qcloud_ccs-1.0.2/setup.cfg
creating dist
Creating tar archive
removing 'qcloud_ccs-1.0.2' (and everything under it)
running upload
Submitting dist/qcloud_ccs-1.0.2.tar.gz to https://upload.pypi.org/legacy/
Server response (200): OK

安装使用

$ pip install qcloud_ccs
Collecting qcloud_ccs
  Downloading qcloud_ccs-1.0.2.tar.gz
Building wheels for collected packages: qcloud-ccs
  Running setup.py bdist_wheel for qcloud-ccs ... done
  Stored in directory: /home/k8s/.cache/pip/wheels/38/45/0d/435a007b7f6a47983ae16dd198e3864d515b42b79d31c490d6
Successfully built qcloud-ccs
Installing collected packages: qcloud-ccs
Successfully installed qcloud-ccs-1.0.2

$ qcloud_ccs
吃瓜群众

到这里,软件包已经可以在pip的软件仓库中找到并安装使用了 V^

后台管理已上传的软件包

引用依赖

如果软件依赖其它软件包,需在setup.py完善相关配置

$ cat setup.py
...
    install_requires=[
        'qcloudapi-sdk-python==2.0.7',
        'requests==2.18.4'
    ],
...
$ pip install qcloud_ccs
Collecting qcloud_ccs
  Downloading qcloud_ccs-1.0.4.tar.gz
Collecting qcloudapi-sdk-python==2.0.7 (from qcloud_ccs)
  Downloading qcloudapi-sdk-python-2.0.7.tar.gz
Collecting requests==2.18.4 (from qcloud_ccs)
  Downloading requests-2.18.4-py2.py3-none-any.whl (88kB)
    100% |████████████████████████████████| 92kB 982kB/s 
Collecting certifi>=2017.4.17 (from requests==2.18.4->qcloud_ccs)
  Downloading certifi-2017.7.27.1-py2.py3-none-any.whl (349kB)
    100% |████████████████████████████████| 358kB 783kB/s 
Collecting idna<2.7,>=2.5 (from requests==2.18.4->qcloud_ccs)
  Downloading idna-2.6-py2.py3-none-any.whl (56kB)
    100% |████████████████████████████████| 61kB 2.1MB/s 
Collecting urllib3<1.23,>=1.21.1 (from requests==2.18.4->qcloud_ccs)
  Downloading urllib3-1.22-py2.py3-none-any.whl (132kB)
    100% |████████████████████████████████| 133kB 1.2MB/s 
Collecting chardet<3.1.0,>=3.0.2 (from requests==2.18.4->qcloud_ccs)
  Downloading chardet-3.0.4-py2.py3-none-any.whl (133kB)
    100% |████████████████████████████████| 143kB 1.3MB/s 
Building wheels for collected packages: qcloud-ccs, qcloudapi-sdk-python
  Running setup.py bdist_wheel for qcloud-ccs ... done
  Stored in directory: /home/k8s/.cache/pip/wheels/cd/d8/d6/aac6a354d0fe4099b6837aceda25fc1f11de6629d4b1581cff
  Running setup.py bdist_wheel for qcloudapi-sdk-python ... done
  Stored in directory: /home/k8s/.cache/pip/wheels/55/5e/4e/e15deea2b7077e4e1838cbdbd512c4cf8e51eab3352ed8989a