简体   繁体   English

导入错误:无法在 wsgi 文件中导入名称“app”

[英]ImportError: cannot import name 'app' in wsgi file

So I had a flask app running on an apache server and everything was working fine.所以我在 apache 服务器上运行了一个烧瓶应用程序,一切正常。 I decided to restart the sever (sudo service apache2 restart) and for some reason this caused the following error to arise when I tried to access the IP:我决定重新启动服务器(sudo service apache2 restart),由于某种原因,当我尝试访问 IP 时,这导致出现以下错误:

File "/var/www/WebApp/webapp.wsgi", line 7, in <module>
    from WebApp import app as application
ImportError: cannot import name 'app'

However, I never changed anything in the wsgi file or moved the location of any file or directory.但是,我从未更改 wsgi 文件中的任何内容或移动任何文件或目录的位置。

Here's my wsgi file:这是我的 wsgi 文件:

#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0, "/var/www/WebApp/")

from WebApp import app as application

And here is the structure of my app (which is in /var/www/):这是我的应用程序的结构(在 /var/www/ 中):

WebApp
├── WebApp
│   ├── README.md
│   ├── app.py
│   └── website
│       ├── __init__.py
│       ├── auth.py
│       ├── database.db
│       ├── models.py
│       ├── static
│       │   ├── style.css
│       │   └── index.js
│       └── templates
│           ├── base.html
│           ├── home.html
│           ├── login.html
│           ├── register.html
└── webapp.wsgi

I have tried changing the import line to我尝试将导入行更改为

from WebApp.app import app as application (says no module named WebApp.app)
from app import app as application
from .WebApp import app as application
from .WebApp.app import app as application
import app as application

And none of them work.它们都不起作用。 And again, what I have in my wsgi file was working for days before last night when it began failing.再说一次,我在 wsgi 文件中的内容在昨晚开始失败之前已经工作了几天。 I also tried doing chmod a+x webapp.wsgi.我也尝试过 chmod a+x webapp.wsgi。

Does anyone have any advice as to how to solve this problem?有没有人对如何解决这个问题有任何建议?

Note: I know there is a very similar stack overflow question on here, but the only answer suggests to add an init.py file, which I already have.注意:我知道这里有一个非常相似的堆栈溢出问题,但唯一的答案建议添加一个我已经拥有的 init.py 文件。 Also, the server crashes before it's able to execute any file aside from the wsgi.此外,服务器在能够执行 wsgi 之外的任何文件之前就崩溃了。

import Webapp.app as application

When this happens, it's an exception handler hiding details.当发生这种情况时,它是一个隐藏细节的异常处理程序。 If you import the root of your app from a Python shell (in a virtual environment, if that's what you're using), more will be revealed.如果您从 Python shell 导入应用程序的根目录(在虚拟环境中,如果这是您使用的),则会显示更多信息。 Here, I forced an error in one my apps:在这里,我在我的一个应用程序中强制出错:

(venv) $ python
Python 3.6.9 (default, Jan 26 2021, 15:33:00) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import wsgi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/vagrant/toydeploy/wsgi.py", line 3, in <module>
    app = create_app()
  File "/home/vagrant/toydeploy/app/__init__.py", line 62, in create_app
    from app.errors import bp as errors_bp  # noqa
  File "/home/vagrant/toydeploy/app/errors/__init__.py", line 6, in <module>
    from app.errors import handlers  # noqa
  File "/home/vagrant/toydeploy/app/errors/handlers.py", line 6, in <module>
    oops
NameError: name 'oops' is not defined
>>> 

Installing and using flake8 is another way of spotting problems early.安装和使用flake8是另一种早期发现问题的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 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 ImportError:无法导入名称'app' - ImportError: cannot import name 'app' Apache + WSGI运行Flask,获取Python ImportError:“无法导入名称…”或“未命名模块…” - Apache + WSGI to run Flask, get Python ImportError: “cannot import name …” or “No module named …” 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:无法导入名称ResponseError - ImportError: cannot import name ResponseError ImportError:无法导入名称tz(psycopg2) - ImportError: cannot import name tz (psycopg2) Django错误ImportError at /无法导入名称模型 - Django Error ImportError at / cannot import name models 错误:在wsgi文件中部署Flask应用程序时出现ImportError - Error: ImportError while deploying Flask app in wsgi file WSGI / APACHE / DJANGO .. ImportError:无法导入设置 - WSGI / APACHE / DJANGO .. ImportError: Could not import settings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM