无法从 Python 中部分初始化的模块“连接”导入名称“mydb”

新手上路,请多包涵

Python 3.8 错误

ImportError: cannot import name 'mydb' from partially initialized module 'connection'
(most likely due to a circular import) (C:\U
sers\Mark04\Documents\Python tutorial\databasing\connection.py)

当我试图执行子模块 select.py

 import bcrypt;
from connection import mydb

有一个导入的模块 connection.py

 import mysql.connector
mydb = "Success";

我不知道是什么问题。当我从我的模块 connection.py 中删除 import mysql.connector 时,错误没有出现,但它没有解决我的问题。

 > python -m select

原文由 Mark Anthony Libres 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 668
2 个回答

要回答上面的问题,我们需要了解循环依赖的问题。

为了理解循环依赖,我想在你面前布置一个简单的例子。

我认为每个应用程序都需要具有以下几个基本块:

 +----------------+-------------------------------------------------------------------------------------------+
|    Filename    |                                        Description                                        |
+----------------+-------------------------------------------------------------------------------------------+
| app.py         | Creates the app and starts the server.                                                    |
| models.py      | Define what the entity will look like (e.g, UserModel has username, email, password etc.) |
| controllers.py | Fetches Data from database, generates HTML and sends the response to the user browser.    |
+----------------+-------------------------------------------------------------------------------------------+

我们的简单示例也将包含三个文件

project/
    - app.py ( Creates and starts the server)
    - models.py ( Class to model a user)
    - controllers.py ( We will fetch data from database, and return html to user.)

app.py 文件的内容如下所示:

 # =============
#     app.py
# =============

# Define the application
app = Flask()

# Define the Database
db = SQLAlchemy(app)

# Register the Controller
from .controllers import auth_controller
app.register_blueprint(auth_controller)

models.py 文件的内容如下所示:

 # =============
#     models.py
# =============

from .app import db

# We will not focus on implementation
class User(db.Model):
    pass

controllers.py 文件的内容如下所示:

 # =============
#     controllers.py
# =============
from flask import Blueprint
from .models import User

# Create the auth app
auth = Blueprint('auth', __name__)

# Define the Rotues
@auth.route('/login')
def login():
    return "I will fetch some data and allow the user to login"

我想现在,我已经列出了我们应用程序的图表,现在让我们继续了解该应用程序的工作原理。

  1. 该应用程序从 app.py 开始
  2. app app.py 文件中的变量在内存中创建。
  3. db 变量 app.py 在内存中创建。
  4. 现在,要从 controllers.py 文件导入 auth --- 文件,我们切换到 ```controllers.py`` 文件
  5. 我们从烧瓶中导入 Blueprint
  6. 要导入 User ,我们切换到 models.py 文件。
  7. 现在,我们在 models.py 文件中导入 db (我们能够导入它,因为它是在步骤 3 中创建的)
  8. 程序继续等等……

上面序列中最重要的导入步骤是 step 7 ,因为它会在我们的应用程序中导致循环依赖的问题,一会儿。

现在我们将尝试更改 app.py 文件来引入循环依赖的问题。

现在,作为开发人员,我们可能会认为我们所有的导入都应该放在文件的顶部,这不是让您的代码更干净吗?是的当然!它确实使代码更清晰。

 # ============================
#       Refactored app.py file
# ============================
from .controllers import auth_controller

# ......
# Rest of the file is same, we only shifted this import at the top

现在,我们的应用程序存在循环依赖问题。让我告诉你怎么做?

  1. 我们的应用程序从 app.py 文件开始
  2. 首先,我们需要导入 auth_controller 来自 controllers.py 文件
  3. 让我们访问 controllers.py 文件,并处理它。
  4. 我们从烧瓶中导入蓝图
  5. 让我们切换到 models.py 要导入的文件 User
  6. models.py 文件中,我们从 app 导入 db (但数据库尚不存在。)

现在,我想你明白了,刚才看到的问题是循环依赖的一个例子。在您的情况下,同样的问题导致 ImportError

解决方案是检查 import statements 并将它们放在正确的位置。有时,我们使用代码格式化程序,它会重构顶部的所有导入语句。这可能是您遇到问题的原因。

我希望这可以回答你的问题!

原文由 Jayant Malik 发布,翻译遵循 CC BY-SA 4.0 许可协议

导入顺序很重要:

例子:

 # A.py
# empty file

 # B.py
import A

 # file1.py
import A # A gets imported before B can import A
import B # B tries to re-import A but A is already imported

将顺序更改为:

 # file1.py
import B
import A

原文由 Ben 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题