__init__.pyを消すとrunserverで動くのにuwsgiでは動作しないなんて・・・

Django

はじめに

Djangoでプログラムを作成してuwsgiで動作させようとしたときにエラーが表示されました。
Django付属のrunserverやApache2(mod_wsgi)で試すと動作するのでエラー内容から原因を調査しました。

エラー内容

最後の方に「myapp」のエラーが表示されていますが、「django.core.exceptions.ImproperlyConfigured」などで検索してもよく分かりませんでした。

hoge > cd /home/www/wsgi/proj
hoge > /home/hoge/.pyenv/shims/uwsgi --http :8900  --module myproj.wsgi

*** Starting uWSGI 2.0.20 (64bit) on [Tue Nov  2 11:19:49 2021] ***
compiled with version: 8.3.0 on 01 November 2021 07:31:29
os: Linux-4.19.0-17-amd64 #1 SMP Debian 4.19.194-3 (2021-07-18)
nodename: xxxx
machine: x86_64
clock source: unix
detected number of CPU cores: 8
current working directory: /home/www/wsgi/proj
detected binary path: /home/hoge/.pyenv/versions/3.9.6/envs/proj-396/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 30912
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :8900 fd 4
spawned uWSGI http 1 (pid: 13984)
uwsgi socket 0 bound to TCP address 127.0.0.1:40887 (port auto-assigned) fd 3
Python version: 3.9.7 (default, Nov  1 2021, 15:28:03)  [GCC 8.3.0]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x557f9fd95960
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72920 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
Traceback (most recent call last):
  File "/home/www/wsgi/proj/./proj/wsgi.py", line 16, in <module>
    application = get_wsgi_application()
  File "/home/hoge/.pyenv/versions/3.9.6/envs/proj-396/lib/python3.9/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
    django.setup(set_prefix=False)
  File "/home/hoge/.pyenv/versions/3.9.6/envs/proj-396/lib/python3.9/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/hoge/.pyenv/versions/3.9.6/envs/proj-396/lib/python3.9/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "/home/hoge/.pyenv/versions/3.9.6/envs/proj-396/lib/python3.9/site-packages/django/apps/config.py", line 255, in create
    return app_config_class(app_name, app_module)
  File "/home/hoge/.pyenv/versions/3.9.6/envs/proj-396/lib/python3.9/site-packages/django/apps/config.py", line 49, in __init__
    self.path = self._path_from_module(app_module)
  File "/home/hoge/.pyenv/versions/3.9.6/envs/proj-396/lib/python3.9/site-packages/django/apps/config.py", line 88, in _path_from_module
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: The app module <module 'myapp' (namespace)> has multiple filesystem locations (['/home/www/wsgi/proj/myapp', '/home/www/wsgi/proj/./myapp']); you must configure this app with an AppConfig subclass with a 'path' class attribute.
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 13941, cores: 1)

原因

現在のファイル群とGitLabに保存したファイルとの差分をとって眺めているとmyappに「__init__.py」が存在しないことが分かりました。
何かの拍子にファイルを削除してしまったようです。

解決方法

cd /home/www/wsgi/proj/myapp
touch __init__.py

さいごに

Samba経由でファイルを修正しているのですが、何かの拍子に消したようで余計な手間がかかりました。
「__init__.py」ファイルは空ファイルなのに、モジュール検索のためのマーカーや存在するディレクトリ名を名前とする名前空間の初期化を行うなど重要な役割を果たしているようです。
エラー内容からはなかなか原因が特定できないケースだったので勉強になりました。

Comments