简体   繁体   中英

Module has no attribute error in python3

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?

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

Your package name (io) conflicts with the Python library's package with the same name, so you actually import a system package.

You can check this by printing io.__all__ .

Changing io.py to something else is probably the best way to go to avoid similar problems. Otherwise, you can use an absolute path.

try

from io import IO

That worked for me when trying to import classes from another file

this has more information:

Python module import - why are components only available when explicitly imported?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM