【问题标题】:add data files to python projects setup.py将数据文件添加到 python 项目 setup.py
【发布时间】:2012-08-24 20:30:37
【问题描述】:

我有一个这样的项目:

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

【问题讨论】:

  • 在 python 2.7 中,他们改变了将文件包含到 MANIFEST.in 或其他东西的方式 - 我不确定也没有使用它,但它可能是一个方向
  • 我在 python 2.6 中!由于某些原因,我现在无法升级!

标签: python setuptools setup.py


【解决方案1】:

不妨试试:

package_data={'negar/data': ['data/*.dat']},

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 您在哪个文件中添加了 import os ... 行?
    【解决方案3】:

    我用data_files

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

    【讨论】:

      猜你喜欢
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      • 2011-05-18
      相关资源
      最近更新 更多