Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing data files before and after distutils/setuptools

I'm doing a platform independent PyQt application. I intend to use write a setup.py files using setuptools. So far I've managed to detech platform, e.g. load specific options for setup() depending on platform in order to use py2exe on Windows... etc...

However, with my application I'm distributing some themes, HTML and images, I need to load these images in the application at runtime. So far they are stored in the themes/ directory of the application.

I've been reading documentation on setuptools and distutils, and figured out that if I gave setup() the data_files options with all the files in the themes/ directory to be installed in "share/MyApp/themes/" it would be installed with a /usr/ prefix, or whatever sys.prefix is on the platform. I assume that I would find the data files using os.path.join(sys.prefix, "share", "MyApp", "themes") nomatter what platform I'm on, right?

However, I want to be able to access the data files during development too, where they reside in the themes/ directory relative to application source. How do I do this? Is there some smart way to figure out whether the application has been installed? Or a utility that maps to the data files regardless of where they are, at the moment?

I would really hate to add all sorts of ugly hacks to see if there are themes relative to the source, or in sys.prefix/share... etc... How do I find there data files during development? and after install on arbitrary platform ?

like image 705
jonasfj Avatar asked Aug 02 '09 17:08

jonasfj


People also ask

What is setuptools used for in Python?

Setuptools is a collection of enhancements to the Python distutils that allow developers to more easily build and distribute Python packages, especially ones that have dependencies on other packages. Packages built and distributed using setuptools look to the user like ordinary Python packages based on the distutils .

What is MANIFEST in used for?

The MANIFEST.in file contains commands that allow you to discover and manipulate lists of files. There are many commands that can be used with different objectives, but you should try to not make your MANIFEST.in file too fine grained.

What is package_data?

The package_data argument is a dictionary that maps from package names to lists of glob patterns. Note that the data files specified using the package_data option neither require to be included within a MANIFEST.in file, nor require to be added by a revision control system plugin.

How do you include data in a python package?

Place the files that you want to include in the package directory (in our case, the data has to reside in the roman/ directory). Add the field include_package_data=True in setup.py. Add the field package_data={'': [... patterns for files you want to include, relative to package dir...]} in setup.py .


2 Answers

I've used a utility method called data_file:

def data_file(fname):
    """Return the path to a data file of ours."""
    return os.path.join(os.path.split(__file__)[0], fname)

I put this in the init.py file in my project, and then call it from anywhere in my package to get a file relative to the package.

Setuptools offers a similar function, but this doesn't need setuptools.

like image 107
Ned Batchelder Avatar answered Nov 15 '22 20:11

Ned Batchelder


You should use the pkgutil/pkg_resources module to load the data files. It even works from within ziped eggs.

like image 44
Sebastian Noack Avatar answered Nov 15 '22 18:11

Sebastian Noack