ModuleNotFoundError: No module named '__main__.web_app'; '__main__' is not a package while importing package from sub folder

ModuleNotFoundError: No module named '__main__.web_app'; '__main__' is not a package while importing package from sub folder

我在 运行 安装我的应用程序时遇到上述错误。我想将应用程序从 web_app/init.py 导入 run.py 文件到 gevent 上的 运行。我的项目结构如下:

myapp
 |---config.ini
 |---run.py
 |---web_app
        |----__init__.py


run.py

from gevent.pywsgi import WSGIServer
import configparser

from .web_app import app
# from web_app.__init__ import app

config = configparser.ConfigParser()
config.read('C:/workspace/Python/myapp/config.ini')
PORT = config.get('SERVER', 'PORT')
PORT = int(PORT)


if __name__ == '__main__':
    print('Serving on port ', PORT)
    WSGIServer(('localhost', PORT), app).serve_forever()

__init.py

from flask import Flask
app = Flask(__name__)
app.config['APPLICATION_ROOT'] = '/myapp'
logger = log_configuration.get_logger(__name__)

def simple(env, resp):
   resp(b'200 OK', [(b'Content-Type', b'application/json')])
   return [b'Hello Verimed User']

@app.route('/test', methods=['GET'])
def test():
   return jsonify({'tasks': "task"})

如果我将 run.py 放在 init 旁边,那么它工作正常。但我想将 run.py 保留在 web_app 文件夹之外。 如何解决这个问题。我一直在努力。

你试过不带“.”吗?在 "web_app"?

前面
from web_app import app

您可以在以下内容中找到更多信息 link:relative imports

run.py.

中添加以下代码后,我解决了上述问题
import sys
sys.path.append('../web_app')
sys.path.insert(0,'./web_app')