"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Fix \"ImproperlyConfigured: Error loading MySQLdb module\" in Django on macOS?

How to Fix \"ImproperlyConfigured: Error loading MySQLdb module\" in Django on macOS?

Published on 2024-11-21
Browse:923

How to Fix \

MySQL Improperly Configured: The Problem with Relative Paths

When running python manage.py runserver in Django, you may encounter the following error:

ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Library/Python/2.7/site-packages/_mysql.so
  Reason: unsafe use of relative rpath libmysqlclient.18.dylib in /Library/Python/2.7/site-packages/_mysql.so with restricted binary

The Cause

This error occurs due to Apple's implementation of System Integrity Protection in OS X El Capitan (10.11). This prevents programs in protected locations like /usr from accessing shared libraries using relative references.

In this case, the shared library _mysql.so contains a relative reference to libmysqlclient.18.dylib.

The Solution

To resolve this issue, you will need to force _mysql.so to use an absolute reference to libmysqlclient.18.dylib. This can be achieved using the install_name_tool utility.

Steps to Resolve the Issue

  1. Ensure that libmysqlclient.18.dylib is located in /usr/local/mysql/lib/.
  2. Run the following command in Terminal:
sudo install_name_tool -change libmysqlclient.18.dylib \
  /usr/local/mysql/lib/libmysqlclient.18.dylib \
  /Library/Python/2.7/site-packages/_mysql.so

This command will update the shared library reference in _mysql.so to use the absolute path to libmysqlclient.18.dylib.

After running this command, you should be able to successfully run python manage.py runserver without the MySQL configuration error.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3