I have a Python project in which I am using many non-code files. Currently these are all images, but I might use other kinds of files in the future. What would be a good scheme for storing and referencing these files?
I considered just making a folder "resources" in the main directory, but there is a problem; Some images are used from within sub-packages of my project. Storing these images that way would lead to coupling, which is a disadvantage.
Also, I need a way to access these files which is independent on what my current directory is.
You can always have a separate "resources" folder in each subpackage which needs it, and use
os.path
functions to get to these from the__file__
values of your subpackages. To illustrate what I mean, I created the following__init__.py
file in three locations:Here's the
__init__.py
file:In c:\temp\work, I create an app, topapp.py, as follows:
This respresents the application using the
topp
package and subpackages. Then I run it:That's as expected. We set the PYTHONPATH to simulate having our package on the path:
As you can see, the resource paths resolved correctly to the location of the actual (sub)packages on the path.
Update: Here's the relevant py2exe documentation.
You may want to use
pkg_resources
library that comes withsetuptools
.For example, I've made up a quick little package
"proj"
to illustrate the resource organization scheme I'd use:Notice how I keep all resources in a separate subpackage.
"code.py"
shows howpkg_resources
is used to refer to the resource objects:If you run it, you get:
If you need to treat a resource as a fileobject, use
resource_stream()
.The code accessing the resources may be anywhere within the subpackage structure of your project, it just needs to refer to subpackage containing the images by full name:
proj.resources.images
, in this case.Here's
"setup.py"
:Caveat: To test things "locally", that is w/o installing the package first, you'll have to invoke your test scripts from directory that has
setup.py
. If you're in the same directory ascode.py
, Python won't know aboutproj
package. So things likeproj.resources
won't resolve.@ pycon2009, there was a presentation on distutils and setuptools. You can find all of the videos here
Eggs and Buildout Deployment in Python - Part 1
Eggs and Buildout Deployment in Python - Part 2
Eggs and Buildout Deployment in Python - Part 3
In these videos, they describe how to include static resources in your package. I believe its in part 2.
With setuptools, you can define dependancies, this would allow you to have 2 packages that use resources from 3rd package.
Setuptools also gives you a standard way of accessing these resources and allows you to use relative paths inside of your packages, which eliminates the need to worry about where your packages are installed.