1

I have an executable that uses some dynamic libraries, and it runs successfully from an OSX terminal when 'DYLD_LIBRARY_PATH' is correctly set:

airbook:layoutSpaceTest daniel$ echo $DYLD_LIBRARY_PATH
/Developer/NVIDIA/CUDA-7.5/lib:/Developer/OptiX/lib64
airbook:layoutSpaceTest daniel$ heliosim -f degraded.sunshapein
'HelioSim'  Port: 1200 with 1 threads
'HelioSim' ENDED

However I cannot run the executable in python as it doesn't pick up 'DYLD_LIBRARY_PATH' or 'LD_LIBRARY_PATH':

airbook:layoutSpaceTest daniel$ python
Python 2.7.10 (default, Aug 22 2015, 20:33:39)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os,sys
>>> os.environ["DYLD_LIBRARY_PATH"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/UserDict.py", line 23, in __getitem__
    raise KeyError(key)
KeyError: 'DYLD_LIBRARY_PATH'

I have read that /usr/bin/env python strips DYLD_LIBRARY_PATH and LD_LIBRARY_PATH from the imported environment. Is there a way to prevent this?

Any help would be greatly appreciated!

(python 2.7, OSX el capitan)

psilouette
  • 83
  • 1
  • 6
  • Thanks. Yes I see now that subprocess.Popen(my_command, env=my_env) or os.exevc will work. However, why do I need to reset my DYLD_LIBRARY_PATH when it is already set before I enter python? I have read that /usr/bin/env python strips DYLD_LIBRARY_PATH and LD_LIBRARY_PATH... – psilouette Jun 05 '16 at 02:25
  • And that comment should be clarifies in your question. – pppery Jun 05 '16 at 12:56
  • @bhargav-rao How is this a duplicate of https://stackoverflow.com/questions/2231227? This question asks specifically about DYLD_LIBRARY_PATH not being inherited by python on macOS (due to System Integrity Protection). That other question is about modifying arbitrary environment flags in sub-processes. – Mark Gates May 16 '18 at 13:19
  • 1
    DYLD_LIBRARY_PATH is not inherited by programs in /usr/bin due to macOS System Integrity Protection (SIP), cf. Apple's documentation of [SIP](https://support.apple.com/en-us/HT204899) and [Runtime Protections](https://developer.apple.com/library/content/documentation/Security/Conceptual/System_Integrity_Protection_Guide/RuntimeProtections/RuntimeProtections.html). The easiest workaround is to use Python installed from [python.org](https://www.python.org/downloads/mac-osx/), which is not protected by SIP. – Mark Gates May 16 '18 at 13:33
  • Another possible workaround is, in python, to set os.environ['DYLD_LIBRARY_PATH'] = os.environ['LIBRARY_PATH'], if all the paths are added to both. $LIBRARY_PATH is used by compilers at link time, while $DYLD_LIBRARY_PATH is used by the loader at run time. – Mark Gates May 16 '18 at 14:55

0 Answers0