1

The "MySQL" client interface library will not load on MacOS Monterey. The relevant traceback lines are as follows: (System is Django, latest versions of everything.)

ImportError: dlopen(/Users/mike/.virtualenvs/djangoprod/lib/python3.11/site-packages/MySQLdb/_mysql.cpython-311-darwin.so, 0x0002): Library not loaded: '@rpath/libmysqlclient.21.dylib'
  Referenced from: '/Users/mike/.virtualenvs/djangoprod/lib/python3.11/site-packages/MySQLdb/_mysql.cpython-311-darwin.so'
  Reason: tried: '/usr/lib/libmysqlclient.21.dylib' (no such file)

It seems to me that Python is trying to use @rpath to find the library, and man dyld explains to me just what that is. So far, so good ...

I have specified export DYLD_LIBRARY_PATH=/usr/local/mysql-8.0.31-macos12-X86_64/lib, which is the correct location, but the library is still not being found. (Notice from the tail of line #2 that it appears to be using @rpath explicitly as part of the library path being searched.) I have read other unrelated forum postings which provide DYLD_LIBRARY_PATH as a solution, albeit in the Linux environment.

I don't know how to actually see what @rpath contains.

I do know that "this used to work," but it's been a while since I had anything to do with this application and I frankly don't recall which MacOS version I was using at that time.

Is it possible that "System Integrity Protection" has anything to do with this? I read that in certain cases that it blocks this environment variable, but it is not entirely clear to me when it does and doesn't apply.

The Python3 interpreter in question is located in a "virtual environment" which means that it is actually running out of a user-local directory which therefore would not be covered by SIP. But the libraries themselves do reside in /usr, which is. I did rebuild that virtual environment.

9
  • Yes SIP does stop DYLD_LIBRARY_PATH. If you unset it what is the error?
    – mmmmmm
    Commented Nov 19, 2022 at 9:42
  • Install the library inside the virtualenv. Commented Nov 19, 2022 at 14:43
  • An interesting idea, Marc ... how does one do that, please? I did, of course, install the "mysqlclient" package, which is the correct one for Python3. But I do not know how one installs a particular library within a VEnv if the package installer does not do so already. Enlighten me, please. Reference to a proper web-page would be just fine. Commented Nov 19, 2022 at 18:00
  • I will try that, mmmmmmm. But, can you please point me to any online resources which tell me more about "how and when" SIP does this? man dyld only says, cryptically: "If System Integrity Protection is enabled, these environment variables are ignored when executing binaries protected by System Integrity Protection." Without plainly saying exactly what those are – and it seems unlikely that the "python3" executable – which is coming from a subdirectory of my home – would qualify. Commented Nov 19, 2022 at 18:02
  • I am grateful for these responses and looking for more, because "I am at my wit's end and up against a very looming deadline – while this issue has me at a dead-stop." Thanks folks. (To clarify, Marc: I see how to install a package, but how does one (separately) install a library? I don't have any idea (yet). Some tutorials seem to use the term "library" and "package" interchangeably, which for our purposes is misleading. Commented Nov 19, 2022 at 18:03

1 Answer 1

1

It appears that System Integrity Protection (SIP) was the root cause of the problem. Turning it off caused the library to be found, once it had been added with export DYLD_LIBRARY_PATH ...etc...

Instructions and explanation can be found here: https://developer.apple.com/documentation/security/disabling_and_enabling_system_integrity_protection

According to the Apple Developer website, this causes the value of DYLD_LIBRARY_PATH to be denied to child processes. And apparently, the Python interface for MySQL must use one somewhere. (How doing such an odd thing would benefit "system integrity" is quite beyond me, but Apple must have its reasons. ...)

FYI: I also found out from Apple's man dyld page what @RPATH means and where its value comes from.

2
  • The benefit is that the dynamic library being called can't be replaced by an arbitrary one that maight have malicious code in it.
    – mmmmmm
    Commented Nov 26, 2022 at 22:32
  • Granted. And I know that MacOS "Applications" are usually self-contained packages which are signed. But it caught me off-guard that the handling of the DYLD_LIBRARY_PATH environment variable would be affected ... and then, "affected in this particular way." I still can't wrap my cranium about this particular aspect of SIP, and I'm sorry also that I have to "disable SIP entirely" to get around it. Commented Nov 27, 2022 at 23:48

You must log in to answer this question.

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