简体   繁体   English

ModuleNotFoundError:没有名为'__main __。web_app'的模块; 从子文件夹导入包时,'__ main__'不是包

[英]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.

相关问题 ModuleNotFoundError:没有名为'__main __。models'的模块; '__main__'不是包 - ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package Python 3 - 在同一目录中导入.py文件 - ModuleNotFoundError:没有名为'__main __。char'的模块; '__main__'不是包 - Python 3 - importing .py file in same directory - ModuleNotFoundError: No module named '__main__.char'; '__main__' is not a package ModuleNotFoundError: 没有名为“__main__.gtts”的模块; '__main__' 不是一个包 - ModuleNotFoundError: No module named '__main__.gtts'; '__main__' is not a package 我遇到一个错误“ ModuleNotFoundError:没有名为'__main __。models'的模块; “ __main__”不是软件包” - I'm having an error “ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package” 在docker中运行龙卷风python:ModuleNotFoundError:没有名为'__main __。config'的模块; '__main__'不是一个包 - running tornado python in docker: ModuleNotFoundError: No module named '__main__.config'; '__main__' is not a package 解决“ModuleNotFoundError: ...'__main__' is not a package”错误 - Working around "ModuleNotFoundError: ...'__main__' is not a package" error ModuleNotFoundError:__main__ 不是包是什么意思? - ModuleNotFoundError: What does it mean __main__ is not a package? 在子包中加载一个pickle文件->模块'__main__'错误 - load a pickle file in a sub package --> error with module '__main__' 安装 ROS 时在 em package 中找不到 __main__ 模块 - __main__ module not found in em package while installing ROS Python:从子包和主包导入子包模块 - Python: importing a sub-package module from both the sub-package and the main package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM