Djangoのマイグレーションでエラーが発生するときの対処法

DjangoでMySQLを使ってマイグレーション時にエラーが発生し、結構はまったので、備忘録として残しておきます。

【環境】

・Windows10(VMWare Workstation15 player)
・Ubuntu20.04
・Python 3.8.2

【エラー対処方法】

「Did you install mysqlclient?」エラーが出た場合の対処方法

以下のようなエラーが発生した時

test@ubuntu:~/デスクトップ/python/myapp$ python manage.py makemigrations helloworld
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/django/db/backends/mysql/base.py", line 16, 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 21, in <module>
    main()
・
・
・
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?
test@ubuntu:~/デスクトップ/python/myapp$

「pip install mysqlclient」をインストールする。

もし、「pip install mysqlclient」で次のようなエラーが出たら「sudo apt install libpython3.8-dev」を実行する

test@ubuntu:~/デスクトップ/python/myapp$ pip install mysqlclient
Defaulting to user installation because normal site-packages is not writeable
Collecting mysqlclient
  Using cached mysqlclient-2.0.1.tar.gz (87 kB)
Using legacy setup.py install for mysqlclient, since package 'wheel' is not installed.
Installing collected packages: mysqlclient
    Running setup.py install for mysqlclient ... error
    ERROR: Command errored out with exit status 1:
     command: /usr/bin/python3.8 -u -c 'import sys, setuptools, tokenize; sys.argv[0]
・
・ 
(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-yff8str7/install-record.txt --single-version-externally-managed --user --prefix= --compile --install-headers /home/test/.local/include/python3.8/mysqlclient Check the logs for full command output.
test@ubuntu:~/デスクトップ/python/myapp$ 
sudo apt install libpython3.8-dev

この後にもう一度「pip install mysqlclient」を実行する。

問題なければこれでマイグレーションが実行できます。

以上です。ありがとうございました。

コメント

タイトルとURLをコピーしました