Any python function to get “data_files” root directory?

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 importlib.resources library (Python 3.7 and up) to load package data. 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 list file patterns in a MANIFEST.in file and set include_package_data=True, or list file patterns in package_data, see Including data files in the setuptools documentation.

For earlier Python versions, you can do the same with the pkg_resources module Resource API to load data files (it is part of the setuptools library, for this very purpose).

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

try:
    from importlib.resources import read_text
except ImportError:
    from pkg_resources import resource_string as read_text

foo_config = read_text(__name__, 'foo.conf')

Leave a Comment

404 Not Found

Not Found

The requested URL was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.