System Integrity Protection breaks DYLD_LIBRARY_PATH for python scripts I have some custom modules that are generated using SWIG that are installed in a a local path eg:

catkin_ws/devel/lib/python2.7/site-packages


When I try to run a python script that uses one of these modules, I get an import error:

"/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
ImportError: No module named _mag_manip


I've figured out that this is due to the shebang at the top of the python script:

#!/usr/bin/env python


Because /usr/bin/env is protected by System Integrity Protection (SIP), the DYLD_LIBRARY_PATH environment variable is not loaded and some modules cannot be found. A fix is to replace the shebang with the hardcoded path of python ie:

#!/usr/local/bin/python


Since my code runs on Linux and Mac OSX, replacing the normal python shebang with a hardcoded path on mac is not a really good solution. Does anybody know what the best solution is here?
Note: This only works in the Z shell (zsh).

Although I would normally say this is a bad idea, I can't see anything wrong with doing this with env, which is a pretty harmless command. Try copying env into /usr/local/bin and replacing /usr/bin/env python in the shebang with just env python (without the absolute path). Assuming you have /usr/local/bin somewhere near the beginning of your PATH variable on OS X, it'll pick up the relocated one, which will no longer be covered under SIP. That shebang will also work on Linux.