24

我有一个这样的项目:

├── 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")
4

3 回答 3

21

我使用了 data_files

data_files = [('', ['negar/data/untouchable.dat'])],
于 2013-03-10T14:19:35.703 回答
12

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

include negar/data/*.dat

之后,我的数据文件已经与我的包安装一起导入。但是因为我打开我的数据文件时出错,python找不到它。这个问题帮助我找到了 包子目录中 Python 访问数据的正确方法,现在我使用这样的东西:

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()
于 2012-08-31T05:56:40.877 回答
3

也许尝试:

package_data={'negar/data': ['data/*.dat']},
于 2012-08-30T07:38:20.363 回答