On 25 Oct, 2012, at 16:50, Tom Epperly <epper...@llnl.gov> wrote:

> I am trying to work with Apple Mountain Lion's install of Python 2.7. I
> have a language interoperability tool, Babel
> http://www.llnl.gov/CASC/components/, that used embedded Python when
> other languages are calling Python (c.g., C++ calling Python). My
> fundamental problem is that sys.path is not being initialized the same
> when I dlopen(libpython2.7.dylib) and initialize Python compared with
> how the sys.path is set when the Python executable is called directly.
> This causes Python to fail to load the numpy module used by Babel.
> 
> bash-3.2$ /usr/bin/python2.7 -c "import sys; print sys.path; import
> numpy">  /tmp/out1
> bash-3.2$ /usr/bin/python -c "import sys; print sys.path; import numpy"
>> /tmp/out2
> bash-3.2$ cat /tmp/out1
> ['',
> '/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']
> bash-3.2$ diff /tmp/out1 /tmp/out2
> bash-3.2$ ls -al /usr/bin/python2.7
> lrwxr-xr-x  1 root  wheel  75 Aug 23 11:10 /usr/bin/python2.7 ->
> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
> 
> Presumably, this C program that uses dlopen(), Py_Initialize, and
> Py_SimpleString should have exactly the same output.
> 
> /** foo.c */
> #include<dlfcn.h>
> #include<stdio.h>
> 
> int
> main(int argc, char **argv)
> {
>  // void *lptr =
> dlopen("/System/Library/Frameworks/Python.framework/Python", RTLD_NOW |
> RTLD_GLOBAL);
>  void *lptr =
> dlopen("/System/Library/Frameworks/Python.framework/Versions/2.7/lib/libpython2.7.dylib",
> RTLD_NOW | RTLD_GLOBAL);
>  if (lptr) {
>    void (*pyinit)(void) = dlsym(lptr, "Py_Initialize");
>    if (pyinit) {
>      int (*runSimple)(const char *);
>      (*pyinit)();              /* initialize Python */
>      runSimple = dlsym(lptr, "PyRun_SimpleString");
>      if (runSimple) {
>        (*runSimple)("import sys ; print sys.path; import numpy");
>      }
>    }
>    else {
>      fprintf(stderr, "Unable to locate Py_Initialize: %s\n", dlerror());
>    }
>  }
>  else {
>    fprintf(stderr, "Error loading Python shared library: %s\n",
> dlerror());
>  }
> }
> 
> bash-3.2$ gcc foo.c ; ./a.out
> ['/usr/lib/python27.zip', '/usr/lib/python2.7',
> '/usr/lib/python2.7/plat-darwin', '/usr/lib/python2.7/plat-mac',
> '/usr/lib/python2.7/plat-mac/lib-scriptpackages',
> '/usr/Extras/lib/python', '/usr/lib/python2.7/lib-tk',
> '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload']
> Traceback (most recent call last):
>  File "<string>", line 1, in<module>
> ImportError: No module named numpy
> 
> However as you see, it has a completely different sys.path. I can't seem
> to find a way to get it to initialize Python with the same sys.path. It
> seems like the libpython2.7.dylib is broken. I am at a loss on how to
> fix this or even who to ask for help.

Not sure why this doesn't work, py2app uses simular code and that works fine.

What's really odd: when I run this on my machine and print sys.prefix it shows 
that sys.prefix is /Library/Frameworks/Python.framework instead of /System/.... 
as I'd expect. When I move the Python framework in /Library/Frameworks aside 
the program gives the same output as your program, with sys.prefix set to 
'/usr'.

Apple publishes the source code for the python version they ship, and when I 
look at 
<http://www.opensource.apple.com/source/python/python-60.3/2.7/fix/getpath.c.ed>
 I get the impression that they changed the way sys.prefix is located.   Stock 
CPython uses a deprecated API and they appear to have switch that to using 
dlopen API. 

Ronald



> 
> Regards,
> 
> Tom Epperly
> 
> _______________________________________________
> Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
> http://mail.python.org/mailman/listinfo/pythonmac-sig
> unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG

_______________________________________________
Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
http://mail.python.org/mailman/listinfo/pythonmac-sig
unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG

Reply via email to