Any python function to get “data_files” root directory?

为君一笑 提交于 2019-11-29 01:33:33

By default, when installing a package as root, relative directory names in the data_files list are are resolved against either the value of sys.prefix (for pure-python libraries) or sys.exec_prefix (for libraries with a compiled extension), so you can retrieve your files based on that. Qouting from the distutils documentation:

If directory is a relative path, it is interpreted relative to the installation prefix (Python’s sys.prefix for pure-Python packages, sys.exec_prefix for packages that contain extension modules).

So for your example, you'll find your files in os.path.join(sys.prefix, 'MyApp', 'CBV').

However, you would be better off using the setuptools extension to distutils and use the pkg_resources module Resource API to load data files. It comes with setuptools for this very purpose. You do want your data files included in the package for that to work best. That means you would not use data_files but instead either set include_package_files=True or list file patterns with package_files, see Including data files in the setuptools documentation.

You can then load such resource files straight from the package into a string with resource_string() for example:

from pkg_resources import resource_string

foo_config = resource_string(__name__, 'foo.conf')
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!