AttributeError: partially initialized module has no attribute

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
4 min

banner

# AttributeError: partially initialized module has no attribute

The Python "AttributeError: partially initialized module has no attribute" occurs for two main reasons:

  1. Having a circular dependency between files, e.g. file A imports file B and vice versa.
  2. Having a local module with the same name as an imported module, e.g. requests.py.

partially initialized module has no attribute

# Naming a local module with the same name as an imported module

Here is an example of how the error occurs in a file called requests.py.

requests.py
import requests def make_request(): # โ›”๏ธ AttributeError: partially initialized module 'requests' # has no attribute 'get' (most likely due to a circular import) res = requests.get('https://reqres.in/api/users') parsed = res.json() print(parsed) make_request()

Notice that I am importing the requests third-party module in a local file called requests.py.

Having my local file named the same as the module I am importing is causing a circular import.

The Python interpreter first looks for the imported module in the built-in modules, then in the current directory, then in the PYTHON PATH, then in the installation-dependent default directory.

So, when we create a local file with the same name as that of a third-party module, we effectively shadow the official module with our local file.

# Make sure you haven't named a local module with a name of a remote module

Make sure you haven't named your local modules with names of remote modules, e.g. requests.py or datetime.py and remove any circular dependencies in import statements.

A good way to start debugging is to print(dir(your_module)) and see what attributes the imported module has.

main.py
import requests # ['__builtins__', '__cached__', '__doc__', '__file__', # '__loader__', '__name__', '__package__', '__spec__', 'requests'] print(dir(requests))

If we look at the attributes the imported requests module has, we don't see any of the actual methods the official requests module provides.

This is a clear indication, that we are shadowing the third-party module with our local module.

This can also happen when you give a module the same name as a standard-library module, e.g. datetime.

You can use the sys module to print all of the built-in module names if you ever wonder if your local modules are clashing with built-in ones.

main.py
import sys # ๐Ÿ‘‡๏ธ print all built-in module names print(sys.builtin_module_names)

The error is also caused if one of the modules you are importing imports a module that has the same name as a local file in your project.

For example, if you import requests and requests imports datetime, but you have a local file called datetime.py, you would still get the error.

# Having circular imports between files

Another common cause of the error is having circular imports between files.

Let's look at an example that uses the modules first_module.py and second_module.py.

Here is the code for first_module.py:

first_module.py
# ๐Ÿ‘‡๏ธ Imports second_module import second_module def first_function(): print('first function') # โ›”๏ธ AttributeError: partially initialized module 'second_module' # has no attribute 'second_function' (most likely due to a circular import) second_module.second_function()

And here is the code for second_module.py:

second_module.py
# ๐Ÿ‘‡๏ธ Imports first_module import first_module def second_function(): print('second function') first_module.first_function()

Notice that the two modules import each other. This is called a circular dependency.

One way to get around two modules depending on one another is to nest the import statement in a function scope.

Here is the updated second_module.py file.

second_module.py
def second_function(): print('second function') # ๐Ÿ‘‡๏ธ Now importing in a function scope import first_module first_module.first_function()
Now that we don't import at the outermost scope in both modules, the order of imports does not cause the interpreter to error out.

# Creating a third file to deal with circular imports

However, a much better approach is to create a file third_module.py which imports first_module and second_module and uses them.

Here is the updated code for first_module.py.

first_module.py
def first_function(): print('first function')

Here is the updated code for second_module.py.

second_module.py
def second_function(): print('second function')

And here is the code for third_module.py which makes use of both of the previous modules.

third_module.py
import first_module import second_module first_module.first_function() second_module.second_function()

Now we don't have any circular imports (importing members between the same modules), which makes our code much easier to reason about.

If you have two modules that import each other, it's a good time to create a third module that imports the previous two.

If none of the suggestions helped, use the dir() function to print all of the attributes the imported module has.

requests.py
import requests # ๐Ÿ‘‡๏ธ ['__builtins__', '__cached__', '__doc__', '__file__', # '__loader__', '__name__', '__package__', '__spec__', 'requests'] print(dir(requests))

If you pass a module object to the dir() function, it returns a list of names of the module's attributes.

If you try to access any attribute that is not in this list, you will get an error.

This means that you are either trying to access an attribute that is not present on the module, or you have an incorrect import statement.

# Conclusion

To solve the Python "AttributeError: partially initialized module has no attribute", make sure you haven't named your local modules with names of remote modules, e.g. requests.py or datetime.py and remove any circular dependencies in import statements.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev