wxPython install breaks sys.path ordering for python.org 2.7.2 on Mac OS X 10.7.3

I reported a bug against python that turns out to be a problem with wxPython
installation.

It is critical that the order of the site-packages folders is not reversed
because binary extensions (e.g. pysvn) cannot be shared between Apple and Python.org
python.

I should be able to install one pysvn for Apple python and another for Python.org.
But because the sys.path is broken by wxPython install python.org ends up seeing
the Apple python binaries and SEGV's.

In python.org 2.7.2 before installing wxPython 2.8.10.1 sys.path is:

['',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages',
'/Library/Python/2.7/site-packages']

After installing it is:

['',
'/usr/local/lib/wxPython-unicode-2.8.12.1/lib/python2.7/site-packages',
'/usr/local/lib/wxPython-unicode-2.8.12.1/lib/python2.7/site-packages/wx-2.8-mac-unicode',
'/usr/local/lib/wxPython-unicode-2.8.12.1/lib/python2.7',
'/Library/Python/2.7/site-packages',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages']

Notice that '/Library/Python/2.7/site-packages' has been more from last to near the start of sys.path.

Apple python uses '/Library/Python/2.7/site-packages' as its site-packages folder. Here is its
sys.path:

['',
'/usr/local/lib/wxPython-unicode-2.8.12.1/lib/python2.7/site-packages',
'/usr/local/lib/wxPython-unicode-2.8.12.1/lib/python2.7/site-packages/wx-2.8-mac-unicode',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
'/Library/Python/2.7/site-packages',
'/usr/local/lib/wxPython-unicode-2.8.12.1/lib/python2.7']

Do you have a patch to fix this?

Barry

When the current installation scheme was being developed the preference of most developers was that a single wxPython install could be used by either a Python.org python or the Apple python. I expect that this will be changing with Phoenix, but I don't know how yet.

This is all done via .pth files and adding /usr/local/lib/wxPython-{WXVER}/lib/python{PYVER} as a "sitedir". If you want to take a closer look at what is happening then start in /Library/Python/2.7/site-packages/wxredirect.pth. (There is also a copy in the Python.org site-packages.) It adds the new sitedir. Then later /usr/local/lib/wxPython-{WXVER}/lib/python{PYVER}/wxhack.pth will be loaded and it will add the actual package folders for wxPython and also import and call a function that will massage the path a little. It is located in the same place and is called wxhack.py.

The wxhack.py module basically moves the newly added path dirs to be further up the list than at the end. This was needed because Apple's install of wxPython would always be found first otherwise. I didn't want to just move them to the beginning because then it couldn't be overridden by setting PYTHONPATH to something else.

From the sys.path lists in your message it appears that in at least the Python.org case that too much is being moved, or something like that. All that was intended to be moved are the two paths added to the end of the list by the wxhack.pth file. If you'd like to try and debug this then the wxhack.py module would be the place to do it. I tried to make it work generically, but perhaps it would be better to just have some code that detects which type of Python it is being run by and then do something different in each case. Or maybe instead of just moving 'num' items from the end it should search for items with the expected prefix and only move them.

If you can experiment a little and see what works well in your use cases and send me a patch then I can try it out with other use cases I know of to make sure that it still handles those ok as well.

···

On 3/4/12 3:12 AM, Barry Scott wrote:

I reported a bug against python that turns out to be a problem with wxPython
installation.

It is critical that the order of the site-packages folders is not reversed
because binary extensions (e.g. pysvn) cannot be shared between Apple and Python.org
python.

I should be able to install one pysvn for Apple python and another for Python.org.
But because the sys.path is broken by wxPython install python.org ends up seeing
the Apple python binaries and SEGV's.

--
Robin Dunn
Software Craftsman

The problem is that fixpath() is called twice. The fix I use is to force the function
to only do its work on the first call. With this patch pysvn works as expected.

Barry

--- wxhack.py.orig 2012-03-07 11:01:44.000000000 +0000
+++ wxhack.py 2012-03-07 11:01:15.000000000 +0000
@@ -5,12 +5,20 @@

import sys

+__path_fixed = False

···

On 5 Mar 2012, at 23:50, Robin Dunn wrote:

If you can experiment a little and see what works well in your use cases and send me a patch then I can try it out with other use cases I know of to make sure that it still handles those ok as well.

+
def fixpath(pyver, num):
     """
     Look for the position in the sys.path of the std library zip file
     and move the last num items to that position. This puts us before
     the stock paths, but after any PYTHONPATH settings or eggs.
     """
+ global __path_fixed
+ if __path_fixed:
+ return
+
+ __path_fixed = True
+
     if '.' in pyver:
         pyver = ''.join(pyver.split('.'))
     tail = 'python%s.zip' % pyver

If you can experiment a little and see what works well in your use cases and send me a patch then I can try it out with other use cases I know of to make sure that it still handles those ok as well.

The problem is that fixpath() is called twice.

Ah, that explains something I was seeing too.

The fix I use is to force the function
to only do its work on the first call. With this patch pysvn works as expected.

Thanks!

···

On 3/7/12 3:05 AM, Barry Scott wrote:

On 5 Mar 2012, at 23:50, Robin Dunn wrote:

--
Robin Dunn
Software Craftsman