利用flask框架进行个人主页开发,在manage.py中利用flask_script添加了shell运行命令,整个manage.py文件代码如下:
"""the launcher of the whole app"""
#!/usr/bin/env python
import os
from app import create_app, db
from flask.ext.script import Manager, Shell
from flask.ext.migrate import Migrate, MigrateCommand
from app.models import User
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)
migrate = Migrate(app, db)
def make_shell_context(): # initialize the running context of python shell
return dict(app=app, db=db)
manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand)
@manager.add_command
def test():
"""Run the unit tests."""
import unittest
tests = unittest.TestLoader().discover('tests')
unittest.TextTestRunner(verbosity=2).run(tests)
if __name__ == "__main__":
manager.run()
最后在终端执行python manage.py shell命令时,遇到报错如下:
Traceback (most recent call last):
File "manage.py", line 27, in <module>
manager.run()
File "/home/curry/myproject/blog/venv/local/lib/python2.7/site-packages/flask_script/__init__.py", line 405, in run
result = self.handle(sys.argv[0], sys.argv[1:])
File "/home/curry/myproject/blog/venv/local/lib/python2.7/site-packages/flask_script/__init__.py", line 342, in handle
app_parser = self.create_parser(prog)
File "/home/curry/myproject/blog/venv/local/lib/python2.7/site-packages/flask_script/__init__.py", line 168, in create_parser
command_parser = command.create_parser(name, parents=[options_parser])
AttributeError: 'function' object has no attribute 'create_parser'
似乎是flask_script本身运行出了问题,对于这种追溯到module源代码中的错误分析不太知道分析方法,希望有高人能够讲解一下,谢谢
认证高级PHP讲师