ImportError – Cannot Import Name X in Python

ImportError occurs when Python is unable to load an attribute (class, method, or variable) from a module. The following are common reasons why that happens:

  • The imported attribute does not exist or is misspelled,
  • There is a circular dependency between the script being executed and the module is loaded,
  • The imported attribute is not available in the Python library of packages, and,
  • Naming scripts after installed modules

In the coming sections, we will discuss these causes and provide a solution for each case.

The imported attribute does not exist or is misspelled

In this case, the function/class/variable we are trying to load does not exist. This can arise when we misspell the attribute we intend to import, or it is just not created. For example, suppose we have two scripts,, and, in the same folder. We intend to run to call some functions in

In the script, we intend to import and use the discounted_price() function from


Output (running

ImportError: cannot import name 'discount_price' from 'math2'

We misspelled the function, hence the error.


To fix the ImportError of this kind, we need to ensure that the attribute(s) we are importing exists. In the case above, we needed to import the discounted_price() function, not discount_price, as follows




Remember that we can also import the module and access its attributes as follows.

Or, import all the attributes in the math2 module and use them as follows


Marked Price: 645
Discounted price: 579.3948290819244
The discount amount: 65.60517091807564

The imported attribute is not available in the Python library

The ImportError resulting here is the same as discussed in the first section. The only difference is that the library being loaded here is not user-defined (it is Python packages that come preinstalled or installed by pip).

For example, suppose we want to convert a Python list into a NumPy array using the NumPy package.


ImportError: cannot import name 'arr' from 'numpy'

NumPy module does not have a function arr and therefore cannot be imported. What we are looking for, instead, is the “array” function.

By importing the correct method, the error is fixed. As said earlier, you can also load the module and access the attribute it contains as follows. If the module is unavailable, as shown below, we run into another class of error AttributeError.


AttributeError: module 'numpy' has no attribute 'arr'

The error explains it well; the “arr” attribute is not in numpy.

Circular Dependency

Circular dependency imports occur when two scripts are interdependent – the script being executed (script A) imports attribute(s) from the second script (script B), which also calls some attributes on the script being executed (script A).

Figure 1: A circular dependency import (Source: Author).

This circular dependency occurs because the module loading process cannot be completed. To understand why this is so, let us see an example.



When we execute the script, we run into ImportError with the description below:

ImportError: cannot import name ‘acceleration’ from partially initialized module ‘physics1’ (most likely due to a circular import)

Python detected a circular import and stopped the execution. The attempt to import modules in this manner can run indefinitely because the module will be partially initialized at every import — because of using from A import attr1.

The script tries to import acceleration() and mc() in physics1, and even before that is done, also tries to import an attribute in

There are different ways to solve this. We will discuss two in this article.

Solution 1: Defer the importation

When we run, we import two functions: acceleration and mc. Circular dependency arises because before these methods are imported, is also trying to import from To fix this problem, we can import functions on after importing the two functions.



Output (when we execute


Note: When attributes are imported, the entire module is scanned. That is why the acceleration() and mc() function prints the output on executing and also on the import line in (from math1 import multiply).

Solution 2: Transfer attributes to a third file

This is probably the best way to deal with circular imports because it gives us a clean code (according to PEP standards it is a good idea to keep imports at the top of the file), and it is straightforward and also efficient.

In this case, we will take all the functions into a script named “” and call them from that central point.





Naming scripts after installed modules

When naming Python scripts, we should avoid giving them names matching the installed packages. This is because if we try importing a user-defined module with a name similar to an installed package Python will throw an ImportError. Let’s see an example.

In this example, we have two scripts named and in the same directory.



On executing, we get the following error message:

ImportError: cannot import name 'add2' from 'math' (unknown location)

When importing modules, Python looks for these packages in specific places – among them is the current working directory and the default package location created during Python installation.

In our current directory, we have a user-defined module named math when we are executing There exists also a preinstalled package called math. In this case, therefore, Python loads the preinstalled math and does not see the script in our current working directory.

The error in this category can be solved by simply avoiding the use of names matching the installed packages for our user-defined modules.


ImportError occurs because Python is unable to import the attributes we are trying to load. This can happen because of several reasons.

Firstly, the imported attribute might not exist or has been misspelled. In this case, you need to countercheck the names of the attributes you are trying to import.

Secondly, we can run into circular dependence during importation. This happens when two scripts are trying to import each other.

We discussed two solutions for this problem and alluded that the best one is to move functions into a single file and import them from there.

Lastly, we covered an ImportError related to how we name our user-defined modules. If we give a name that matches the installed package to our user-defined modules, Python may default to incorrectly loading system-wide packages.