Search code examples
pythonimporterror

Import Error: Cannot import function after clearly declaring in the __init__.py


Say, I have three files __init__.py, time.py and readers.py in a directory analogpy.

time.py has a class and readers.py has function read_file()

__init__.py has following

from analogpy import time
from analogpy import readers

when I import readers from analogpy and call the function read_file(), it shows the following error

from analogpy import readers
ImportError: cannot import name 'read_file' from 'analogpy' (analogpy/__init__.py)

Functions are clearly defined because I see they work fine when compiling individual files. What am I not understanding here?


Solution

    1. you can update your __init__.py like this:
    from .readers import read_file
    

    then you can from analogpy import readfile in other file and call the function

    1. or you can use
    from analogpy import readers
    
    readers.read_file()
    

    to call the function