0
votes

I am setting up a Django backend for my application. I am trying to change the default sqlite database to mysql. While performing the first migration, I get the following error:

Traceback (most recent call last): File "/Users/user1/djangoApp/djangoProject/env/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 15, in import MySQLdb as Database File "/Users/user1/djangoApp/djangoProject/env/lib/python3.6/site-packages/MySQLdb/init.py", line 18, in import _mysql ImportError: dlopen(/Users/user1/djangoApp/djangoProject/env/lib/python3.6/site-packages/_mysql.cpython-36m-darwin.so, 2): Library not loaded: @rpath/libmysqlclient.21.dylib Referenced from: /Users/user1/djangoApp/djangoProject/env/lib/python3.6/site-packages/_mysql.cpython-36m-darwin.so Reason: image not found

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "manage.py", line 15, in execute_from_command_line(sys.argv) File "/Users/user1/djangoApp/djangoProject/env/lib/python3.6/site-packages/django/core/management/init.py", line 381, in execute_from_command_line utility.execute() File "/Users/user1/djangoApp/djangoProject/env/lib/python3.6/site-packages/django/core/management/init.py", line 357, in execute django.setup() File "/Users/user1/djangoApp/djangoProject/env/lib/python3.6/site-packages/django/init.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "/Users/user1/djangoApp/djangoProject/env/lib/python3.6/site-packages/django/apps/registry.py", line 112, in populate app_config.import_models() File "/Users/user1/djangoApp/djangoProject/env/lib/python3.6/site-packages/django/apps/config.py", line 198, in import_models self.models_module = import_module(models_module_name) File "/Users/user1/djangoApp/djangoProject/env/lib/python3.6/importlib/init.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 994, in _gcd_import File "", line 971, in _find_and_load File "", line 955, in _find_and_load_unlocked File "", line 665, in _load_unlocked
File "", line 678, in exec_module File "", line 219, in _call_with_frames_removed File "/Users/user1/djangoApp/djangoProject/env/lib/python3.6/site-packages/django/contrib/auth/models.py", line 2, in from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager File "/Users/user1/djangoApp/djangoProject/env/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 47, in class AbstractBaseUser(models.Model): File "/Users/user1/djangoApp/djangoProject/env/lib/python3.6/site-packages/django/db/models/base.py", line 101, in new new_class.add_to_class('_meta', Options(meta, app_label)) File "/Users/user1/djangoApp/djangoProject/env/lib/python3.6/site-packages/django/db/models/base.py", line 305, in add_to_class value.contribute_to_class(cls, name) File "/Users/user1/djangoApp/djangoProject/env/lib/python3.6/site-packages/django/db/models/options.py", line 203, in contribute_to_class self.db_table = truncate_name(self.db_table, connection.ops.max_name_length()) File "/Users/user1/djangoApp/djangoProject/env/lib/python3.6/site-packages/django/db/init.py", line 33, in getattr return getattr(connections[DEFAULT_DB_ALIAS], item) File "/Users/user1/djangoApp/djangoProject/env/lib/python3.6/site-packages/django/db/utils.py", line 202, in getitem backend = load_backend(db['ENGINE']) File "/Users/user1/djangoApp/djangoProject/env/lib/python3.6/site-packages/django/db/utils.py", line 110, in load_backend return import_module('%s.base' % backend_name) File "/Users/user1/djangoApp/djangoProject/env/lib/python3.6/importlib/init.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/Users/user1/djangoApp/djangoProject/env/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 20, in ) from err django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.

  • OS:macOS Mojave
  • Framework: Django
  • I am using a virtualenv

I have already installed "mysqlclient" using the command

pip3 install mysqlclient

I have verified that the mysqlclient folder exists under the installed location.

I have the mysql server up and running.

I have tried many available solutions online but nothing works

To reproduce:

1 - Start a django project using

"django-admin startproject djangoApp"

2 - Install and start the mysql server.

3 - Update the settings.py file of the entry application to the following

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'djangoApp',
        'USER': 'root',
        'PASSWORD': 'password',
        'HOST': 'localhost',
        'PORT': ''
    }
}

4 - Now try to migrate using

python manage.py migrate

Expected:The migration should be successful

Actual:Migration fails with the given error

I am new to Django

1
P.S. djangoApp is name of my django project as well as my databaseAshm Walia
have you run "python manage.py makemigrations" ?Mohit Harshan
The traceback is showing that you did not install Django in a virtualenv. Where is the virtualenv, how did you activate it, and was it activated when you installed both Django and mysqlclient?Daniel Roseman
@Mohit Harshan I am not able to run that command eitherAshm Walia
@DanielRoseman Thank you for pointing that out. I posted the incorrect traceback. I activated it using the command "source env/bin/activate "I get the same traceback when I run the command in my virtualenv.Ashm Walia

1 Answers

0
votes

I needed to symlink the lib files to the /usr/local/lib directory.

Fourth answer in the link worked for me

Thank you @Mohit Harshan @Daniel Rosema for taking time out to help me