打包和发布你的Python项目

打包和发布项目

安装依赖文件

pip install wheel

pip install twine # 用来上传项目到PyPI

配置项目

初始化文件

setup.py

两个主要的功能:

  1. setup函数
  2. 命令行接口,使用python setup.py --help-commands列出所有命令

setup.cfg

包含setup.py命令的默认选项。例如:

[bdist_wheel]
# This flag says that the code is written to work on both Python 2 and Python
# 3. If at all possible, it is good practice to do this. If you cannot, you
# will need to generate wheels for each Python version that you support
universal=1

README.rst

reStructureText写的README内容

MANIFEST.in

当你需要一些附加文件的时候,而在python setup.py sdist(或者 bdist_wheel)不会自动包含时。例如:

include *.txt
recursive-include examples *.txt *.py
prune examples/sample?/build

详情

setup() 参数

name

name='sample',

名称

version

version='1.2.0',

版本,参考:

1.2.0.dev1  # Development release
1.2.0a1     # Alpha Release
1.2.0b1     # Beta Release
1.2.0rc1    # RC Release
1.2.0         # Final Release
1.2.0.post1 # Post Release

description

description='A sample Python project',
long_description=long_description,

长短介绍

url

url='https://github.com/pypa/sampleproject',

主页

author

author='The Python Packaging Authority',
author_email='[email protected]',

作者信息

license

license='MIT',

classifiers

classifiers=[
    # How mature is this project? Common values are
    #   3 - Alpha
    #   4 - Beta
    #   5 - Production/Stable
    'Development Status :: 3 - Alpha',

    # Indicate who your project is intended for
    'Intended Audience :: Developers',
    'Topic :: Software Development :: Build Tools',

    # Pick your license as you wish (should match "license" above)
     'License :: OSI Approved :: MIT License',

    # Specify the Python versions you support here. In particular, ensure
    # that you indicate whether you support Python 2, Python 3 or both.
    'Programming Language :: Python :: 2',
    'Programming Language :: Python :: 2.6',
    'Programming Language :: Python :: 2.7',
    'Programming Language :: Python :: 3',
    'Programming Language :: Python :: 3.2',
    'Programming Language :: Python :: 3.3',
    'Programming Language :: Python :: 3.4',
],

分类信息

详情

keywords

keywords='sample setuptools development',

关键字

packages

packages=find_packages(exclude=['contrib', 'docs', 'tests*']),

项目需要包含的包目录,可以手动指名,使用setuptools.find_packages可以自动发现,使用exclude关键字指名需要排除的目录

install_requires

install_requires=['peppercorn'],

依赖包,使用pip时会自动解决依赖

package_data

package_data={
    'sample': ['package_data.dat'],
},

经常需要附加文件的包中。这些可能是包相关数据,或者文档。统一称为为"package data"

必须相对路径

data_files

data_files=[('my_data', ['data/data_file'])],

如果你想将data files放在包外。

成对(目录,文件s)序列指名要安装的文件到目录中。如果目录是相对目录通常是:

>>> import sys
>>> sys.prefix
'/usr'

scripts

帮你构造安装后的可以调用脚本

entry_points

entry_points={
  ...
},

定义plugins

console_scripts

注册脚本接口

开发模式

建议使用开发模式进行测试

python setup.py develop

或者使用pip打包

pip install -e .

打包项目

代码发布

python setup.py sdist

Wheels

Universal Wheels

python setup.py bdist_wheel --universal
  1. 项目可以直接跑2和3
  2. 不需要任何C扩展

Pure Python Wheels

python setup.py bdist_wheel

不同时支持2和3,你可能需要借用2to3,然后运行两次setup.py bdist_wheel

Platform Wheels

python setup.py bdist_wheel

上传项目到PyPI

创建帐号

  1. 手动注册帐号,https://pypi.python.org/pypi?%3Aaction=register_form
  2. 注册项目

注册项目

  1. 使用PyPI网站提供的表单(推荐)
  2. 使用python setup.py register注册

如果是PyPI网站注册的,还需要建立~/.pypirc文件

[distutils]
index-servers=pypi

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

也可以不指名密码,在执行时使用-p PASSWORD指名

上传

  1. (推荐) twine upload dist/*
  2. python setup.py sdist bdist_wheel upload

你可能感兴趣的:(python)