简体   繁体   中英

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

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. But I want to keep run.py outside web_app folder. How to rosolve this. I tried all the way.

Have you tried it without the "." in front of "web_app"?

from web_app import app

You can find some more info in the following link: relative imports

I resolved above problem after added following code into the run.py .

import sys
sys.path.append('../web_app')
sys.path.insert(0,'./web_app')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM