[英]ImportError - deploy Falcon app to AWS Elastic Beanstalk
I'm having an ImportError after deploying to Elastic Beanstalk. 我在部署到Elastic Beanstalk后遇到了ImportError。 We're running Falcon to make an API. 我们正在运行Falcon来制作API。 I have successfully used the EB CLI to initialise an app, create the environment and deploy my code. 我已成功使用EB CLI初始化应用程序,创建环境并部署我的代码。 We use Python 2.7 (for now). 我们使用Python 2.7(现在)。
The AWS EB site for the app/environment says everything went well, but when I send a request to the api, the logs show this error: 应用程序/环境的AWS EB站点说一切顺利,但是当我向api发送请求时,日志会显示以下错误:
[Wed Nov 30 06:02:25.555507 2016] [:error] [pid 29352] [remote 127.0.0.1:56624] Traceback (most recent call last):
[Wed Nov 30 06:02:25.555524 2016] [:error] [pid 29352] [remote 127.0.0.1:56624] File "/opt/python/current/app/api/app.py", line 12, in <module>
[Wed Nov 30 06:02:25.555549 2016] [:error] [pid 29352] [remote 127.0.0.1:56624] from middlewares import require_json
[Wed Nov 30 06:02:25.555566 2016] [:error] [pid 29352] [remote 127.0.0.1:56624] ImportError: No module named middlewares
[Wed Nov 30 06:02:26.558411 2016] [:error] [pid 29352] [remote 127.0.0.1:60720] mod_wsgi (pid=29352): Target WSGI script '/opt/python/current/app/api/app.py' cannot be loaded as Python module.
Locally my imports work just fine. 本地我的进口工作得很好。 The relevant parts of the api look like this: api的相关部分如下所示:
-api/
-__init__.py #import app
-app.py
-middlewares/
-__init__.py #import require_json
-require_json.py
Line 12 of my app.py file: 我的app.py文件的第12行:
from middlewares import require_json
But the logs are consistently saying this is an ImportError. 但是日志一直在说这是一个ImportError。 Any thoughts on what's happened here? 关于这里发生了什么的任何想法? I'm baffled. 我很困惑。
As for the __init__.py
files importing modules; 至于__init__.py
文件导入模块; this is a legacy code base and I'm not sure what the implications are for changing that just yet, other than the tests will fail. 这是一个遗留代码库,我不确定改变它的含义是什么,除了测试失败。
EDIT 编辑
This morning I've tried commenting out those import lines, but that just gives me errors on the next import: 今天早上我尝试评论出那些导入行,但这只会让我在下次导入时出错:
[Wed Nov 30 21:52:00.699228 2016] [:error] [pid 11254] [remote 172.31.8.163:37352] Traceback (most recent call last):
[Wed Nov 30 21:52:00.699245 2016] [:error] [pid 11254] [remote 172.31.8.163:37352] File "/opt/python/current/app/api/app.py", line 15, in <module>
[Wed Nov 30 21:52:00.699270 2016] [:error] [pid 11254] [remote 172.31.8.163:37352] import resources
[Wed Nov 30 21:52:00.699288 2016] [:error] [pid 11254] [remote 172.31.8.163:37352] ImportError: No module named resources
I've also done what's recommended in this article . 我也完成了本文的建议。
import sys
sys.path.insert(0, '/opt/python/current/app')
With the way your code appears to be structured, you shouldn't be pointing WSGIScriptAlias
directive of Apache at your app.py
file directly. 使用您的代码的结构化方式,您不应该直接在app.py
文件中指向Apache的WSGIScriptAlias
指令。
What you need to do is create a separate WSGI script file to use with WSGIScriptAlias
and in it have: 您需要做的是创建一个单独的WSGI脚本文件以与WSGIScriptAlias
一起使用,其中包含:
from api.app import application
You will also need to configure mod_wsgi so that the parent directory of the api
directory is in the Python module search path. 您还需要配置mod_wsgi,以便api
目录的父目录位于Python模块搜索路径中。 You can do in the mod_wsgi configuration, or do it in this new WSGI script file. 您可以在mod_wsgi配置中执行,也可以在此新的WSGI脚本文件中执行此操作。
Presuming you had added the WSGI script file as app.wsgi
in the same directory as api
appears as a subdirectory, it could include: 假设您已将WSGI脚本文件作为app.wsgi
添加到与api
显示为子目录的同一目录中,它可能包括:
import os
import sys
sys.path.insert(0, os.path.dirname(__file__))
from api.app import application
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.