之前我在 Python 开发实践 中曾经提到过通过 python distribute 实现包分发。但是一直没有机会研究如何实现 (没错…),今天就粗略的通过一个例子简单描述一下一个包的制作:
# setup.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from setuptools import setup, find_packages
import sys
# 兼容旧版本 setuptools
extra = {}
if sys.version_info >= (3,):
extra['use_2to3'] = True
extra['convert_2to3_doctests'] = ['src/your/module/README.txt']
extra['use_2to3_fixers'] = ['your.fixers']
setup(
# 包名称
name='your.module',
# 版本号
version = '1.0',
# 包描述
description='This is your awesome module',
# 作者名称
author='You',
# 作者 URL
author_email='your@email',
# 如果有需要
license = "PSF",
# 如果有需要
url = "",
# 依赖包
install_requires=[],
# 源代码目录
package_dir = {'':'src'},
# 包文件
packages = find_packages(),
# 包含其他必须文件,前面是文件夹,后面是扩展名,将会按此格式排列文件
package_data = {# include them:'': ['*.txt', '*.rst'],
},
# 单元测试
test_suite = 'your.module.tests',
**extra
)
在 setuptools >= 0.7 的版本上,distribute 的功能已经被整合进入 Setuptools,例子也是适合于 0.7 以上版本的配置。上面这个例子适用于纯 Python 的包,很多时候我们需要做一些对 Python 的性能提升 (比如动态库 binding),怎么做呢?这是下一篇文要写的了…