Flask ImportError:无法导入名称''

时间:2018-03-02 15:23:59

标签: python flask import

这是我的应用程序树,因为这个应用程序比较大,我使用蓝图分区结构来构建它。

RPOSS
├───.idea
├───app
│   ├───customer_panel
│   │   ├───static
│   │   ├───templates
│   │   └───__pycache__
│   ├───owner_panel
│   │   ├───static
│   │   │   └───js
│   │   │       └───lib
│   │   ├───templates
│   │   └───__pycache__
│   ├───progressive_panel
│   │   ├───static
│   │   ├───templates
│   │   └───__pycache__
│   ├───static
│   ├───templates
│   └───__pycache__
├───Include
├───instance
│   └───__pycache__
├───Lib
├───Scripts
└───__pycache__

RPOSS/app/run.py

from app import app
app.run()

RPOSS/app/__init__.py

from app.views import Rmod
from app.customer_panel.views import Cmod
from app.owner_panel.views import Omod
from app.progressive_panel.views import Smod
from instance.config import engine
from sqlalchemy.orm import sessionmaker


app = Flask(__name__,
        static_folder='./static',
        instance_relative_config=True,
        instance_path=r"C:\Users\Orbit\RPOSS\instance")
app.config.from_object('config')
app.config.from_pyfile('config.py')

bcrypt = Bcrypt(app)
Session = sessionmaker(bind=engine)
db_session = Session()
Bootstrap(app)

app.register_blueprint(Cmod)
app.register_blueprint(Rmod, url_prefix="/RPOSS")
app.register_blueprint(Omod, url_prefix="/RPOSS/owner_panel")
app.register_blueprint(Smod, url_prefix="/RPOSS/progressive_panel")

RPOSS/app/views.py

from flask import ...
from app.forms import ...
from app.models import ClassName


Rmod = Blueprint('RPOSS', __name__,
             template_folder='templates',
             static_folder='static')

RPOSS/app/models.py

from app import bcrypt
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import ... 


Base = declarative_base()


class ClassName(Base):
    # I am using bcrypt here to hash values
    ...

注意:我运行了模型并创建了数据库,一切正常。

Traceback (most recent call last):
  File "C:/Users/Orbit/RPOSS/app/models.py", line 1, in <module>
    from app import bcrypt
  File "C:\Users\Orbit\RPOSS\app\__init__.py", line 7, in <module>
    from app.views import Rmod
  File "C:\Users\Orbit\RPOSS\app\views.py", line 3, in <module>
    from app.models import Employee
  File "C:\Users\Orbit\RPOSS\app\models.py", line 1, in <module>
    from app import bcrypt
ImportError: cannot import name 'bcrypt'

有人可以帮我弄清楚发生了什么吗?

1 个答案:

答案 0 :(得分:1)

您有循环导入。

app.models尝试导入app以解析名称bcrypt

  File "C:/Users/Orbit/RPOSS/app/models.py", line 1, in <module>
    from app import bcrypt

app/__init__.py文件首先导入另一个模块,app.views

  File "C:\Users\Orbit\RPOSS\app\__init__.py", line 7, in <module>
    from app.views import Rmod

此模块想要从app.models导入:

  File "C:\Users\Orbit\RPOSS\app\views.py", line 3, in <module>
    from app.models import Employee

这是您开始使用的文件,但您可能将其作为脚本运行,因此由Python导入__main__,这意味着Python将在<{1}下再次导入 名字。

但是现在你遇到了问题,因为它再次导入app.model

app

此时, File "C:\Users\Orbit\RPOSS\app\models.py", line 1, in <module> from app import bcrypt 已经创建,但尚未完成导入。名称app尚未分配给。

在导入bcrypt之前,您需要将名称bcrypt移到一行。将行app.model移至下方 from app.views import Rmod行,或稍后使用单独的函数导入您的蓝图。

我更喜欢使用Application Factory来设置应用和蓝图。更改您的bcrypt = Bcrypt(app)方法以在工厂函数中导入蓝图。