我是Flask的新手,并且在flask_lab文件夹中flask run
时,我下面导入了相关的AttributeError。
任何帮助将不胜感激。
工作目录:
flask_lab
├── __init__.py
├── Pipfile
├── Pipfile.lock
├── README.md
├── tmp
│ ├── __init__.py
│ └── test.py
└── app.py
flask_lab / app.py:
import os
import click
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import flask_lab.tmp.test.demo
app = Flask(__name__)
prefix = 'sqlite:////'
app.config['SQLALCHEMY_DATABASE_URI'] = prefix + os.path.join(app.root_path, 'data.db')
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(20))
flask_lab.tmp.test.demo()
flask_lab / tmp / test.py:
import flask_lab.app
print(flask_lab.app.db)
def demo():
print('yeah!')
错误:
Traceback (most recent call last):
File "/home/huafeng/.local/share/virtualenvs/flask_lab-x_qc9OjH/bin/flask", line 10, in <module>
sys.exit(main())
File "/home/huafeng/.local/share/virtualenvs/flask_lab-x_qc9OjH/lib/python3.6/site-packages/flask/cli.py", line 894, in main
cli.main(args=args, prog_name=name)
File "/home/huafeng/.local/share/virtualenvs/flask_lab-x_qc9OjH/lib/python3.6/site-packages/flask/cli.py", line 557, in main
return super(FlaskGroup, self).main(*args, **kwargs)
File "/home/huafeng/.local/share/virtualenvs/flask_lab-x_qc9OjH/lib/python3.6/site-packages/click/core.py", line 717, in main
rv = self.invoke(ctx)
File "/home/huafeng/.local/share/virtualenvs/flask_lab-x_qc9OjH/lib/python3.6/site-packages/click/core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/huafeng/.local/share/virtualenvs/flask_lab-x_qc9OjH/lib/python3.6/site-packages/click/core.py", line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/huafeng/.local/share/virtualenvs/flask_lab-x_qc9OjH/lib/python3.6/site-packages/click/core.py", line 555, in invoke
return callback(*args, **kwargs)
File "/home/huafeng/.local/share/virtualenvs/flask_lab-x_qc9OjH/lib/python3.6/site-packages/click/decorators.py", line 64, in new_func
return ctx.invoke(f, obj, *args, **kwargs)
File "/home/huafeng/.local/share/virtualenvs/flask_lab-x_qc9OjH/lib/python3.6/site-packages/click/core.py", line 555, in invoke
return callback(*args, **kwargs)
File "/home/huafeng/.local/share/virtualenvs/flask_lab-x_qc9OjH/lib/python3.6/site-packages/flask/cli.py", line 767, in run_command
app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)
File "/home/huafeng/.local/share/virtualenvs/flask_lab-x_qc9OjH/lib/python3.6/site-packages/flask/cli.py", line 293, in __init__
self._load_unlocked()
File "/home/huafeng/.local/share/virtualenvs/flask_lab-x_qc9OjH/lib/python3.6/site-packages/flask/cli.py", line 317, in _load_unlocked
self._app = rv = self.loader()
File "/home/huafeng/.local/share/virtualenvs/flask_lab-x_qc9OjH/lib/python3.6/site-packages/flask/cli.py", line 377, in load_app
raise_if_not_found=False)
File "/home/huafeng/.local/share/virtualenvs/flask_lab-x_qc9OjH/lib/python3.6/site-packages/flask/cli.py", line 235, in locate_app
__import__(module_name)
File "/home/huafeng/Desktop/flask_lab/app.py", line 7, in <module>
import flask_lab.tmp.test.demo
File "/home/huafeng/Desktop/flask_lab/tmp/test.py", line 3, in <module>
print(flask_lab.app.db)
AttributeError: module 'flask_lab' has no attribute 'app'
收到来自stackoverflow的抱怨,抱怨代码太多,细节不够。...更多的单词,更多的单词......
答案 0 :(得分:0)
在解释app.py
时,如果python解释器遇到行import flask_lab.tmp.test.demo
,则它将立即开始解释tmp / test.py。但是tmp / test.py再次导入flask_lab。
这时,由于解释器已经遇到了flask_lab,它将开始在该命名空间中搜索app
。但是它直到那条线才达到。由于您已经在模块中定义tmp.test
之前导入了app
,因此尚无flask_lab.app
,因此会出现错误。
如果直接调用tmp.test,那么也会遇到循环导入错误。
因此,解决方法是避免循环导入。将db对象移动到一个单独的模块,并在这两个模块中调用它。 Flask-SQLAlchemy提供了一种称为init_app
的方法,用于这种用例。
让我们创建一个名为common的模块,其中将包含公共变量。
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
import os
import click
from flask import Flask
from .common import db
from .tmp.test import demo
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(20))
app = Flask(__name__)
prefix = 'sqlite:////'
app.config['SQLALCHEMY_DATABASE_URI'] = prefix + os.path.join(app.root_path, 'data.db')
db.init_app(app)
demo()
from .common import db
print(db)
def demo():
print('yeah!')
请注意,我还用相对进口代替了flask_lab
进口。他们更干净。包内的代码应避免在导入中使用包名。这样一来,如果您以后更改软件包名称,则无需更改其中的所有代码即可。