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

Alex_P
  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!

Martin Bonner supports Monica

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

CMake cannot find source file, even though it is in the correct location

Why do I get a not declared in this scope error even though the header file is included?

Why do I need "Unblock-File" even though execution policy is RemoteSigned?

Why do I get a version mismatch during composer install for an outdated php version even though correct php version is installed?

Python: No such file or directory even though file is correct

Why do I have a deadlock even though it includes an endless loop?

why do I get a locale error even though it is set?

Why do I get a WrongMethodTypeException on invokeExact even though MethodHandle is OK

Why do I keep getting TypeError even though input is an interger?

Docker wants a requirement file even though I do not want it to?

I am trying to import my database values, but the results are not being displayed, even though the table structure is correct

File not found, even though the directory is correct

FXMLLoader raises "Location is not set", even though file exists on location

Why is app.use() not serving up the 'public' directory when I save even though the path is appears to be correct?

Why does Git want to correct my line endings to CRLF, even though I want them to be in LF?

Why can't I get the correct captcha even though the output is the same as the user input?

Xcode cannot symbolicate my crash logs even though I have correct .dSYM file

python file not found exception even though I have given the correct directory

Why can't I import opencv3 even though the package is installed?

Cannot import modules even though the are in the same directory as the file I am working in

Getting a import module not found error even though it is there when I do pip3 list?

Why can I not hardlink to a file I don't own even though I can move it?

Why can't I delete a file even though I have write permissions?

When reading huge HDF5 file with "pandas.read_hdf() ", why do I still get MemoryError even though I read in chunks by specifying chunksize?

Python class attributeError, even though I have that attribute

Why do I get a StringOutOfBoundsException even though I am within bounds of the String array?

Why do I get permission denied even though I am part of the owner group?

Why do I get IP from romania even though I am located in germany?

Why do I get "Module 'myControllers' is not available" in AngularJS even though I've already define it?