Python发布

Python库的打包,发布,分发.

打包

  • Source Distribution/sdist: setuptools用来制作源码分发
  • bdist_wheel扩展用来创建wheels(可包含二进制扩展库)
  • twine发布到pypi
# 前提
pip install wheel twine
# 打成发布包
python setup.py sdist bdist_wheel

# twine 发布
twine upload --repository-url http://10.100.220.103:6543/ dist/*

setup.py

setup.py里的entry_points会最终生成entry_points.txt并存放在EGG-INFO文件夹.
entry point一般是一个可调用函数,方便命令行调用.

python setup.py --help-commands

# 打包wheel
python setup.py bdist_wheel -d TARGET
# 上传本地
python3 setup.py bdist_wheel upload -r dev

一个setup.py的例子:

from setuptools import setup, find_packages
import os
from os.path import dirname


here = os.path.abspath(dirname(__file__))

required = []

setup(
    name='loads',
    version='1.0.0',
    description='Test Project.',
    packages=find_packages(exclude=['tests']),
    packages=['locust', 'locust.rpc', 'locust.rpc', 'locust.templates'],
    include_package_data=True,
    zip_safe=False,
    tests_require=['unittest2', 'mock'],
    install_requires=required,
    entry_points={
        'console_scripts': [
            'locust = locust.main:main',
        ]
    },
)

MANIFEST.in

包含文件配置,包含非py代码

recursive-include loads/packages/locust/static *

setup.cfg

[easy_install]
index_url = http://pypi.douban.com/simple

.pypirc(可选)

注:使用twine上传时可指定repository,则该文件不必要存在。

存放于Home目录

[distutils]
index-servers =
  dev

[dev]
repository: http://xulizhao.com:6543/pypi/
username: admin
password: admin

本地pypi

搭建本地pypi服务或镜像

PyPI Cloud

# 运行
pserve server.ini

# 配置:允许上传覆盖
pypi.allow_overwrite = True

功能更强的其他选择

分发

  • pyinstaller : 制作成安装包
  • dh-virtualenv:打包成deb系统格式
  • Pynsist:Build Windows installers for Python applications

扩展阅读