By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,799 Members | 1,303 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,799 IT Pros & Developers. It's quick & easy.

Trying to Learn Packages

P: n/a
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
Share this Question
Share on Google+
3 Replies

P: n/a
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

P: n/a
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

P: n/a
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.