python-3.x 执行相对导入错误的混淆导入错误

rhfm7lfc  于 2023-08-08  发布在  Python
关注(0)|答案(1)|浏览(115)

我在Flask中做了一些事情,我收到了导入错误:

.
____app_name
| |______init__.py
| |____api
| | |______init__.py
| |____app_init.py
| |____extensions.py
| |____models.py
| |____views.py
|____db.sqlite
|____instance
| |____settings.cfg
|____start.py
|____static
|____templates

字符串
我当前的工作目录是/Development/web/python/AppStructure,当我尝试运行python3 start.py

$ python3 start.py 
/usr/local/lib/python3.7/site-packages/flask_marshmallow/__init__.py:27: UserWarning: Flask-SQLAlchemy integration requires marshmallow-sqlalchemy to be installed.
  "Flask-SQLAlchemy integration requires "
Traceback (most recent call last):
  File "start.py", line 1, in <module>
    from app_name.app_init import app
  File "/Development/web/python/AppStructure/app_name/app_init.py", line 5, in <module>
    from .api import api_blueprint
  File "/Development/web/python/AppStructure/app_name/api/__init__.py", line 2, in <module>
    from app_name.extensions import User, user_schema
  File "/Development/web/python/AppStructure/app_name/extensions.py", line 1, in <module>
    from .app_init import app, db, ma
ImportError: cannot import name 'app' from 'app_name.app_init' (/Development/web/python/AppStructure/app_name/app_init.py)


extensions.py

from .app_init import app, db, ma

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    fname = db.Column(db.String(100))
    mname = db.Column(db.String(100))
    lname = db.Column(db.String(100))
    dob = db.Column(db.Date)
    user_type = db.Column(db.String(20))
    email = db.Column(db.String(100), unique=True, nullable=True)
    mobile_phone = db.Column(db.String(12), unique=True, nullable=True)
    landline_phone = db.Column(db.String(20))
    pob = db.Column(db.String(80)) # Place of birth
    government_id = db.Column(db.String(90)) # Passport/ Work Permit Card/ Adhar Card /Residence Card or DL
    government_id_number = db.Column(db.String(150), unique=True)
    registered = db.Column(db.DateTime)
    modified = db.Column(db.DateTime)

    def __init__(
        self, 
        fname, 
        mname, 
        lname, 
        dob, 
        user_type, 
        email, 
        mobile_phone, 
        landline_phone,
        place_of_birth, 
        government_id, 
        government_id_number,
        registered,
        modified
        ):
        self.fname = fname
        self.mname = mname
        self.lname = lname
        self.dob = dob
        self.user_type = user_type
        self.email = email
        self.mobile_phone = mobile_phone
        self.landline_phone = landline_phone
        self.place_of_birth = place_of_birth 
        self.government_id = government_id
        self.government_id_number = government_id_number
        self.registered = registered
        self.modified = modified

# User Schema
class UserSchema(ma.Schema):

    class Meta:
        fields = (
            "id",
            "fname", 
            "mname", 
            "lname", 
            "dob", 
            "user_type", 
            "email", 
            "mobile_phone", 
            "landline_phone",
            "place_of_birth", 
            "government_id", 
            "government_id_number",
            "registered",
            "modified"
            )

# Init Schema 
user_schema = UserSchema()
users_schema = UserSchema(many=True)

  • app_init.py*
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from .views import main
from .api import api_blueprint

def create_app(config_file='settings.cfg'):
    # Init App
    _app = Flask(__name__, instance_relative_config=True)
    _app.config.from_pyfile(config_file)
    # db.init_app(app)
    _app.register_blueprint(main) # View (the website).
    _app.register_blueprint(api_blueprint)
    return _app

app = create_app()
ma = Marshmallow(app)
db = SQLAlchemy(app)


我做错了什么?

eaf3rand

eaf3rand1#

由于extensions.py和app_init.py在同一个文件夹中,所以你不需要

# not working
from .app_init import app, db, ma

字符串
您可以直接这样做:

# working
from app_init import app, db, ma


或(危险,但有效):

# working
from . import app, db, ma


或者你可以直接导入整个文件:

import app_init


希望这对你有帮助。祝你好运。

相关问题