cannot import name 'mydb' from partially initialized module 'connection' in Python

importerror: cannot import name python3
cannot import name django
pycharm importerror: cannot import name
importerror cannot import name join
importerror cannot import name device
importerror cannot import name unknown location
importerror cannot import name emailmessage
gunicorn cannot import name

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 dunno what is the problem, the error doesn't appear when I remove import mysql.connector from my module connection.py but it does not solved my problem

> python -m select

This error comes when you are attempting circular import. make sure you have not created any file with the same name with that of the module you are trying to import in the directory.

Traps for the Unwary in Python's Import System — Nick Coghlan's , This has changed in Python 3.3: now any directory on sys.path with a name that "<string>", line 1, in <module> ImportError: No module named example.foo. The problem is that you have a circular import: in app.py. from mod_login import mod_login in mod_login.py. from app import app This is not permitted in Python. See Circular import dependency in Python for more info. In short, the solution are. either gather everything in one big file; delay one of the import using local import

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.

  1. The app starts from app.py
  2. app variable inside the app.py file gets created in memory.
  3. db variable inside the app.py gets created in memory.
  4. Now, to import auth from controllers.py file we switch to ```controllers.py`` file
  5. We import Blueprint from flask.
  6. To import User, we switch to models.py file.
  7. Now, inside models.py file we import db (We are able to import it because it was created in step 3)
  8. And program continues so on and so on....

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?

  1. Our app starts from app.py file
  2. First, we need to import auth_controller from controllers.py file
  3. Let's visit the controllers.py file, and process it.
  4. From flask we import Blueprint
  5. Let's switch to models.py file to import User
  6. Inside 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!

Solved: Python: Cannot import name 'Transport', When I try to execute the command "from jsonrpclib import jsonrpc" I get the following error "ImportError: cannot import name 'Transport' ". ImportError: cannot import name 'train_test_split' from 'sklearn.linear_model. Hi@MD, I think in your version, linear_model don't have train_test_split

The error says that it is "most likely due to a circular import". Check to make sure that in the files you are importing you are not importing the file you showed. If you are that makes a circle of importing. for example my file is file.py and I am importing file2.py: import file > import file2 > import file > import file2 > import file... Make sure that is not happening. I had the same problem as you and this fixed it.

ImportError: cannot import name ?, Traceback (most recent call last): File "<input>", line 1, in <module> ImportError: cannot import name 'Troll'. the only thing that it did manage to� Sometimes while trying to install any package using pip, we get the the following import error: ImportError: cannot import name 'main' This mostly relates to the pip package manager .

The order of the imports matters:

Example:

# A.py
# empty file
# B.py
import A
# file1.py
import A
import B

change the order to:

# file1.py
import B
import A

TF2.2 ImportError: cannot import name 'dense_features' � Issue , When importing hooks_helper from official, see the following issue pop up. Traceback (most recent call last): File� Using python 3.7 from Anaconda, I got "ImportError: cannot import name 'quote' from 'pytube.compat'". Fixed by uninstalling pytube and installing pytube3 as indicated here: nficano/pytube#591 . Suggest changing the dependency to pytube3 as default.

I don't know the root cause, but it will work perfectly fine if you roll back to Python 3.7

ImportError: cannot import name , Hi Team, I tried to follow your doc: https://github.com/IBM/python-sdk-core/blob/ master/Authentication.md and got the following error. In [1]: from� ImportError: cannot import name 'export_saved_model' from 'tensorflow.python.keras.saving.saved_model' I have also tried @kari0219 's suggestion and also tried doing that with conda uninstall -y tensorflow

Workaround to: ImportError: cannot import name 'collections_abc , On December 11, 2019, a new version (1.23.0) of the google-cloud-bigquery library was released, which (in certain environments) breaks flake8 3.7.0+ fails with "ImportError: cannot import name 'Type'" in python 3.5.0 and 3.5.1 $ pip install flake8 I can't run flake8 --bug-report since I can't run it at all.

ImportError: cannot import name UNALTERABLE_COUNTRIES, But because of this error we can't view the code in Edit Form View and many other problems ImportError: cannot import name UNALTERABLE_COUNTRIES. The Python documentation also says that it is advisable to use import X, instead of other statements, such as from module import *, or from module import a,b,c. You may also see many code-bases using deferred importing even if there isn't a circular dependency, which speeds up the startup time, so this is not considered bad practice at all

ImportError: cannot import name 'appdirs', Hi Guys, I am trying to install tweepy module in my Linux system, but it is showing me the import name 'appdirs' How can I solve this error?

Comments
  • Could it be that you have another module (or script) named mysql in your sys.path that imports either select or connection ? In all cases, there's more to it than what you posted (I just tried your code example and it doesn't exhibit this issue), so please post a proper minimal reproducible example