[英]ModuleNotFoundError: No module named '__main__.web_app'; '__main__' is not a package while importing package from sub folder
I'm getting above error while running my app. 我在运行我的应用程序时遇到了上述错误。 I want to import app from web_app/ init .py into run.py file to run on gevent. 我想将web_app / init .py中的应用程序导入run.py文件以在gevent上运行。 My project structure is like: 我的项目结构如下:
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 __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"})
If I keep run.py beside init then it's working fine. 如果我在init旁边保持run.py,那么它工作正常。 But I want to keep run.py outside web_app folder. 但我想将run.py保留在web_app文件夹之外。 How to rosolve this. 如何解决这个问题。 I tried all the way. 我一路试过。
Have you tried it without the "." 你试过没有“。”吗? in front of "web_app"? 在“web_app”面前?
from web_app import app
You can find some more info in the following link: relative imports 您可以在以下链接中找到更多信息: relative imports
I resolved above problem after added following code into the run.py . 在将以下代码添加到run.py后,我解决了上述问题。
import sys
sys.path.append('../web_app')
sys.path.insert(0,'./web_app')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.