将数据文件添加到 python 项目 setup.py [英] add data files to python projects setup.py

查看:34
本文介绍了将数据文件添加到 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 安装项目:

and inside that my file Virastar.py need some data from data.untouchable.dat. it works fine until I install the project with this 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',
        ],
    }, 
    ...  
)

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

after that when I start my program and when it needs that data file it return this error:

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

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

even in my egg-info sources I can't find any data file:

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

我错过了什么吗?

谢谢大家.

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

Do I have to add any special thing in init.py?

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

and I have to add this: I used untouchable.dat just like this:

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

推荐答案

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

The first problem is that I didn't import my data file into the package with MANIFEST.in file. I imported it like this:

include negar/data/*.dat

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

After that my data file already imported with my package install. but because I had mistakes in open my data files, python couldn't find it. this question helped me to find the right way Python Access Data in Package Subdirectory and now I use something like this:

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()

这篇关于将数据文件添加到 python 项目 setup.py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆