Making code run in both source tree and installation path

Carl Banks pavlovevidence at gmail.com
Wed Jul 1 02:04:15 EDT 2009


On Jun 29, 9:20 am, Javier Collado <javier.coll... at gmail.com> wrote:
> I've seen different approaches:
> - distutils trick in setup.py to modify the installed script (i.e.
> changing a global variable value) so that it has a reference to the
> data files location.


One of my biggest complaints about distutils is that it doesn't do
this, a limitation made worse by the fact that distutils allows you to
specify an alternate data file directory, but your scripts have no way
to know what that alternate directory is installed.  Which really
limits the usefulness of that functionality.

The most common way I've seen people work around this issue is to
throw their data files into the package directories.  Yuck.

At one point I hacked up a setup.py file to look in the distutils data
structure and pull out the data install location, and wrote out a
trivial python file listing that location.  (The distutils build data
structure is helpfully returned by setup function.)  I never felt good
about it, and it wouldn't work if the install was done in steps (such
as build as user, install as superuser).

If you care to navigate the murky internals of distutils to do this in
a more elegant way, more power to you, and if so I'd recommend doing
it that way.


> - Heuristic in the package code to detect when it's being executed
> from the source tree and when it has been the installed
> - Just using an environment variable that the user must set according
> to his needs

I guess I'd combine these two.  Make a sensible guess about where the
data files are by checking out the environment, but if the data files
aren't there (such as if the user installs to a different data
location) then they are responsible for setting an env variable or
config option.



> I guess that there are other options, for example, maybe using
> buildout. What would you say it's the best/more elegant option to
> solve this problem?

Another option is not to use distutils at all.  If you are writing an
application, I think that would be a good idea.  I don't think
applications really need to be located in site-packages.

If you have any C-extensions it might complicate this.


Carl Banks



More information about the Python-list mailing list