问题
Azure gives a module import error but the same code runs perfectly locally.
2019-05-29T02:50:47.388004719Z: [ERROR] Traceback (most recent call last):
2019-05-29T02:50:47.388027419Z: [ERROR] File "/usr/local/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2019-05-29T02:50:47.388031719Z: [ERROR] worker.init_process()
2019-05-29T02:50:47.388035419Z: [ERROR] File "/usr/local/lib/python3.6/site-packages/gunicorn/workers/base.py", line 129, in init_process
2019-05-29T02:50:47.388039419Z: [ERROR] self.load_wsgi()
2019-05-29T02:50:47.388043019Z: [ERROR] File "/usr/local/lib/python3.6/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
2019-05-29T02:50:47.388047119Z: [ERROR] self.wsgi = self.app.wsgi()
2019-05-29T02:50:47.388050819Z: [ERROR] File "/usr/local/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
2019-05-29T02:50:47.388054619Z: [ERROR] self.callable = self.load()
2019-05-29T02:50:47.388075019Z: [ERROR] File "/usr/local/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
2019-05-29T02:50:47.388084919Z: [ERROR] return self.load_wsgiapp()
2019-05-29T02:50:47.388088619Z: [ERROR] File "/usr/local/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
2019-05-29T02:50:47.388092219Z: [ERROR] return util.import_app(self.app_uri)
2019-05-29T02:50:47.388095619Z: [ERROR] File "/usr/local/lib/python3.6/site-packages/gunicorn/util.py", line 350, in import_app
2019-05-29T02:50:47.388099119Z: [ERROR] __import__(module)
2019-05-29T02:50:47.388102419Z: [ERROR] File "/home/site/wwwroot/app.py", line 102, in <module>
2019-05-29T02:50:47.388106219Z: [ERROR] from views.auth import register_user_bp, user_login_bp, user_logout_bp
2019-05-29T02:50:47.388109619Z: [ERROR] File "/home/site/wwwroot/views/auth.py", line 4, in <module>
2019-05-29T02:50:47.388113619Z: [ERROR] from Forms import *
2019-05-29T02:50:47.388116919Z: [ERROR] ModuleNotFoundError: No module named 'Forms'
It seems to find one of my custom modules, but not the one in the root directory from a subdirectory. However, it appears to successfully import Config.py from app.py, which are both in the root directory.
The program runs from app.py in the root directory if that is important
I think there is something about the way that gunicorn/WSGI works that I don't understand. I can't figure out what changes the way imports behave.
project_root
├── app.py
├── Config.py
├── Forms.py
├── __init__.py
├── manage.py
├── migrations
├── Models.py
├── requirements.txt
├── static
├── templates
│ ├── change_profile.html
│ ├── dash_app_layout.py
│ ├── default_login.html
│ ├── default_logout.html
│ ├── _formhelpers.html
│ ├── header.html
│ ├── index.html
│ ├── __init__.py
│ ├── nav_layout.py
│ ├── record_meeting.html
│ ├── register_user.html
│ ├── upload_meeting.html
│ └── voice_enroll.html
├── utils
│ ├── audio_processor
│ │ ├── AudioProcessor.py
│ │ ├── audio_type_converter.py
│ │ ├── Deidentifier.py
│ │ ├── Identification
│ │ ├── __init__.py
│ │ ├── Utterance.py
│ │ └── voice_enroll.py
│ ├── db_operations.py
│ ├── __init__.py
│ ├── interim_result_processor.py
│ └── utils.py
└── views
├── auth.py
├── dash_callbacks.py
├── __init__.py
├── meeting_upload.py
├── profile.py
└── record_meeting.py
I'm not sure why it works fine locally, but can't import from Forms.py when on the webapp. I'm pretty sure it has something to do with the file structure, but I can't figure out what and how to fix it.
回答1:
Try move the Forms.py
to folder views
or
add these two line in auth.py
import sys
sys.path.append('../')
to add the '../' path to the PYTHONPATH.
Hope it works.
回答2:
I tried to reproduce your issue, then I discovered the issue was possibly caused by missing your project path in the sys.path
.
I guess that you developed locally in a Python virtual environment, so the sys.path
will include your project path automatically when you command source <project_root>/bin/active
to enable virtual environment. But without virtual environment, your project path will not be default imported into the sys.path
of system Python runtime.
So the solution is that you can manually append /home/site/wwwroot
to sys.path
via add new code line into app.py
as below.
from flask import Flask
import sys
sys.path.append('/home/site/wwwroot')
# Then you can normally import Forms module
from Forms import *
来源:https://stackoverflow.com/questions/56352494/flask-app-cant-find-module-when-deployed-to-azure-works-fine-locally