`console_scripts`如何从`package_data`访问文件

时间:2015-10-24 07:27:42

标签: python python-2.7 setuptools setup.py

创建console_scripts作为entry_points时,如何访问包中的数据文件(package_data)?

setup(
    # other stuff
    entry_points={
        'console_scripts': [
            'runme = mypackage.a_py_file:a_function_within_the_py_file',
        ]
    }
)

目录结构:

├── mypackage
│   ├── __init__.py
│   └── a_py_file.py
├── requirements.txt
├── setup.py
└── data 
    ├── a.data
    └── b.data

处理console_scripts的Python文件:

# a_py_file.py
def a_function_within_the_py_file:
    # HOW TO READ a.data FILE HERE

2 个答案:

答案 0 :(得分:2)

如何更改cwd

import os
os.chdir(__file__)

conftest.py听起来像个好地方。或者附加到test命令的文件。

答案 1 :(得分:2)

所以这就是我所做的并且有效:

import os
import pkg_resources

os.chdir(pkg_resources.get_distribution('mypackage').location)
# running as if the script is invoked from project's root