enter image description here

Saya mengalami banyak masalah dalam mengimpor modul. Konten dalam file saya ada di atas:

Run.py:

from erp import app

if __name__ == '__main__':
    app.run(debug=True)

All_blueprints.py:

from flask import Flask
from flask_restful import Api
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow

from blueprint_finance.all_resources import api_finance

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:////tmp/test.db"
db = SQLAlchemy(app)
ma = Marshmallow(app)

app.register_blueprint(api_finance)

Erp/init.py

from .all_blueprints import app, db, ma

Database1.py

from erp import app

class Author(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255))

Saya terus mendapatkan kesalahan impor, yang sekarang adalah

Traceback (panggilan terakhir terakhir): File "/home/arsalan/python_practise/MY_WORK_FILES/React_works/React_Container_Mount/backend/run.py", baris 25, di dari aplikasi impor erp File "/home/arsalan/python_practise/MY_WORK_FILES/React_works/React_Container_Mount/backend/erp/init.py", baris 1, di dari aplikasi impor .all_blueprints, db, ma File "/home/arsalan/python_practise/MY_WORK_FILES/React_works/React_Container_Mount/backend/erp/all_blueprints.py", baris 12, di dari blueprint_finance.all_resources impor api_finance ModuleNotFoundError: Tidak ada modul bernama 'blueprint_finance'

Jika ada yang bisa menunjukkan kesalahannya, itu akan sangat membantu karena saya tersesat sejak kemarin. Terima kasih banyak!

0
Arsalan Ahmad Ishaq 13 Maret 2020, 15:03

1 menjawab

Jawaban Terbaik

Saya pikir cara Anda membuat cetak biru Anda bukanlah cara yang benar (Lihat link ini untuk penjelasan rinci).

Jika kami merujuk pada cara Anda menyusun kode,

erp/
    blueprint_finance     ---> is your blueprint package
        __init__.py       ---> is where you create your blueprint
    all_blueprints.py     ---> is where you register your blueprint

Mari kita mulai dengan file erp/blueprint_finance/__init__.py:

from flask import Blueprint

bp = Blueprint('blueprint_finance', __name__)

from erp.blueprint_finance import all_resources
from erp.blueprint_finance.finances_resources import resource1

erp/all_blueprints.py

from flask import Flask
from flask_restful import Api
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:////tmp/test.db"
db = SQLAlchemy(app)
ma = Marshmallow(app)

from erp.blueprint_finance import bp as blueprint_finance_bp
app.register_blueprint(blueprint_finance_bp)
1
Tobin 13 Maret 2020, 13:29