Python 3.8 error
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)
When I tried to execute child module select.py
import bcrypt; from connection import mydb
That has an imported module connection.py
import mysql.connector mydb = "Success";
I don't know what is the problem. The error doesn't appear when I remove import mysql.connector from my module connection.py, but it does not solve my problem.
> python -m select
The Python "ImportError: cannot import name" occurs when we have circular imports (importing members between the same files). To solve the error, move the objects to a third file and import them from a central location in other files, or nest one of the imports in a function.
You can, however, use the imported module inside functions and code blocks that don't get run on import. Generally, in most valid cases of circular dependencies, it's possible to refactor or reorganize the code to prevent these errors and move module references inside a code block.
Generally, the Python Circular Import problem occurs when you accidentally name your working file the same as the module name and those modules depend on each other. This way the python opens the same file which causes a circular loop and eventually throws an error.
To answer the above question, we need to understand the problem of circular dependency.
To understand the circular dependency, I want to layout a simple example, in front of you.
I think every app needs to have the few basic blocks as follows:
+----------------+-------------------------------------------------------------------------------------------+ | 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. | +----------------+-------------------------------------------------------------------------------------------+
Our simple example will also have three files
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.)
The contents of the app.py file will look as follows:
# ============= # 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)
The contents of the models.py file will look as follows:
# ============= # models.py # ============= from .app import db # We will not focus on implementation class User(db.Model): pass
The contents of the controllers.py file will look as follows:
# ============= # 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"
I think now, I have laid out a diagram of our app, now let's proceed to understanding how the app will work.
app.py
app
variable inside the app.py
file gets created in memory.db
variable inside the app.py
gets created in memory.auth
from controllers.py
file we switch to ```controllers.py`` fileBlueprint
from flask.User
, we switch to models.py
file.models.py
file we import db
(We are able to import it because it was created in step 3)The most important import step in the above sequence is step 7
, becuase it will cause the problem of circular dependency in our app, in just a moment.
Now we will try to change the app.py
file to introduce the problem of circular dependency.
Now, as a developer, we might think that all our imports should be at the top of the file, doesn't it make your code cleaner. Yes, of course! it does make the code cleaner.
# ============================ # Refactored app.py file # ============================ from .controllers import auth_controller # ...... # Rest of the file is same, we only shifted this import at the top
Now, we have a problem of circular dependency in our app. Let me show you, how?
app.py
fileauth_controller
from controllers.py
filecontrollers.py
file, and process it.models.py
file to import User
models.py
file, we import db
from app
(But db does not exist yet.) Now, I think you got the point, The problem if just saw, was an example of circular dependency. The same problem is causing the ImportError
in your case.
The solution would be to inspect the import statements
and put them at the right place. Sometimes, we use code formatter, and it refactors all the import statements at the top. This might be causing the problem for you.
I hope the this may answer your question!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With