Search code examples
pythonpython-importattributeerror

Python attribute error when calling a function from another file


Currently I am having problems with calling functions from another file in pycharm, I originally also had this problem in visual studio. I have two files one called Main and the other Database, when I am in my Main file and import Database and run a function from database I am getting this attribute error. Database also needs to run the start function after finishing so I imported Start into the Database file and ran the function in there, when running I get this attribute error. However when removing one of these functions the program works flawlessly. This is confusing since even in intellisense it picks up the functions after being imported. Below I will post any code to help visualize the situation and post the error code, thanks for reading! Main File:

import Database as DB


def Start():
    print("\n---------------\n")
    print("1: Database\n")
    print("2: TensorFlowTest\n")
    print("3: Quit Program\n")
    print("---------------\n")

    x = int(input("What would you like to open?\n"))

    if (x == 1):
        DB.dataMain()


Start()

Database File:

import Main as main


def dataMain():
    print("\n---------------\n")
    print("Gathering all files...")
    if (len(all_filenames) != 0):
        dataAppend()
        dataSort()
    else:
        print("No Additional Data added to database")
        dataSort()
    print("Completed\n")
    print("---------------")
    main.Start()

This is the code error in console: Error Code in console

One last thing both files are in the same directory which is the project directory

Im sorry for such a long description, and a confusing one! but thankyou for reading and posting any suggestions to fix this problem! thankyou!


Solution

  • In the end I imported the function I needed where I was calling the function, seemed to fix the issue. Not sure if its the best way of going about things but it works fine for the moment.

    Database Example Code:

    def dataMain():
        print("\n---------------\n")
        print("Gathering all files...")
        if (len(all_filenames) != 0):
            dataAppend()
            dataSort()
        else:
            print("No Additional Data added to database")
            dataSort()
        print("Completed\n")
        print("---------------")
        import Main as main
        main.Start()