[Distutils] Script-specific .pth files?

Phillip J. Eby pje at telecommunity.com
Sat Feb 11 22:38:59 CET 2006

At 04:19 PM 2/11/2006 -0500, Jim Fulton wrote:

>Here's a nutty idea I thought of when thinking about some of the
>recent back and forth.
>I realized, when thinking about "plugins" versus "scripts" is
>that a key use case is getting a script installed so that it will
>run. A script comes from an egg, which depends on other eggs, and
>so on.  (Of course, this gets more exciting for complex script,
>like, say, Zope. :)
>It occurs to me that, when a script is installed, easy_install
>could generate a .pth file that named exactly those eggs the script
>needed. A wrapper script could be generated that used this .pth file.

It'd have to be some other extension, to avoid confusing a normally-started 
Python that happens to be processing .pth files in that directory.

The code could be as simple as:

    sys.path[:0]=[line.strip() for line in open(__file__+'.eggs','r')]

>There wouldn't be any need for run-time analysis.  The script wouldn't
>need to manipulate PYTHONPATH to find the needed eggs, as all of the
>needed eggs would be captured in the script-specific .pth file.
>Rather than determining the working set at run time, the working set
>would be determined at install time.
>I see a number of advantages to this approach:
>- No complicated run-time path gymnastics
>- Faster start-up, as there is no run-time searching for eggs
>    or analysis of which eggs to use

It's not actually going to eliminate the processing, unless the importing 
of the entry point gets hardcoded into the script, such that it no longer 
uses the entry point mechanism to locate the entry point at runtime.  I'm 
not sure I want to do that, in part because this data is more fragile if 
you move things around.  Right now, you can upgrade a dependency of a 
package without needing to reinstall the depender, because the paths get 
fixed up at runtime.  Skipping the dynamic resolution would break if you 
remove an older version of something on upgrade.

>- More determinstic behavior.  An installed script will always
>    use the same eggs.  The human who installed the script can inspect
>    the .pth file to see exactly what's being used.  They can even modify
>    it if the feel so bold.

You lose the ability to override PYTHONPATH-supplied paths this way, so 
it's a bit less backward-compatible.  On the other hand, scripts are 
hardwired to a specific version of the eggs and will barf if you use 
PYTHONPATH to override them anyway.  Hm.  This just might work.

More information about the Distutils-SIG mailing list