2

I'm new to Python and Django, And I'm trying to run a Django app which uses MySQL as a database,
I installed mysqlclient using pip, which it shows as Requirement already satisfied: mysqlclient in ./env/lib/python3.7/site-packages (1.4.4)

But when I run the project or try to create a superuser,
it throws me the following error -

Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$ ls
djangocrashcourse-master        env                             projectNameHere
Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$ source env/bin/activate
(env) Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$ pip install mysqlclient
Requirement already satisfied: mysqlclient in ./env/lib/python3.7/site-packages (1.4.4)
(env) Aniruddhas-MacBook-Pro:python_django aniruddhanarendraraje$ cd djangocrashcourse-master/
(env) Aniruddhas-MacBook-Pro:djangocrashcourse-master aniruddhanarendraraje$ python manage.py runserver
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/Users/aniruddhanarendraraje/Documents/work/pocs/pythonBasics/python_django/env/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 15, in <module>
    import MySQLdb as Database
  File "/Users/aniruddhanarendraraje/Documents/work/pocs/pythonBasics/python_django/env/lib/python3.7/site-packages/MySQLdb/__init__.py", line 18, in <module>
    from . import _mysql
ImportError: dlopen(/Users/aniruddhanarendraraje/Documents/work/pocs/pythonBasics/python_django/env/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so, 2): Library not loaded: @rpath/libmysqlclient.21.dylib
  Referenced from: /Users/aniruddhanarendraraje/Documents/work/pocs/pythonBasics/python_django/env/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so
  Reason: image not found

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

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 926, in _bootstrap_inner
    self.run()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/aniruddhanarendraraje/Documents/work/pocs/pythonBasics/python_django/env/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "/Users/aniruddhanarendraraje/Documents/work/pocs/pythonBasics/python_django/env/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "/Users/aniruddhanarendraraje/Documents/work/pocs/pythonBasics/python_django/env/lib/python3.7/site-packages/django/utils/autoreload.py", line 77, in raise_last_exception
    raise _exception[1]
  File "/Users/aniruddhanarendraraje/Documents/work/pocs/pythonBasics/python_django/env/lib/python3.7/site-packages/django/core/management/__init__.py", line 337, in execute
    autoreload.check_errors(django.setup)()
  File "/Users/aniruddhanarendraraje/Documents/work/pocs/pythonBasics/python_django/env/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "/Users/aniruddhanarendraraje/Documents/work/pocs/pythonBasics/python_django/env/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/aniruddhanarendraraje/Documents/work/pocs/pythonBasics/python_django/env/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
    app_config.import_models()
  File "/Users/aniruddhanarendraraje/Documents/work/pocs/pythonBasics/python_django/env/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "/Users/aniruddhanarendraraje/Documents/work/pocs/pythonBasics/python_django/env/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/aniruddhanarendraraje/Documents/work/pocs/pythonBasics/python_django/djangocrashcourse-master/posts/models.py", line 5, in <module>
    class Posts(models.Model):
  File "/Users/aniruddhanarendraraje/Documents/work/pocs/pythonBasics/python_django/env/lib/python3.7/site-packages/django/db/models/base.py", line 117, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/Users/aniruddhanarendraraje/Documents/work/pocs/pythonBasics/python_django/env/lib/python3.7/site-packages/django/db/models/base.py", line 321, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/aniruddhanarendraraje/Documents/work/pocs/pythonBasics/python_django/env/lib/python3.7/site-packages/django/db/models/options.py", line 204, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/Users/aniruddhanarendraraje/Documents/work/pocs/pythonBasics/python_django/env/lib/python3.7/site-packages/django/db/__init__.py", line 28, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Users/aniruddhanarendraraje/Documents/work/pocs/pythonBasics/python_django/env/lib/python3.7/site-packages/django/db/utils.py", line 201, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/aniruddhanarendraraje/Documents/work/pocs/pythonBasics/python_django/env/lib/python3.7/site-packages/django/db/utils.py", line 110, in load_backend
    return import_module('%s.base' % backend_name)
  File "/Users/aniruddhanarendraraje/Documents/work/pocs/pythonBasics/python_django/env/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/aniruddhanarendraraje/Documents/work/pocs/pythonBasics/python_django/env/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 20, in <module>
    ) from err
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

After adding the following to __init__.py-

import pymysql
pymysql.install_as_MySQLdb()

enter image description here

2
  • install mysqlclient Commented Aug 31, 2019 at 15:15
  • I already have MySql in my computer, which I'm using in other languages, will this override my Db settings or just add a python client for MySql? Commented Aug 31, 2019 at 15:17

1 Answer 1

2

Install mysqlclient on virtualenv by which you run your django project

Install mysqlclient and pymysql

pip install pymysql
pip install mysqlclient==1.4.4

Then add following lines on project root (belong with settings.py) __init__.py

import pymysql
pymysql.install_as_MySQLdb()

Example project structure

├── manage.py
├── stackoverflow
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── templates
Sign up to request clarification or add additional context in comments.

7 Comments

I add these import pymysql pymysql.install_as_MySQLdb() in __init__.py file?
Yes. these 2 lines in __init__.py
Now I'm getting a django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
I have installed an old version. Install the new version by pip install mysqlclient==1.3.13
did pip install mysqlclient==1.4.4 but getting the same error, do I need set any PATH or something?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.