Trying to Learn Packages

David wizzardx at gmail.com
Sun Jun 22 17:49:29 CEST 2008


On Sun, Jun 22, 2008 at 4:07 PM, Saul Spatz <sspatz at kcnet.com> wrote:
> 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?
>
> What is the usual way of dealing with this?
>

The usual method (afaik) is to use relative paths, and to not change
into the script dirs before running them.

eg, instead of:

cd src/dir1/dir2/dir3
./script.py

You run it like this:
./src/dir1/dir2/dir3/script.py

And then all the scripts under src can load data files using a path
like this: './data/datafile'

Another method is to use a file-finder func which looks in various
places (using PATH, PYHONPATH, etc), and returns the first-found path
to the file.



More information about the Python-list mailing list