1. 首先创建项目,目录结构如下

worker_package/
├── LICENSE
├── README.md
├── setup.py
└── workerwork
    ├── __init__.py
    └── models.py

其中 worker_package/workerwork 是主代码目录,setup.py 是必备的打包文件

setup.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: workerwork(workerwork@qq.com)
# Description:

from setuptools import setup, find_packages

setup(
    name = 'workerwork',
    version = '0.0.1',
    keywords='wow',
    description = 'a library for wow Developer',
    license = 'MIT License',
    url = '',
    author = 'workerwork',
    author_email = 'workerwork@qq.com',
    packages = find_packages(),
    include_package_data = True,
    platforms = 'any',
    install_requires = [
        'requests>=2.19.1',
        'pycrypto>=2.6.1',
        'xmltodict>=0.11.0'
        ],
)

models.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: workerwork(workerwork@qq.com)

class Message():
    @classmethod
    def test(self):
        print('Hello World')

__init__.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: workerwork(workerwork@qq.com)

from .models import Message

2. 安装到本地测试

接下来在 setup.py 所在目录下执行安装命令,安装到本地

$ pip install .

在项目中你就可以使用测试了

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: workerwork(workerwork@qq.com)

from workerwork import Message

Message.test()

3. 打包

测试完成后,再上传到 pypi 之前需要先打包

$ python setup.py <params>

params 有如下取值

sdist             create a source distribution (tarball, zip file, etc.)
bdist             create a built (binary) distribution
bdist_dumb        create a "dumb" built distribution
bdist_rpm         create an RPM distribution
bdist_wininst     create an executable installer for MS Windows
bdist_egg         create an "egg" distribution

sdist 可以支持上传到 pypi

$ python setup.py sdist

然后根目录中会出现 dist 目录存放打包文件

4. 上传 pypi

最后一步上传到 pypi,首先去官网搜索确认项目名没有被占用,并注册用户,然后使用 twine 进行上传

下载 twine

$ pip install twine

上传

$ twine upload dist/*

然后根据提示输入用户名密码即可

5. 设置全局账户信息

创建 ~/.pypirc 文件并添加如下信息

[distutils]
index-servers=pypi

[pypi]
repository = https://upload.pypi.org/legacy/
username = <username>
password = <password>

然后再次上传就不会提示输入用户密码了