Last updated: Apr 8, 2024
Reading timeยท6 min
The error "ImportError: cannot import name X" occurs for multiple reasons:
sys.py
and then importing from the sys
module.The error often occurs when we have circular imports (importing members between the same files).
To solve the error, move the functions or classes to a third file and import them from a central location in other files.
Here is an example of having circular imports (importing members between the same files).
This file is called main.py
and imports a function from another.py
.
from another import do_multiplication # ๐๏ธ Imports from another.py def do_addition(a, b): return a + b print(do_multiplication(5, 5))
And here is the code for another.py
.
from main import do_addition # ๐๏ธ Imports from main.py def do_multiplication(a, b): return a * b # โ๏ธ ImportError: cannot import name 'do_multiplication' from partially initialized module 'another' (most likely due to a circular import) (/home/borislav/Desktop/bobbyhadz_python/another.py) print(do_addition(5, 5))
We are importing members between the two files, so they depend on one another.
This is very confusing for the Python interpreter and should be avoided.
Here is the updated code for third.py
.
def do_addition(a, b): return a + b def do_multiplication(a, b): return a * b
And now we are able to import the two functions in any other file without having circular imports.
from third import do_addition, do_multiplication print(do_addition(5, 5)) # ๐๏ธ 10 print(do_multiplication(5, 5)) # ๐๏ธ 25
The another.py
file can also import the functions from third.py
without any
issues.
Now the interpreter won't get confused when running our code because we don't have imports between the same files.
a.py
and b.py
, move the objects in a file called c.py
and import the objects from c.py
to avoid any circular imports.If the error persists, try to import the entire module, instead of importing a specific member from the module.
import math # ['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'cbrt', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'exp2', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'lcm', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'nextafter', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc', 'ulp'] print(dir(math))
You can use the dir()
function to print all of the attributes of the module
and check if the member you are trying to import exists in the module.
You can also use your IDE to try to autocomplete when accessing specific members.
If autocomplete doesn't automatically start, try pressing CTRL + Space
on your
keyboard.
If your IDE can't help you with autocomplete, the member you are trying to access likely doesn't exist in the module.
You can also try to autocomplete directly when importing the module.
For example, if you start typing from numpy import
and press CTRL + Space
to start autocomplete, your IDE should come up with suggestions for valid
imports.
from numy import <press CTRL + Space>
Carefully check your spelling as the names of functions, classes and modules are case-sensitive.
If you misspell the name of the function or class you're trying to import, the error occurs.
For example, the following import statement causes the error.
# ImportError: cannot import name 'Array' from 'numpy' (/home/borislav/Desktop/bobbyhadz_python/venv/lib/python3.11/site-packages/numpy/__init__.py) from numpy import Array print(Array)
The error is caused because I tried to import Array
(uppercase A
) from
numpy
and the function is actually named array
(lowercase a
).
from numpy import array # โ Works print(array)
Correcting the spelling error resolves the issue.
You might be trying to import a function or class from a module that doesn't export it.
The error is caused when:
The same could be the case for your specific version of Python.
A standard module might not contain the function you are trying to import because it is added in a later version or has been removed.
You can try to google around to see if the package you are importing from contains the function or class you are trying to import.
You can check your Python version with the python --version
command if
importing built-in modules.
python --version
Or you can check your version of a specific package with the
pip show package-name
command if importing from third-party modules.
pip show numpy
Make sure to replace numpy
with the name of the package you are importing
from.
Make sure you haven't named a file in your project with the same name as the
module you are trying to import from, e.g. numpy.py
.
This would shadow the module you are trying to import from and is often a cause of the error.
Notice that there is a local file named numpy.py
in the example.
When I try to import from the numpy
module, the error is raised because Python
only finds my local numpy
file.
To resolve the error, rename your local file to not shadow the remote, third-party module.
For example, if you have a local file named example.py
, you won't be able to
import from the example
module because the Python interpreter will only be
able to find your own example.py
file when resolving the path.
If the error persists and you use Jupyter Notebook, try restarting the kernel.
To restart the kernel:
p
in command mode (you shouldn't be editing a cell when you press
p
).To solve the "ImportError: cannot import name X" error, make sure:
sys.py
.