469,955 Members | 2,576 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,955 developers. It's quick & easy.

Trying to Learn Packages

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?

Thanks,
Saul
Jun 27 '08 #1
3 931
On Sun, Jun 22, 2008 at 4:07 PM, Saul Spatz <ss****@kcnet.comwrote:
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.
Jun 27 '08 #2
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
Jun 27 '08 #3
Cédric Lucantis wrote:
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.
A couple of good ideas here. Thanks.
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Peter Saffrey | last post: by
reply views Thread by Rich Burridge | last post: by
13 posts views Thread by Iain King | last post: by
1 post views Thread by metaperl | last post: by
2 posts views Thread by erikcw | last post: by
65 posts views Thread by Chris Carlen | last post: by
1 post views Thread by SPE - Stani's Python Editor | last post: by
reply views Thread by Michael Matthews | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.