__init__.py
from flask import Flask
app = Flask(__name__)
from app import routes
microblog.py
,set 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
答案 0 :(得分:2)
从python模块名称开始,我假设您正在使用Miguel Grindberg创建的Flask Mega-Tutorial。
Mega-Tutorial的目录结构如下:
microblog.py
app
的文件夹/目录__init__.py
routes.py
请注意,您的python模块和Miguel的模块之间有两个区别:
您有一个名为run.py
的重复文件,该文件执行与他的microblog.py
相同的操作。要解决此问题,您可以保留run.py
,将其放置在当前工作目录中,然后set FLASK_APP=run.py
,也可以删除run.py
并在调用microblog.py
时使用flask run
它将运行您设置为FLASK_APP
的内容。
您有一个名为app.py
的不必要的python模块,请将其删除,就可以了。
请记住,您随时可以查看Miguel's Microblog GitHub来仔细检查您的代码是否与每章结尾处的代码匹配。