无法作为Python模块加载

无法作为Python模块加载

本文介绍了目标WSGI脚本'/opt/python/current/app/.../wsgi.py'无法作为Python模块加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在aws beantalk上部署django应用,但我得到了500.

I am trying to deploy a django app on aws beanstalk but I get a 500.

我收到ImportError,我尝试了网络上提出的所有建议,直到最后.

I get the ImportError and I have tried all what the web has suggested, I think, to no end.

aws服务器日志中的跟踪:

The trace from the aws server log:

-------------------------------------
/var/log/httpd/error_log
-------------------------------------
[Sun Mar 26 02:55:17.126990 2017] [:error] [pid 11381] [remote 127.0.0.1:45055] mod_wsgi (pid=11381): Target WSGI script '/opt/python/current/app/src/kirr/wsgi.py' cannot be loaded as Python module.
[Sun Mar 26 02:55:17.127048 2017] [:error] [pid 11381] [remote 127.0.0.1:45055] mod_wsgi (pid=11381): Exception occurred processing WSGI script '/opt/python/current/app/src/kirr/wsgi.py'.
[Sun Mar 26 02:55:17.127087 2017] [:error] [pid 11381] [remote 127.0.0.1:45055] Traceback (most recent call last):
[Sun Mar 26 02:55:17.127244 2017] [:error] [pid 11381] [remote 127.0.0.1:45055]   File "/opt/python/current/app/src/kirr/wsgi.py", line 7, in <module>
[Sun Mar 26 02:55:17.127252 2017] [:error] [pid 11381] [remote 127.0.0.1:45055]     application = get_wsgi_application()
[Sun Mar 26 02:55:17.127347 2017] [:error] [pid 11381] [remote 127.0.0.1:45055]   File "/opt/python/run/venv/lib/python3.4/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
[Sun Mar 26 02:55:17.127354 2017] [:error] [pid 11381] [remote 127.0.0.1:45055]     django.setup(set_prefix=False)
[Sun Mar 26 02:55:17.127446 2017] [:error] [pid 11381] [remote 127.0.0.1:45055]   File "/opt/python/run/venv/lib/python3.4/site-packages/django/__init__.py", line 22, in setup
[Sun Mar 26 02:55:17.127453 2017] [:error] [pid 11381] [remote 127.0.0.1:45055]     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Sun Mar 26 02:55:17.127584 2017] [:error] [pid 11381] [remote 127.0.0.1:45055]   File "/opt/python/run/venv/lib/python3.4/site-packages/django/conf/__init__.py", line 53, in __getattr__
[Sun Mar 26 02:55:17.127602 2017] [:error] [pid 11381] [remote 127.0.0.1:45055]     self._setup(name)
[Sun Mar 26 02:55:17.127630 2017] [:error] [pid 11381] [remote 127.0.0.1:45055]   File "/opt/python/run/venv/lib/python3.4/site-packages/django/conf/__init__.py", line 41, in _setup
[Sun Mar 26 02:55:17.127635 2017] [:error] [pid 11381] [remote 127.0.0.1:45055]     self._wrapped = Settings(settings_module)
[Sun Mar 26 02:55:17.127655 2017] [:error] [pid 11381] [remote 127.0.0.1:45055]   File "/opt/python/run/venv/lib/python3.4/site-packages/django/conf/__init__.py", line 97, in __init__
[Sun Mar 26 02:55:17.127659 2017] [:error] [pid 11381] [remote 127.0.0.1:45055]     mod = importlib.import_module(self.SETTINGS_MODULE)
[Sun Mar 26 02:55:17.127780 2017] [:error] [pid 11381] [remote 127.0.0.1:45055]   File "/opt/python/run/baselinenv/lib64/python3.4/importlib/__init__.py", line 109, in import_module
[Sun Mar 26 02:55:17.127786 2017] [:error] [pid 11381] [remote 127.0.0.1:45055]     return _bootstrap._gcd_import(name[level:], package, level)
[Sun Mar 26 02:55:17.127802 2017] [:error] [pid 11381] [remote 127.0.0.1:45055]   File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
[Sun Mar 26 02:55:17.127818 2017] [:error] [pid 11381] [remote 127.0.0.1:45055]   File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
[Sun Mar 26 02:55:17.127831 2017] [:error] [pid 11381] [remote 127.0.0.1:45055]   File "<frozen importlib._bootstrap>", line 2212, in _find_and_load_unlocked
[Sun Mar 26 02:55:17.127843 2017] [:error] [pid 11381] [remote 127.0.0.1:45055]   File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
[Sun Mar 26 02:55:17.127854 2017] [:error] [pid 11381] [remote 127.0.0.1:45055]   File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
[Sun Mar 26 02:55:17.127865 2017] [:error] [pid 11381] [remote 127.0.0.1:45055]   File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
[Sun Mar 26 02:55:17.127881 2017] [:error] [pid 11381] [remote 127.0.0.1:45055]   File "<frozen importlib._bootstrap>", line 2224, in _find_and_load_unlocked
[Sun Mar 26 02:55:17.127902 2017] [:error] [pid 11381] [remote 127.0.0.1:45055] ImportError: No module named 'kirr'

wsgi.py 文件:

import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "kirr.settings")
application = get_wsgi_application()

/etc/httpd/conf.d/ wsgi.conf :

/etc/httpd/conf.d/wsgi.conf:

LoadModule wsgi_module modules/mod_wsgi.so
WSGIPythonHome /opt/python/run/baselinenv
WSGISocketPrefix run/wsgi
WSGIRestrictEmbedded On

<VirtualHost *:80>

Alias /static/ /opt/python/current/app/static/
<Directory /opt/python/current/app/static/>
Order allow,deny
Allow from all
</Directory>


WSGIScriptAlias / /opt/python/current/app/src/kirr/wsgi.py


<Directory /opt/python/current/app/>
  Require all granted
</Directory>

WSGIDaemonProcess wsgi processes=1 threads=15 display-name=%{GROUP} \
  python-path=/opt/python/current/app:/opt/python/run/venv/lib64/python3.4/site-packages:/opt/python/run/venv/lib/python3.4/site-packages user=wsgi group=wsgi \
  home=/opt/python/current/app
WSGIProcessGroup wsgi
</VirtualHost>

LogFormat "%h (%{X-Forwarded-For}i) %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

我的django应用程序中有两个配置文件,位于.elasticbeanstalk内:

There are two config files in my django app located inside .elasticbeanstalk:

.elasticbeanstalk/**01_Packages.config**:
`packages:
  yum:
    git: []
    mysql-server: []
    mysql: []
    libjpeg-turbo-devel: []
    zlib-devel: []`

.elasticbeanstalk/**02_Python.config**:
`container_commands:
    01_migrate:
            command: "source /opt/python/run/venv/bin/activate && python src/manage.py migrate --noinput"
        leader_only: true
    02_createsu:
        command: "source /opt/python/run/venv/bin/activate && python src/manage.py createsu"
        leader_only: true
    03_collectstatic:
        command: "source /opt/python/run/venv/bin/activate && python src/manage.py collectstatic --noinput"

option_settings:
    "aws:elasticbeanstalk:application:environment":
        DJANGO_SETTINGS_MODULE: "kirr.settings"
        "PYTHONPATH": "/opt/python/current/app/src:$PYTHONPATH"
        "ALLOWED_HOSTS": ".elasticbeanstalk.com"
    "aws:elasticbeanstalk:container:python":
        WSGIPath: src/kirr/wsgi.py
            NumProcesses: 3
            NumThreads: 20
    "aws:elasticbeanstalk:container:python:staticfiles":
        "/static/": "www/static/"

这是我尝试过的:

ATTEMPT 1:有些建议可能是python版本问题.我使用python 3.4重新构建了django应用程序,与在beantalk上相同,但没有任何效果.

ATTEMPT 1: Some suggested it could be a python version issue. I re-built my django app using python 3.4, same as on beanstalk, but there was no effect.

尝试2:如建议的此处,我更改了wsgi文件的特权.为此,我将ssh放入beantalk(eb ssh)中,cd进入目录,然后:

ATTEMPT 2: As suggested here, I have changed the privileges of the wsgi file. To do this, I ssh into beanstalk (eb ssh), cd to directory and then:

$ sudo chmod a+x wsgi.py

没有效果.

ATTEMPT 3,如建议在这里,我将SOLINUX的值更改为禁用为:

ATTEMPT 3, as suggested here, I have changed the SOLINUX value to disabled as:

(venv)[ec2-user@ip-112-31-10-129 src]$ sudo nano /etc/selinux/semanage.conf

有什么主意吗?

推荐答案

因此,显然我将配置文件保存在错误的文件夹中. .elasticbeanstalk/而不是. ebextensions/.使用后者可以解决问题.

So, apparently I had saved the config files in the wrong folder. .elasticbeanstalk/ instead of . ebextensions/. Using the latter fixed the problem.

如果git不想添加隐藏文件夹,请记住强制添加git,例如:

Remember to force git-add if the git does not want to add the hidden folder, as:

git add -f .ebextensions/

这篇关于目标WSGI脚本'/opt/python/current/app/.../wsgi.py'无法作为Python模块加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:00