2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ModuleNotFoundError: No module named 'MySQLdb'

Posted at

事象 : DjangoでMySQLに接続確認しようとしたら怒られた

  1. Cloud9でPython3の環境を作成
    • Djangoがデフォルトでインストールされていた!素敵!
  2. Cloud9からRDSのMySQLを接続できるように設定
    • mysqlコマンドがデフォルトでインストールされていた!素敵!
  3. setting.pyにMySQLへの設定をして接続確認!エラー!
$ python manage.py migrate
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 15, in <module>
    import MySQLdb as Database
ModuleNotFoundError: No module named 'MySQLdb'

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

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 347, in execute
    django.setup()
# ...省略...
  File "/usr/local/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?

原因 : MySQLに接続するドライバがインストールされていないから

Cloud9にmysqlコマンドやDjangoがデフォルトでインストールされているからってPythonにMySQL用ドライバがデフォルトでインストールされているわけじゃない。
MySQLのドライバも「mysqlclient」以外にも「PyMySQL」とか「mysql-connector」とかいろいろあるようですし・・・。

# MySQL用ドライバが何にもない・・・
$ pip list | grep -i mysql

対応 : PyMySQLをインストールする

参考 : Python3系でDjangoでMySQLに接続する

  1. PyMySQLをインストールする
  2. manage.pyでPyMySQLを利用できるよう設定する
  3. settings.pyにOPTIONSを追記する
    • 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"を設定しないと「(mysql.W002) MySQL Strict Mode is not set for database connection 'default'」が出ます
    • 参考 : Databases | Django documentation | Django
  4. python manage.py migrateで接続確認する
# 1. PyMySQLをインストールする
$ pip install PyMySQL
Defaulting to user installation because normal site-packages is not writeable
Collecting PyMySQL
  Downloading PyMySQL-1.0.2-py3-none-any.whl (43 kB)
     |████████████████████████████████| 43 kB 2.3 MB/s 
Installing collected packages: PyMySQL
Successfully installed PyMySQL-1.0.2
manage.py
#!/usr/bin/env python
import os
import sys
# 2. PyMySQLを利用できるように以下を追記する
import pymysql

pymysql.install_as_MySQLdb()
# ...省略...
setting.py
# 3. settings.pyにOPTIONSを追記する
# ...省略...
DATABASES = {
    'default': {
# ...省略...
        'PORT': '3306',
        'OPTIONS': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
        },
# ...省略...
# 4. 接続確認する
$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  No migrations to apply.
2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?