[英]Apache + WSGI to run Flask, get Python ImportError: “cannot import name …” or “No module named …”
Hello dear SO users and welcome to my first ever SO question, 尊敬的SO用户您好,欢迎来到我的第一个SO问题,
I am currently looking to deploy a Flask application using Apache and mod_wsgi
. 我目前正在寻找使用Apache和
mod_wsgi
部署Flask应用程序的方法。 Everything was coded with Python 3.4 so I had to follow several tutorials / questions to get mod_wsgi
working with my version of Python (because apt-get install gets you the 3.4 version which is Python 2.7 compatible, you have to compile it after getting it with pip in a Python 3 virtualenv for it to be the 4.x version). 一切都使用Python 3.4进行了编码,因此我必须遵循几个教程 / 问题才能使
mod_wsgi
使用我的Python版本(因为apt-get install获得了与Python 2.7兼容的3.4版本,因此必须在将其与之兼容后进行编译点入Python 3 virtualenv,使其成为4.x版本)。
The Apache server error.log shows Apache/2.4.10 (Debian) mod_wsgi/4.4.13 Python/3.4.2 configured
so this seems to be working fine. Apache服务器error.log显示了配置的
Apache/2.4.10 (Debian) mod_wsgi/4.4.13 Python/3.4.2 configured
因此看来工作正常。
My problem lies in the importation of the application in the .wsgi file . 我的问题在于.wsgi文件中的应用程序导入。 Tutorials such as this one usually do
from FlaskApp import app as application
considering their file tree, and this works as intended with a minimal application, I tried it without any problem. 诸如此类的教程通常是
from FlaskApp import app as application
考虑它们的文件树来完成的,并且这在最小的应用程序中可以正常工作,我尝试了它没有任何问题。
My application is a little more complicated and uses a manage.py file to run on localhost, which may be part of the problem once trying to deploy on Apache. 我的应用程序稍微复杂一点,并使用manage.py文件在localhost上运行,一旦尝试在Apache上进行部署,这可能是问题的一部分。
From what I have found on countless SO Questions and other sites' questions, this most likely comes from: 从我在无数SO问题和其他站点的问题中发现的信息来看,这很可能来自:
sys.path
problem (if the top-level folder is not in the sys.path
, Python 3 cannot see it) sys.path
问题(如果顶级文件夹不在sys.path
,则Python 3无法看到它) Mainly out of frustration, I ended up using chmod 744 LongAppName
in /var/www/ resulting in drwxrwxr-x rights. 主要是出于沮丧,我最终在/ var / www /中使用
chmod 744 LongAppName
产生了drwxrwxr-x权限。 My file tree is the following: 我的文件树如下:
drwxrwxr-x LongAppName
drwxr-xr-x LongAppName
drwxr--r-- ShorterAppName
-rw-r--r-- app.py
-rw-r--r-- commands.py
-rw-r--r-- __init__.py
drwxr--r-- static
drwxr--r-- templates
<irrelevant files>
-rw-r--r-- __init__.py
-rw-r--r-- manage.py
drwxr-xr-x venv
<more irrelevent files>
-rwxr-xr-x longappname.wsgi
So for the first possible fix, permissions seem fine to me. 因此,对于第一个可能的修复,权限对我来说似乎不错。 Regarding the second one, I'm printing
sys.path
in the .wsgi file and it outpouts ['/var/www/LongAppName', <among others>]
which shows that the top-level folder is in the sys.path
. 关于第二个,我打印
sys.path
在.wsgi文件,并将其outpouts ['/var/www/LongAppName', <among others>]
这表明顶级文件夹是在sys.path
。
My longappname.wsgi is as follows: 我的longappname.wsgi如下:
#!/var/www/LongAppName/LongAppName/venv/bin/python3
import os, sys, logging
#Doing some prints to check if we're in the venv and such.
PROJECT_DIR = '/var/www/LongAppName/'
if PROJECT_DIR not in sys.path:
sys.path.insert(0, PROJECT_DIR)
def execfile(filename):
globals = dict( __file__ = filename )
exec( open(filename).read(), globals )
activate_this = os.path.join( PROJECT_DIR+'LongAppName/', 'venv/bin', 'activate_this.py' )
execfile( ativate_this )
#Printing sys.path and sys.executable here
logging.basicConfig(stream=sys.stderr)
from LongAppName.ShorterAppName.app import manager as application
#The previous line changed a lot, I tried importing app, changing the path but this one was the first one I wrote.
Python can't seem to find LongAppName.ShorterAppName.app and I don't know why. Python似乎找不到LongAppName.ShorterAppName.app,我也不知道为什么。
From the number of attempts I have made to test different alternatives to the code I wrote in the first place, I'm starting to lose track of what was good and what might cause the problem so I require your help to figure this out. 从我尝试测试我最初编写的代码的不同替代方案的尝试中,我开始失去了对什么是好的以及可能导致问题的原因的跟踪,因此我需要您的帮助来解决这一问题。
Thanks in advance! 提前致谢!
Some of your directory permissions are now suspect. 现在怀疑您的某些目录权限。 You have:
你有:
drwxr--r-- ShorterAppName
Better to be using: 最好使用:
drwxr-xr-x ShorterAppName
Same for static and templates. 静态和模板相同。
What we really need to see is the exact Python exception details with traceback from the Apache error logs. 我们真正需要查看的是确切的Python异常详细信息,并具有从Apache错误日志进行追溯的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.