[英]Django mod_wsgi: ImportError: No module named 'home'
I have a Django project that runs without issue using the dev server (runserver), but fails when serving through Apache/WSGI. 我有一个使用开发服务器(runserver)可以正常运行的Django项目,但是通过Apache / WSGI提供服务时失败。
The error is ImportError: No module named 'home'
and here's the trace from Apache's error log: 错误是ImportError: No module named 'home'
,这是来自Apache错误日志的跟踪:
[Thu May 03 19:01:11.010623 2018] [mpm_event:notice] [pid 8568:tid 139727981401984] AH00489: Apache/2.4.18 (Ubuntu) mod_wsgi/4.6.4 Python/3.5 configured -- resuming normal operations
[Thu May 03 19:01:11.010742 2018] [mpm_event:info] [pid 8568:tid 139727981401984] AH00490: Server built: 2018-04-18T14:53:04
[Thu May 03 19:01:11.010763 2018] [core:notice] [pid 8568:tid 139727981401984] AH00094: Command line: '/usr/sbin/apache2'
[Thu May 03 19:01:11.011684 2018] [wsgi:info] [pid 8572:tid 139727981401984] mod_wsgi (pid=8572): Python home /home/xxx/is_websites/webenv/.
[Thu May 03 19:01:11.011766 2018] [wsgi:info] [pid 8572:tid 139727981401984] mod_wsgi (pid=8572): Initializing Python.
[Thu May 03 19:01:11.014159 2018] [wsgi:info] [pid 8571:tid 139727981401984] mod_wsgi (pid=8571): Python home /home/xxx/is_websites/webenv/.
[Thu May 03 19:01:11.014238 2018] [wsgi:info] [pid 8571:tid 139727981401984] mod_wsgi (pid=8571): Initializing Python.
[Thu May 03 19:01:11.052639 2018] [wsgi:info] [pid 8572:tid 139727981401984] mod_wsgi (pid=8572): Attach interpreter ''.
[Thu May 03 19:01:11.054513 2018] [wsgi:info] [pid 8571:tid 139727981401984] mod_wsgi (pid=8571): Attach interpreter ''.
[Thu May 03 19:01:11.073009 2018] [wsgi:info] [pid 8571:tid 139727981401984] mod_wsgi (pid=8571): Adding '/home/xxx/is_websites/' to path.
[Thu May 03 19:01:11.075298 2018] [wsgi:info] [pid 8572:tid 139727981401984] mod_wsgi (pid=8572): Adding '/home/xxx/is_websites/' to path.
[Thu May 03 19:01:23.330027 2018] [wsgi:info] [pid 8572:tid 139727768385280] mod_wsgi (pid=8572): Create interpreter '127.0.1.1|'.
[Thu May 03 19:01:23.346866 2018] [wsgi:info] [pid 8572:tid 139727768385280] mod_wsgi (pid=8572): Adding '/home/xxx/is_websites/' to path.
[Thu May 03 19:01:23.347805 2018] [wsgi:info] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx] mod_wsgi (pid=8572, process='', application='127.0.1.1|'): Loading Python script file '/home/xxx/is_websi$
[Thu May 03 19:01:23.665257 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx] mod_wsgi (pid=8572): Failed to exec Python script file '/home/xxx/is_websites/is_spa/is_spa/wsgi.py'.
[Thu May 03 19:01:23.665326 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx] mod_wsgi (pid=8572): Exception occurred processing WSGI script '/home/xxx/is_websites/is_spa/is_spa/wsgi$
[Thu May 03 19:01:23.667315 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx] Traceback (most recent call last):
[Thu May 03 19:01:23.667375 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx] File "/home/xxx/is_websites/is_spa/is_spa/wsgi.py", line 16, in <module>
[Thu May 03 19:01:23.667386 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx] application = get_wsgi_application()
[Thu May 03 19:01:23.667399 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx] File "/home/xxx/is_websites/webenv/lib/python3.5/site-packages/django/core/wsgi.py", line 12, in get_w$
[Thu May 03 19:01:23.667405 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx] django.setup(set_prefix=False)
[Thu May 03 19:01:23.667416 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx] File "/home/xxx/is_websites/webenv/lib/python3.5/site-packages/django/__init__.py", line 24, in setup
[Thu May 03 19:01:23.667422 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx] apps.populate(settings.INSTALLED_APPS)
[Thu May 03 19:01:23.667432 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx] File "/home/xxx/is_websites/webenv/lib/python3.5/site-packages/django/apps/registry.py", line 89, in p$
[Thu May 03 19:01:23.667439 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx] app_config = AppConfig.create(entry)
[Thu May 03 19:01:23.667449 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx] File "/home/xxx/is_websites/webenv/lib/python3.5/site-packages/django/apps/config.py", line 90, in cre$
[Thu May 03 19:01:23.667455 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx] module = import_module(entry)
[Thu May 03 19:01:23.667465 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx] File "/home/xxx/is_websites/webenv/lib/python3.5/importlib/__init__.py", line 126, in import_module
[Thu May 03 19:01:23.667471 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx] return _bootstrap._gcd_import(name[level:], package, level)
[Thu May 03 19:01:23.667481 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx] File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Thu May 03 19:01:23.667493 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx] File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Thu May 03 19:01:23.667504 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx] File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
[Thu May 03 19:01:23.667539 2018] [wsgi:error] [pid 8572:tid 139727768385280] [client xxx.xxx.xxx.xxx] ImportError: No module named 'home'
It seems like the error is in settings.INSTALLED_APPS
, which looks for a home
app. 似乎错误在于settings.INSTALLED_APPS
,它正在寻找home
应用。
I can't find any issue with the Django files. 我找不到Django文件的任何问题。 I have a home
app with an __init__.py
file, and everything works on the dev server. 我有一个带有__init__.py
文件的home
应用程序,并且一切都在开发服务器上运行。
This makes me think it might be a mod_wsgi
issue, so I reinstalled mod_wsgi to make sure I installed against the correct python version from my virtualenv, and I changed group ownership recursively to www-data
to make sure I don't have permission issues. 这使我认为这可能是mod_wsgi
问题,因此我重新安装了mod_wsgi以确保从virtualenv中针对正确的python版本进行了安装,然后将组所有权递归更改为www-data
以确保没有权限问题。
I'm sure I'm missing something simple... but I don't see it! 我确定我缺少一些简单的东西...但是我看不到!
Here's the apache virtual host setup: 这是apache虚拟主机设置:
LogLevel info
WSGIScriptAlias / /home/xxx/is_websites/is_spa/is_spa/wsgi.py
WSGIDaemonProcess xxx_website processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup xxx_website
<Directory /home/xxx/is_websites/is_spa/is_spa/>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Here is apache mod wsgi.load
: 这是apache mod wsgi.load
:
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
Here is apache mod wsgi.conf
: 这是apache mod wsgi.conf
:
WSGIPythonHome /home/xxx/is_websites/webenv/
WSGIPythonPath /home/xxx/is_websites/is_spa/
As requested, here's what I get when manually importing home
: 根据要求,这是手动导入home
时得到的:
(webenv) xxx@django-wagtail-postgres:~/is_websites/is_spa$ python
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import home
>>> home.__file__
'/home/xxx/is_websites/is_spa/home/__init__.py'
>>>
And here's the output from wsgi make install
: 这是wsgi make install
的输出:
(webenv) root@django-wagtail-postgres:/home/xxx/mod_wsgi-4.6.4# make install
/usr/bin/apxs2 -i -S LIBEXECDIR=/usr/lib/apache2/modules -n 'mod_wsgi' src/server/mod_wsgi.la
/usr/share/apache2/build/instdso.sh SH_LIBTOOL='/usr/share/apr-1.0/build/libtool' src/server/mod_wsgi.la /usr/lib/apache2/modules
/usr/share/apr-1.0/build/libtool --mode=install install src/server/mod_wsgi.la /usr/lib/apache2/modules/
libtool: install: install src/server/.libs/mod_wsgi.so /usr/lib/apache2/modules/mod_wsgi.so
libtool: install: install src/server/.libs/mod_wsgi.lai /usr/lib/apache2/modules/mod_wsgi.la
libtool: finish: PATH="/home/xxx/is_websites/webenv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/sbin" ldconfig -n /usr/lib/apache2/modules
----------------------------------------------------------------------
Libraries have been installed in:
/usr/lib/apache2/modules
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the '-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the 'LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the 'LD_RUN_PATH' environment variable
during linking
- use the '-Wl,-rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to '/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
chmod 644 /usr/lib/apache2/modules/mod_wsgi.so
(webenv) root@django-wagtail-postgres:/home/xxx/mod_wsgi-4.6.4#
I added Python-path to the Damon process as instructed (as well as python-home): WSGIDaemonProcess website processes=2 threads=15 display-name=%{GROUP} python-home=/home/xxx/is_websites/webenv/ python-path=/home/xxx/is_websites/is_spa
我按照指示(以及python-home)将Python路径添加到了Damon进程中: WSGIDaemonProcess website processes=2 threads=15 display-name=%{GROUP} python-home=/home/xxx/is_websites/webenv/ python-path=/home/xxx/is_websites/is_spa
But I still get an error. 但是我仍然得到一个错误。 This time ImportError: No module named 'is_spa.is_spa'
这次ImportError: No module named 'is_spa.is_spa'
[Mon May 07 14:39:15.769405 2018] [wsgi:info] [pid 3693:tid 140405951944576] mod_wsgi (pid=3693): Cleanup interpreter ''.
[Mon May 07 14:39:15.771668 2018] [wsgi:info] [pid 3692:tid 140405951944576] mod_wsgi (pid=3692): Cleanup interpreter ''.
[Mon May 07 14:39:15.785445 2018] [wsgi:info] [pid 3693:tid 140405951944576] mod_wsgi (pid=3693): Terminating Python.
[Mon May 07 14:39:15.786996 2018] [wsgi:info] [pid 3692:tid 140405951944576] mod_wsgi (pid=3692): Terminating Python.
[Mon May 07 14:39:15.801152 2018] [wsgi:info] [pid 3693:tid 140405951944576] mod_wsgi (pid=3693): Python has shutdown.
[Mon May 07 14:39:15.801201 2018] [wsgi:info] [pid 3693:tid 140405951944576] mod_wsgi (pid=3693): Exiting process 'website'.
[Mon May 07 14:39:15.802813 2018] [wsgi:info] [pid 3692:tid 140405951944576] mod_wsgi (pid=3692): Python has shutdown.
[Mon May 07 14:39:15.802837 2018] [wsgi:info] [pid 3692:tid 140405951944576] mod_wsgi (pid=3692): Exiting process 'website'.
[Mon May 07 14:39:16.736463 2018] [mpm_event:notice] [pid 3926:tid 140137508214656] AH00489: Apache/2.4.18 (Ubuntu) mod_wsgi/4.6.4 Python/3.5 configured -- resuming normal operations
[Mon May 07 14:39:16.736597 2018] [core:notice] [pid 3926:tid 140137508214656] AH00094: Command line: '/usr/sbin/apache2'
[Mon May 07 14:39:16.827772 2018] [wsgi:info] [pid 3929:tid 140137508214656] mod_wsgi (pid=3929): Attach interpreter ''.
[Mon May 07 14:39:16.832887 2018] [wsgi:info] [pid 3928:tid 140137508214656] mod_wsgi (pid=3928): Attach interpreter ''.
[Mon May 07 14:39:16.871324 2018] [wsgi:info] [pid 3929:tid 140137508214656] mod_wsgi (pid=3929): Adding '/home/xxx/is_websites/is_spa' to path.
[Mon May 07 14:39:16.876253 2018] [wsgi:info] [pid 3928:tid 140137508214656] mod_wsgi (pid=3928): Adding '/home/xxx/is_websites/is_spa' to path.
[Mon May 07 14:39:19.891511 2018] [wsgi:info] [pid 3929:tid 140137402570496] mod_wsgi (pid=3929): Create interpreter '127.0.1.1|'.
[Mon May 07 14:39:19.908227 2018] [wsgi:info] [pid 3929:tid 140137402570496] mod_wsgi (pid=3929): Adding '/home/xxx/is_websites/is_spa' to path.
[Mon May 07 14:39:19.909016 2018] [wsgi:info] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] mod_wsgi (pid=3929, process='website', application='127.0.1.1|'): Loading Python script file '/home/xxx/is_websites/is_spa/is_spa/wsgi.py'.
[Mon May 07 14:39:20.262585 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] mod_wsgi (pid=3929): Failed to exec Python script file '/home/xxx/is_websites/is_spa/is_spa/wsgi.py'.
[Mon May 07 14:39:20.262676 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] mod_wsgi (pid=3929): Exception occurred processing WSGI script '/home/xxx/is_websites/is_spa/is_spa/wsgi.py'.
[Mon May 07 14:39:20.263610 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] Traceback (most recent call last):
[Mon May 07 14:39:20.263692 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] File "/home/xxx/is_websites/is_spa/is_spa/wsgi.py", line 16, in <module>
[Mon May 07 14:39:20.263702 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] application = get_wsgi_application()
[Mon May 07 14:39:20.263715 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] File "/home/xxx/is_websites/webenv/lib/python3.5/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Mon May 07 14:39:20.263721 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] django.setup(set_prefix=False)
[Mon May 07 14:39:20.263732 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] File "/home/xxx/is_websites/webenv/lib/python3.5/site-packages/django/__init__.py", line 19, in setup
[Mon May 07 14:39:20.263738 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Mon May 07 14:39:20.263749 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] File "/home/xxx/is_websites/webenv/lib/python3.5/site-packages/django/conf/__init__.py", line 56, in __getattr__
[Mon May 07 14:39:20.263755 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] self._setup(name)
[Mon May 07 14:39:20.263765 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] File "/home/xxx/is_websites/webenv/lib/python3.5/site-packages/django/conf/__init__.py", line 43, in _setup
[Mon May 07 14:39:20.263783 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] self._wrapped = Settings(settings_module)
[Mon May 07 14:39:20.263795 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] File "/home/xxx/is_websites/webenv/lib/python3.5/site-packages/django/conf/__init__.py", line 106, in __init__
[Mon May 07 14:39:20.263801 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] mod = importlib.import_module(self.SETTINGS_MODULE)
[Mon May 07 14:39:20.263811 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] File "/home/xxx/is_websites/webenv/lib/python3.5/importlib/__init__.py", line 126, in import_module
[Mon May 07 14:39:20.263818 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] return _bootstrap._gcd_import(name[level:], package, level)
[Mon May 07 14:39:20.263828 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Mon May 07 14:39:20.263840 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Mon May 07 14:39:20.263851 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
[Mon May 07 14:39:20.263863 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
[Mon May 07 14:39:20.263875 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Mon May 07 14:39:20.263886 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Mon May 07 14:39:20.263898 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
[Mon May 07 14:39:20.263909 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
[Mon May 07 14:39:20.263921 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Mon May 07 14:39:20.263932 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Mon May 07 14:39:20.263944 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
[Mon May 07 14:39:20.263970 2018] [wsgi:error] [pid 3929:tid 140137402570496] [remote xxx.xxx.xxx.xxx] ImportError: No module named 'is_spa.is_spa'
But the is_spa
module is there: 但是is_spa
模块在那里:
(webenv) xxx@django-wagtail-postgres:~/is_websites/is_spa$ python
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import is_spa
>>> is_spa.__file__
'/home/wag/is_websites/is_spa/is_spa/__init__.py'
>>>
Is there a particular location that the script must be located in a Django project? 脚本必须位于Django项目中的特定位置吗? My folder structure is /is_spa/is_spa/
, where the first directory is the project, and the second is the app. 我的文件夹结构是/is_spa/is_spa/
,其中第一个目录是项目,第二个目录是应用程序。 I currently have the WSGI script in the app directory, but I can't imagine that would matter when we explicitly set the python-path
option. 我目前在应用程序目录中有WSGI脚本,但是当我们显式设置python-path
选项时,我无法想象这会很重要。
The root cause is that the WSGIDamonProcess needs the python-path
to be set explicitly in it's options. 根本原因是WSGIDamonProcess需要在其选项中显式设置python-path
。 It does not inherit the global value. 它不继承全局值。
As @GrahamDumpleton pointed out, changing 正如@GrahamDumpleton指出的那样,
WSGIDaemonProcess website processes=2 threads=15 display-name=%{GROUP}
to 至
WSGIDaemonProcess website processes=2 threads=15 display-name=%{GROUP} python-path=/home/xxx/is_websites/is_spa
solved the issue. 解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.