Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I receive an AttributeError even though import, spelling and file location is correct?

  1. I am using PyCharm
  2. All files are in the directory 'venv'

    • venv
    • NoteFunction.py
    • NoteMainApp.py
    • ...

I split up my code in five separate files. One 'main' file, gathering all other files and creating eventually the GUI. The prefix for the files is 'Note' followed an appropriate description.

My problem now is the import of 'NoteTopMenu' into the main file 'NoteMainApp'. The code is:

import NoteStatusbar as SB
import NoteTopMenu as TM
import NoteWidgets as NW
import tkinter as tk


class MainApp(tk.Frame):

    def __init__(self, parent):

        tk.Frame.__init__(self,parent)
        super().__init__(parent)
        self.topbar = TM.TopMenu(parent)
        self.widget = NW.FrontFrames(parent)
        self.statusbar = SB.StatusBar(parent)


root = tk.Tk()
MainApp(root).pack(side="top", fill="both")

root.mainloop()

I receive the error message:

Traceback (most recent call last):
  File "C:/Users/PycharmProjects/MindNotez/NoteMainApp.py", line 2, in <module>
    import NoteTopMenu as TM
  File "C:\Users\PycharmProjects\MindNotez\NoteTopMenu.py", line 2, in <module>
    import NoteMainApp as Main
  File "C:\Users\PycharmProjects\MindNotez\NoteMainApp.py", line 29, in <module>
    MainApp(root).pack(side="top", fill="both")
  File "C:\Users\PycharmProjects\MindNotez\NoteMainApp.py", line 13, in __init__
    self.topbar = TM.TopMenu(parent)

AttributeError: module 'NoteTopMenu' has no attribute 'TopMenu'

The code in NoteTopMenu is:

import NoteMainApp as Main
import NoteWidgets as NW
import tkinter as tk


class TopMenu(NW.FrontFrames):
    """Class creating the top menu bar."""
    def __init__(self, master):
        super().__init__(master)
        # *******Top-Navigation Bar (tnb)**********
        tnb = tk.Menu(master)
        Main.root.config(menu=tnb)
        ....

If I comment the NoteTopMenu out in the main file, the code runs without a problem. I checked my spelling but PyCharm also offers me code-completion. Therefore, PyCharm finds the file, the module, my class and other module are imported without an issue. Do you know why the file/module is not being found or fails to be imported?

Full code is here on GitHub: MindNotez

Thank you very much for your help!

like image 558
Alex_P Avatar asked Nov 15 '18 09:11

Alex_P


1 Answers

You invoke NoteMainApp.py which imports NoteTopMenu.py which imports NoteMainApp.py which does not go on to re-import NoteTopMenu.py (because the import has already started). The import of NoteMainApp.py then goes on to parse the rest of the file. At this point the module NoteTopMenu is defined, but it doesn't have any attributes (because you haven't got round to defining them yet) ... hence the error.

I suggest that NoteTopMenu.py should not import NoteMainApp.py (and if there are any bits which both files need, they should be moved into another file that both can import)

like image 125
Martin Bonner supports Monica Avatar answered Nov 08 '22 11:11

Martin Bonner supports Monica