2

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?

  • 1
    Isn't the issue that DYLD_LIBRARY_PATH should better not be used anymore? This certainly refers to how the package was built and how the linking parameters were set. This illuminative SO answer helped me a lot to understand related issues. – normanius Dec 7 '17 at 23:33
1

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.

  • Don't think that works because if I just put env in the shebang, I get: env: bad interpreter: No such file or directory – user3293204 Jun 9 '17 at 13:50
  • Sorry, I meant env python. – William T Froggard Jun 9 '17 at 18:21
  • That works in zsh but barfs in ksh and bash. – fd0 Jun 9 '17 at 18:29
  • 1
    Oh my, I had completely forgotten about the behavior of BASH. Whoops. ZSH really is so much more convenient, for this and countless other reasons. In any case, I'll leave this up for Z-Shell users. – William T Froggard Jun 9 '17 at 18:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .