Trying to Learn Packages

Cédric Lucantis omer at no-log.org
Sun Jun 22 11:53:00 EDT 2008


Le Sunday 22 June 2008 16:07:37 Saul Spatz, vous avez écrit :
> Hi,
>
> I'm making a project into my first package, mainly for organization, but
> also to learn how to do it.  I have a number of data files, both
> experimental results and PNG files.  My project is organized as a root
> directory, with two subdirectories, src and data, and directory trees
> below them.  I put the root directory in my pythonpath, and I've no
> trouble accessing the modules, but for the data, I'm giving relative
> paths, that depend on the current working directory.
>
> This has obvious drawbacks, and hard-coding a directory path is worse.
> Where the data files are numbers, I can simply incorporate them in
> python scripts that initialize data structures , but what can I do with
> the image files?

For a small project you can do the same with images or any kind of data, for 
instance by serializing your objects with pickle in ascii mode and putting 
the result in a string constant (well, I never did it and can't guarantee it 
will work, this is only an example of what people do in many situations)

>
> What is the usual way of dealing with this?
>

A more conventional way is to provide a configure script to run before 
compiling/installing. That script should let the user choose where to install 
datafiles with a command line option such as --datadir=/path or provide a 
reasonable default value. Then it will auto-generate some code defining this 
value as a global variable, to make it accessible to the rest of your own 
code. Ideally, your app would also provide a command line option or an 
environment variable to override this hard-coded setting at runtime.

But maybe the distutils tools have some features for this, I don't know enough 
of them to tell that.

-- 
Cédric Lucantis



More information about the Python-list mailing list