LoginSignup
1
1

More than 1 year has passed since last update.

Did you install mysqlclient?って聞かれたらどうすればいい?

Posted at

目的

いつも通りDJangoRestframeworkでAPIの作成に励んでいて、ふとしたことから仮想環境をanacondaからvirtualenvに変更しようとして色々とインストールし直していた。その時に事件は起こった。

いつも通りpython manage.py runserverでローカルサーバーを起動しようとした。
するとこのようなエラーが出てきた。。

 File "/Users/Yuto/dev/api_practice/tinder_api/tinder-venv/lib/python3.8/site-packages/django/contrib/auth/base_user.py", line 48, in <module>
    class AbstractBaseUser(models.Model):
  File "/Users/Yuto/dev/api_practice/tinder_api/tinder-venv/lib/python3.8/site-packages/django/db/models/base.py", line 122, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "/Users/Yuto/dev/api_practice/tinder_api/tinder-venv/lib/python3.8/site-packages/django/db/models/base.py", line 326, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/Yuto/dev/api_practice/tinder_api/tinder-venv/lib/python3.8/site-packages/django/db/models/options.py", line 207, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/Users/Yuto/dev/api_practice/tinder_api/tinder-venv/lib/python3.8/site-packages/django/utils/connection.py", line 15, in __getattr__
    return getattr(self._connections[self._alias], item)
  File "/Users/Yuto/dev/api_practice/tinder_api/tinder-venv/lib/python3.8/site-packages/django/utils/connection.py", line 62, in __getitem__
    conn = self.create_connection(alias)
  File "/Users/Yuto/dev/api_practice/tinder_api/tinder-venv/lib/python3.8/site-packages/django/db/utils.py", line 204, in create_connection
    backend = load_backend(db['ENGINE'])
  File "/Users/Yuto/dev/api_practice/tinder_api/tinder-venv/lib/python3.8/site-packages/django/db/utils.py", line 111, in load_backend
    return import_module('%s.base' % backend_name)
  File "/opt/anaconda3/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/Users/Yuto/dev/api_practice/tinder_api/tinder-venv/lib/python3.8/site-packages/django/db/backends/mysql/base.py", line 17, in <module>
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

。。。!?!?

Did you install mysqlclient?

mysqlclientはインストール済みニダ。。。。

解決策

mysqlclientがないのではなく、pymysqlをインストールする必要があるよう。

$ pip install pymysql

さらにsettings.pyのある階層のinit.pyに下記のように記述する。

__init__.py
import pymysql

pymysql.install_as_MySQLdb()

もう一度ローカルサーバーを立ち上げる

$ python manage.py runserver
January 12, 2022 - 17:59:32
Django version 3.2, using settings 'tinder_api.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

参考

Error loading MySQLdb Module 'Did you install mysqlclient or MySQL-python?'

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1