我的核心Flask应用代码如下:
from flask import Flask
from action_test import test1, test2
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.debug = True
app.run()
然而,当我尝试用python运行它时,没有真正发生的事情。服务器没有启动,我没有收到任何错误。这个过程似乎开始并在一个循环中反复思考。
如果我注释掉该行
#from action_test import test1, test2
服务器正常启动。如果我进入python解释器并执行相同的代码行
from action_test import test1, test2
就其本身而言也没有任何问题。
我的action_test代码如下:
import action2
def test1():
fl = action2.FollowersList()
mech = action2.Mechanics()
manager = action2.Manager()
manager.launch(mech,fl)
for i in range(0,10):
manager.iterate(mech,fl)
def test2():
messageList = []
fl = action2.FollowersList()
mech = action2.Mechanics()
manager = action2.Manager()
manager.launch(mech,fl)
for i in range(0,2000):
message = manager.iterate(mech,fl)
messageList.append(message)
return messageList
该模块还依赖于导入另一个模块,该模块具有丰富的类和功能。当我自己导入action_test时,一切正常,两个函数test1和test2从解释器执行没有问题。模块action2完全独立于瓶子,旨在只执行一些脚本,并且不会定义任何@ app.route类型的东西或与Flask相关的任何其他东西。
为什么Flask可能在导入此模块时遇到麻烦?
答案 0 :(得分:1)
使用pdb
或ipdb
。
当我使用Flask示例目录中的flaskr
示例应用程序时(请小心使用您安装的相同版本,master现在包含损坏的更改),它看起来像这样:
$ ((0.10.1)) ipdb flaskr.py
/usr/local/lib/python2.7/dist-packages/IPython/frontend.py:30: UserWarning: The top-level `frontend` package has been deprecated. All its subpackages have been moved to the top `IPython` level.
warn("The top-level `frontend` package has been deprecated. "
WARNING: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv.
WARNING: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv.
> /home/javl/sandbox/flaskr/flask/examples/flaskr/flaskr.py(11)<module>()
10 :license: BSD, see LICENSE for more details.
---> 11 """
12
?
查看现有命令的帮助ipdb> ?
Documented commands (type help <topic>):
========================================
EOF bt cont enable jump pdef psource run unt
a c continue exit l pdoc q s until
alias cl d h list pfile quit step up
args clear debug help n pinfo r tbreak w
b commands disable ignore next pinfo2 restart u whatis
break condition down j p pp return unalias where
Miscellaneous help topics:
==========================
exec pdb
Undocumented commands:
======================
retval rv
n
next
ipdb> n
> /home/javl/sandbox/flaskr/flask/examples/flaskr/flaskr.py(13)<module>()
12
---> 13 from sqlite3 import dbapi2 as sqlite3
14 from flask import Flask, request, session, g, redirect, url_for, abort, \
ipdb> n
> /home/javl/sandbox/flaskr/flask/examples/flaskr/flaskr.py(14)<module>()
13 from sqlite3 import dbapi2 as sqlite3
---> 14 from flask import Flask, request, session, g, redirect, url_for, abort, \
15 render_template, flash, _app_ctx_stack
ipdb>
> /home/javl/sandbox/flaskr/flask/examples/flaskr/flaskr.py(18)<module>()
17 # configuration
---> 18 DATABASE = '/tmp/flaskr.db'
19 DEBUG = True
对我而言,在我进入app.run()
行时,它就会中断。但是在你的情况下,你在应用程序启动时遇到了一些循环,所以如果你到了app.run()
,你应该调试你的应用程序。
我的猜测是,你将action_test.py
命名为。如果是这种情况,请将其重命名为与您的软件包和模块不冲突的内容。