setuptools的package_data问题
setup {
package_data = ['', ['*.txt']]
}
不起作用,打包为源代码的时候并没有把package下的txt文件放进去。
找到setuptools的官方文档,有这么一段话:
This tells setuptools to install any data files it finds in your packages. The data files must be under CVS or Subversion control, or else they must be specified via the distutils' MANIFEST.in file. (They can also be tracked by another revision control system, using an appropriate plugin
原来是必须配置版本管理软件或者在目录下建立一个MANIFEST.in配置文件,没装cvs,选用第二种方法:
建立MANIFEST.in,写入:
include *.txt
表示匹配所有txt文件
然后python setup.py sdist, 数据文件已正常导入
setuptools处理package_data有三种方法:一种是上面的方法
from setuptools import setup, find_packages
setup(
...
package_data = {
# 任何包如果包含 *.txt or *.rst 文件都加进去,可以处理多层package目录结构
'': ['*.txt', '*.rst'],
# 如果hello包下面有*.msg文件也加进去
'hello': ['*.msg'],
}
)
还有一种是自动识别,多层package都可以处理:
from setuptools import setup, find_packages
setup(
...
include_package_data = True
)
会搜索所有package的所有数据文件,这种方法比较省事
最后一种情况是数据文件位于package的字目录里面,采用手动设置的方法,比如说在mypkg的data目录下,可以这样做:
from setuptools import setup, find_packages
setup(
...
packages = find_packages('src'), # include all packages under src
package_dir = {'':'src'}, # tell distutils packages are under src
package_data = {
# If any package contains *.txt files, include them:
'': ['*.txt'],
# And include any *.dat files found in the 'data' subdirectory
# of the 'mypkg' package, also:
'mypkg': ['data/*.dat'],
}
)
详见setuptools官方文档:http://peak.telecommunity.com/DevCenter/setuptools#including-data-files