将数据文件添加到python项目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")

原文由 Shahin 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 601
2 个回答

第一个问题是我没有将我的数据文件导入到带有 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()

原文由 Shahin 发布,翻译遵循 CC BY-SA 3.0 许可协议

我使用 了数据文件

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

原文由 dfrankow 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏