将数据文件添加到python项目setup.py

时间:2012-08-30 06:49:16

标签: python setuptools setup.py

我有一个这样的项目:

├── CHANGES.txt
├── LICENSE
├── MANIFEST.in
...
├── docs
│   └── index.rst
├── negar
│   ├── Negar.py
│   ├── Virastar.py
│   ├── Virastar.pyc
│   ├── __init__.py
│   ├── data
│   │   ├── __init__.py
│   │   └── untouchable.dat
│   ├── gui.py
│   ├── gui.pyc
│   ├── i18n
│   │   ├── fa_IR.qm
│   │   └── fa_IR.ts
│   └── negar.pro
├── setup.py
...

并在其中我的文件Virastar.py需要来自data.untouchable.dat的一些数据。它工作正常,直到我使用此setup.py

安装项目
setup(
    ...
    include_package_data=True,
    packages = find_packages() + ['negar'],
    package_dir={'negar': 'negar'},
    package_data={'negar': ['data/*.dat']},
    entry_points={
        'console_scripts': [
            'negar = negar.Negar:main',
        ],
    }, 
    ...  
)

之后,当我启动程序时,当需要该数据文件时,它会返回此错误:

IOError: [Errno 2] No such file or directory: 'data/untochable.dat'

即使在我的egg-info来源中,我也找不到任何数据文件:

...
negar/Negar.py
negar/Virastar.py
negar/__init__.py
negar/gui.py
negar/data/__init__.py

我错过了什么吗?

谢谢大家。

编辑:我是否必须在 init .py中添加任何特殊内容?

我必须补充一点:我使用了untouchable.dat就像这样:

f = codecs.open('data/untouchable.dat', encoding="utf-8")

3 个答案:

答案 0 :(得分:13)

我使用了data_files

data_files = [('', ['negar/data/untouchable.dat'])],

答案 1 :(得分:9)

第一个问题是我没有将我的数据文件导入到包含MANIFEST.in文件的包中。我这样导入它:

include negar/data/*.dat

之后我的数据文件已经与我的软件包安装一起导入。但是因为我在打开我的数据文件时出错,所以python无法找到它。这个问题帮助我找到了正确的方法 Python Access Data in Package Subdirectory现在我使用这样的东西:

import os
this_dir, this_filename = os.path.split(__file__)
DATA_PATH = os.path.join(this_dir, "data", "data.txt")
print open(DATA_PATH).read()

答案 2 :(得分:2)

也许试试:

package_data={'negar/data': ['data/*.dat']},
相关问题