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

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

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-25 07:31:29150browse

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.

The above is the detailed content of How to Fix \"ImproperlyConfigured: Error loading MySQLdb module\" in Django on macOS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn