views:

316

answers:

3

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 ?

I hope somebody can help... Thanks...

//Regards Jonas Finnemann Jensen

+3  A: 

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.

Ned Batchelder
How will this work when data_files are installed in /usr/share/AppName/themes/And my executable is in something like /usr/lib/AppName I'd assume...DistUtils documentation says it'll be installed with the sys.prefix as prefix for the relative path that I gave the data files to be stored in... On *nix systems data files should probably also be in /usr/share/AppName/
jopsen
I've used this with a package_data option in setup.py. Not sure about data_files.
Ned Batchelder
Okay... Yeah, package_data is also an option... Maybe I should go with that... I just don't think it's going to generate Debian compliant packages :)
jopsen
+1  A: 

You could try pkg_resources:

my_data = pkg_resources.resource_string(__name__, fname)
J.F. Sebastian
A: 

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

Sebastian Noack