[Distutils] EasyInstall: Issue: wrapper script weirdness

Ryan Tomayko rtomayko at gmail.com
Mon Jun 20 05:32:35 CEST 2005

I was able to package py.test (and the rest of py.lib) into an egg  
without much issue. I ran into a small problem with the py.test  
script, however, and the issue could effect other scripts as well.

There is a main script, "py.test", that is very short::

     from _Findlay import py

py.lib is packaged such that "_findpy.py" is installed as a script so  
it sits next to py.test under PKG-INFO/scripts and a wrapper is  
generated at [script-dir]/_findpy.py as well. _findpy just does some  
sys.path munging on import and then brings in the py module.

For clarity, we have a set of files that look like this (on my machine):

/usr/local/bin/py.test                  (setuptools wrapper)
/usr/local/bin/_findpy.py               (setuptools wrapper)
py.test.egg/PKG-INFO/scripts/py.test    (actual script)
py.test.egg/PKG-INFO/scripts/_findpy.py (actual module)

When I try to run the script using the setuptools generated wrapper I  

     ImportError: cannot import name py

<bonus points if you figured it out already>

This is happening on the "from _findpy import py" line in PKG-INFO/ 
scripts/py.test. The text of ImportError looked more like a circular  
import problem than a "module not found" problem so I checked it out  
and the PKG-INFO/scripts/_findpy module is indeed being loaded and  
executed and it is finding the "py" package okay.

What's happening is that '/usr/local/bin' comes before 'PKG-INFO/ 
scripts' on sys.path so that execution moves something like this:

   1. `/usr/local/bin/py.test`
   2. execfile `PKG-INFO/scripts/py.test`
   3. import _findpy (found at `/usr/local/bin/_findpy.py`)
   4. execfile `PKG-INFO/scripts/_findpy.py`

Step three is bad.

It would probably be best to make py.lib's scripts a little less  
complex (installing _findpy.py as a script doesn't strike me as an  
elegant approach). On the other hand, the way setuptools lays out  
sys.path when wrapper scripts are invoked results in unexpected  
behavior. If a script is importing a module it believes to be in the  
same directory, it is definitely not wanting to import the wrapper  
version. It may be best to remove the wrapper script directory from  
sys.path entirely or at least push it back behind the PKG-INFO/ 
scripts directory. I can't think of a single scenario where a wrapped  
script would want to import another wrapper.

I haven't tried anything yet but will fire off a patch if I get in  
there to have a look.

Ryan Tomayko
                                  rtomayko at gmail.com

More information about the Distutils-SIG mailing list