Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module has no attribute error in python3

Tags:

python

class

contents of io.py

class IO:
    def __init__(self):
        self.ParsingFile = '../list'

    def Parser(self):
        f = open(ParsingFile, 'r')
        print(f.read())

contents of main.py

import sys
sys.path.insert(0, "lib/")

try:
    import io
except Exception:
    print("Could not import one or more libraries.")
    exit(1)

print("Libraries imported")
_io_ = io.IO()

When I run python3 main.py I get the following error:

Libraries imported
Traceback (most recent call last):
  File "main.py", line 11, in <module>
    _io_ = io.IO()
AttributeError: module 'io' has no attribute 'IO'

Any idea what's going wrong?

like image 463
theprogrammer Avatar asked Feb 25 '17 18:02

theprogrammer


People also ask

How do I fix No attribute error in Python?

Attribute errors in Python are raised when an invalid attribute is referenced. To solve these errors, first check that the attribute you are calling exists. Then, make sure the attribute is related to the object or data type with which you are working.

What does it mean when module has no attribute?

It's simply because there is no attribute with the name you called, for that Object. This means that you got the error when the "module" does not contain the method you are calling.

How does Python handle attribute errors?

To avoid the AttributeError in Python code, a check should be performed before referencing an attribute on an object to ensure that it exists. The Python help() function can be used to find out all attributes and methods related to the object. To resolve the AttributeError , a try-except block can be used.


1 Answers

My file was called io. It seems that there already exists a package called io which caused the confusion.

like image 144
theprogrammer Avatar answered Sep 22 '22 12:09

theprogrammer