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

How can a `console_scripts` access file from `package_data`

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

Python 要处理的文件 console_scripts:

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

改变cwd怎么样?

import os
os.chdir(__file__)

conftest.py 听起来是做这件事的好地方。或者附加到您的 test 命令的文件。

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

import os
import pkg_resources

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