简体   繁体   English

Apache + WSGI运行Flask,获取Python ImportError:“无法导入名称…”或“未命名模块…”

[英]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问题和其他站点的问题中发现的信息来看,这很可能来自:

  • A permission problem (if the user executing the current script doesn't have reading right on the script he's looking to import) 权限问题(如果执行当前脚本的用户对他要导入的脚本没有正确的阅读权限)
  • A 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.

相关问题 Apache2 和 Django - [wsgi:error] ImportError: cannot import name 'get_version' -&gt; No module named 'django' - Apache2 and Django - [wsgi:error] ImportError: cannot import name 'get_version' -> No module named 'django' ImportError:没有名为bottle的模块— WSGI + python + apache - ImportError: No module named bottle — WSGI + python + apache Flask和wsgi,ImportError:无法导入名称应用 - Flask and wsgi, ImportError: cannot import name app 导入错误:在 VM 上通过 apache2 和 wsgi 提供flask 应用程序时,无法从“flask”导入名称“request” - ImportError: cannot import name 'request' from 'flask' when serving flask app via apache2 and wsgi on VM Django + mod_wsgi + apache:ImportError:未命名模块<module name> - Django + mod_wsgi + apache: ImportError: No module named <module name> 无法将目标WSGI脚本作为Python模块+ ImportError加载:未命名模块 - Target WSGI script cannot be loaded as Python module + ImportError: No module named ImportError:没有名为cv2的模块— WSGI + python + apache - ImportError: No module named cv2 — WSGI + python + apache 使用WSGI将Flask应用程序部署到Apache时出错-ImportError:没有名为MySQLdb的模块 - Error deploying Flask Application to Apache using WSGI - ImportError: No module named MySQLdb 带有 Flask 应用程序的 mod_wsgi:ImportError:没有名为 Flask 的模块 - mod_wsgi with Flask app: ImportError: No module named flask Django 1.9 Apache错误导入django.core.handlers.wsgi ImportError:没有名为“ django”的模块 - Django 1.9 Apache error import django.core.handlers.wsgi ImportError: No module named 'django'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM