Python Flask NoAppException:在导入"微博"时,引发了一个ImportError:

时间:2018-06-05 02:15:31

标签: python flask

__init__.py

from flask import Flask
app = Flask(__name__)
from app import routes

microblog.pyset FLASK_APP=microblog.py

from app import app

app.py

from flask import Flask
app = Flask(__name__)
from app import routes

routes.py

from app import app

@app.route('/')
@app.route('/index')
def index():
    return "Hello, World!"

run.py

#!flask/bin/python
from app import app
app.run(debug=True)

当我flask run时,我得到:

flask.cli.NoAppException
NoAppException: While importing "microblog", an ImportError was raised:

Traceback (most recent call last):
  File "c:\python27\lib\site-packages\flask\cli.py", line 235, in locate_app
    __import__(module_name)
  File "C:\Users\ACER\FlaskTEST\venv\app\MicroBlog\microblog.py", line 1, in <module>
    from app import app
  File "C:\Users\ACER\FlaskTEST\venv\app\MicroBlog\app\__init__.py", line 5, in <module>
    from app import routes
  File "C:\Users\ACER\FlaskTEST\venv\app\MicroBlog\app\app.py", line 4, in <module>
    from app import routes
ImportError: cannot import name routes

1 个答案:

答案 0 :(得分:2)

从python模块名称开始,我假设您正在使用Miguel Grindberg创建的Flask Mega-Tutorial

Mega-Tutorial的目录结构如下:

当前工作目录:

  • microblog.py
  • 名为app的文件夹/目录

Current_Working_Directory / app:

  • __init__.py
  • routes.py

请注意,您的python模块和Miguel的模块之间有两个区别:

  1. 您有一个名为run.py的重复文件,该文件执行与他的microblog.py相同的操作。要解决此问题,您可以保留run.py,将其放置在当前工作目录中,然后set FLASK_APP=run.py,也可以删除run.py并在调用microblog.py时使用flask run它将运行您设置为FLASK_APP的内容。

  2. 您有一个名为app.py的不必要的python模块,请将其删除,就可以了。

请记住,您随时可以查看Miguel's Microblog GitHub来仔细检查您的代码是否与每章结尾处的代码匹配。