Home > Mobile >  ImportError: cannot import name 'app' in wsgi file
ImportError: cannot import name 'app' in wsgi file

Time:11-07

So I had a flask app running on an apache server and everything was working fine. 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:

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.

Here's my wsgi file:

#!/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
application.secret_key = 'dxJO>BQ,7FXsw^s[t*8mC`<&]o|d@F'

And here is the structure of my app (which is in /var/www/):

WebApp
├── WebApp
│   ├── README.md
│   ├── app.py
│   └── website
│       ├── __init__.py
│       ├── auth.py
│       ├── database.db
│       ├── databases.py
│       ├── models.py
│       ├── static
│       │   ├── form-style.css
│       │   └── index.js
│       └── templates
│           ├── base.html
│           ├── fail.html
│           ├── home.html
│           ├── login.html
│           ├── manage.html
│           ├── reset.html
│           ├── sign_up.html
│           └── success.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. I also tried doing 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. Also, the server crashes before it's able to execute any file aside from the wsgi.

CodePudding user response:

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. 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.

CodePudding user response:

Use

import Webapp.app as application
  • Related