AttributeError: Partially Initialized Module Has No Attribute – How To Fix It?

AttributeError: partially initialized module has no attribute

If you are getting trouble with the “AttributeError: partially initialized module has no attribute” error in Python, do not worry. Today, our article will give a few solutions and detailed explanations to handle the problem. Keep on reading to get your answer.

Why does the error “AttributeError: partially initialized module has no attribute” occur?

If you are a newbie to Python programming language, you may get the AttributeError. This is a common error that occurs when you try to access a non-existent attribute of an object.

Especially, the error “AttributeError: partially initialized module has no attribute” turns up because of the following problems:

Current file’s name is the same as the imported module.

Suppose you named your file datetime.py. And in this file, you used the datetime module to get the current time. You will certainly get the error. Look at our example below.

Code: 

# Import module named datetime to get the current time
import datetime as dt
 
now = dt.datetime.now()
 
print("Now is", now)
print("It's time to study")
print("Welcome to Learn Share IT")

Result:

Traceback (most recent call last):
  line 2, in <module>
    import datetime as dt
  line 4, in <module>
    now = dt.datetime.now()
AttributeError: partially initialized module 'datetime' has no attribute 'datetime' (most likely due to a circular import)

Two file import each other

Sometimes, you create two modules and want them to share their attributes. So you will import the second module to the first one and also import the first one to the second one. This is called Circular Import. As a result, you will get the error.

For example, you have a module named infor.py that stores your name and a module named greet.py with a function to print out Hello World. Now, if you call your name from module infor.py you will get the error immediately.

Module infor.py and greet.py, respectively.

# Module infor.py
import greet
 
myName = "Learn Share It"
# Module greet.py
import infor
 
def sayGreet():
    print("Hello World")

name = infor.myName
 
print("My name is", name)
sayGreet()

Result:

Traceback (most recent call last):
  line 1, in <module>
    import infor
  line 1, in <module>
    import greet
  line 6, in <module>
    name = infor.myName
AttributeError: partially initialized module 'infor' has no attribute 'myName' (most likely due to a circular import)

Going on. Now, we provide you with solutions to these issues.

Solutions to the problems

Change the current file’s name so as to be different from the imported package

To solve this problem, you need to change the file’s name. For example, we will change our file’s name from datetime.py to get_date_time.py and see the result:

Code:

# Import module named datetime to get the current time
import datetime as dt
 
now = dt.datetime.now()
 
print("Now is", now)
print("It's time to study")
print("Welcome to Learn Share IT")

Result:

Now is 2022-10-04 08:49:34.406122
It's time to study
Welcome to Learn Share IT

Merge two modules

Understandly, because you want two modules to have the same attributes, we import them mutually. In this case, it causes errors. To keep the purpose and have no errors, you can easily merge them into a new one as a solution.

Code:

def sayGreet():
    print("Hello World")

myName = "Learn Share It"
print("My name is", myName)
sayGreet()

Result:

My name is Learn Share It
Hello World

Summary

Our article has shown you the root of the error “AttributeError: partially initialized module has no attribute” and detailed solutions. We hope that through our examples, you understand the problem to avoid it in the future.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *