BLOGサブスレッドの日常
2020.12.17
pyenv-virtualenv 上で動かしていたDjangoが突然動かなくなった?
kura
お久しぶりです。くらはしです。
朝起きたら、気温が3℃と出ていたので、朝のエアコンタイマー運用をはじめました。
それは突然起こりました
開発業務からは離れていますが、今回、Django の admin サイトをいじってみようと思いまして、ローカル環境を作って動かしていました。(環境構築手順は割愛)
よしよし動くぞと思っていたら、突然以下のエラーが発生するようになってしまいました(泣)
[kurahashimac@kura-MacBook-Pro🍺 trial] % pyenv local djangotest
(djangotest) [kurahashimac@kura-MacBook-Pro🍺 trial] % python src/manage.py runserver
Traceback (most recent call last):
File "src/manage.py", line 11, in main
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "src/manage.py", line 22, in <module>
main()
File "src/manage.py", line 17, in main
) from exc
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
確認したこと
まず、PythonやDjangoがちゃんとインストールされているか?参照先が正しいのか?の確認をしました。
(djangotest) [kurahashimac@kura-MacBook-Pro🍺 trial] % pyenv versions
system
3.6.5
3.6.5/envs/djangotest
* djangotest (set by /Users/kurahashimac/temp/trial/.python-version)
(djangotest) [kurahashimac@kura-MacBook-Pro🍺 trial] %
(↓pipのバージョンが古くて怒られていますが後で更新しました)
(djangotest) [kurahashimac@kura-MacBook-Pro🍺 trial] % pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
asgiref (3.3.1)
defusedxml (0.6.0)
diff-match-patch (20200713)
Django (3.1.3)
django-import-export (2.4.0)
et-xmlfile (1.0.1)
jdcal (1.4.1)
MarkupPy (1.14)
odfpy (1.4.1)
openpyxl (3.0.5)
pip (9.0.3)
pytz (2020.4)
PyYAML (5.3.1)
setuptools (39.0.1)
sqlparse (0.4.1)
tablib (2.0.0)
xlrd (1.2.0)
xlwt (1.3.0)
You are using pip version 9.0.3, however version 20.2.4 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(djangotest) [kurahashimac@kura-MacBook-Pro🍺 trial] % which python
/Users/kurahashimac/.anyenv/envs/pyenv/shims/python
(djangotest) [kurahashimac@kura-MacBook-Pro🍺 trial] % python
Python 3.6.5 (default, Nov 13 2020, 18:29:56)
[GCC Apple LLVM 12.0.0 (clang-1200.0.32.27)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/Users/kurahashimac/.anyenv/envs/pyenv/versions/3.6.5/lib/python36.zip', '/Users/kurahashimac/.anyenv/envs/pyenv/versions/3.6.5/lib/python3.6', '/Users/kurahashimac/.anyenv/envs/pyenv/versions/3.6.5/lib/python3.6/lib-dynload', '/Users/kurahashimac/.anyenv/envs/pyenv/versions/djangotest/lib/python3.6/site-packages']
他の人に確認しても間違ってなさそうとのこと∩(・ω・)∩
さて困った
お手上げ状態だったので、開発メンバーにプロジェクト一式を見てもらったところ天の声が!
src/.python-version
たぶんこいつのせい!!
おおお?
エラーする環境
rwxr-xr-x@ 8 kurahashimac staff 256 11 13 17:54 .
drwx------@ 10 kurahashimac staff 320 12 17 16:48 ..
-rw-r--r--@ 1 kurahashimac staff 6 11 13 17:54 .python-version
-rw-r--r--@ 1 kurahashimac staff 167936 11 13 17:48 db.sqlite3
-rwxr-xr-x@ 1 kurahashimac staff 663 11 13 18:20 manage.py
drwxr-xr-x@ 11 kurahashimac staff 352 11 13 16:17 manager
drwxr-xr-x@ 8 kurahashimac staff 256 11 13 16:06 project
動作する環境
drwxr-xr-x 7 kurahashimac staff 224 11 13 19:50 .
drwxr-xr-x 5 kurahashimac staff 160 11 13 18:41 ..
-rw-r--r-- 1 kurahashimac staff 167936 11 13 19:50 db.sqlite3
-rwxr-xr-x 1 kurahashimac staff 663 11 13 18:20 manage.py
drwxr-xr-x 11 kurahashimac staff 352 11 13 16:17 manager
drwxr-xr-x 8 kurahashimac staff 256 11 13 16:06 project
確かに余分な .python-version
がいました。
無事解決!
天の声を元に、src/.python-version
を削除したら動くようになりました!!!
(src/
の親ディレクトリにも .python-version
がいました)
src/
の親ディレクトリで pyenv local djangotest
としていて、そこで python src/manage.py
してるのに、異なった .python-version
が参照されてしまったのかもしれません。
なぜそんな状態になっていたのかは分かりませんが、突然動かなくなった(泣)となった際には、疑ってみてください。
この記事を書いた人
kura