Float07
Float07

Reputation: 368

Error with wxPython - partially initialized module 'wx' has no attribute 'Frame' (most likely due to a circular import)

I'm following a small Python program example using wxPython, as follows:

import wx

class MyFrame(wx.Frame):    
    def __init__(self):
        super().__init__(parent=None, title='Hello World')
        panel = wx.Panel(self)

        self.text_ctrl = wx.TextCtrl(panel, pos=(5, 5))
        my_btn = wx.Button(panel, label='Press Me', pos=(5, 55))

        self.Show()

if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame()
    app.MainLoop()

But it returns an error:

Traceback (most recent call last):
  File "/home/float/.local/lib/python3.8/site-packages/wx/__init__.py", line 1, in <module>
    import wx
  File "/home/float/.local/lib/python3.8/site-packages/wx/__init__.py", line 3, in <module>
    class MyFrame(wx.Frame):    
AttributeError: partially initialized module 'wx' has no attribute 'Frame' (most likely due to a circular import)
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 72, in apport_excepthook
    from apport.fileutils import likely_packaged, get_recent_crashes
  File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
    from apport.report import Report
  File "/usr/lib/python3/dist-packages/apport/report.py", line 15, in <module>
    import xml.dom, xml.dom.minidom
  File "/home/float/.local/lib/python3.8/site-packages/wx/xml.py", line 13, in <module>
    from ._xml import *
ImportError: attempted relative import with no known parent package

Original exception was:
Traceback (most recent call last):
  File "/home/float/.local/lib/python3.8/site-packages/wx/__init__.py", line 1, in <module>
    import wx
  File "/home/float/.local/lib/python3.8/site-packages/wx/__init__.py", line 3, in <module>
    class MyFrame(wx.Frame):    
AttributeError: partially initialized module 'wx' has no attribute 'Frame' (most likely due to a circular import)

I can't find any "circular import" in my program, so I'm assuming this error comes from the wxPython lib. Is this assumption correct? And how can I fix that?

I'm running this on WSL2 (Ubuntu 20)

edit: Problem was solved by runnin "pip3 install --upgrade --force-reinstall wxPython", but I would like to know why that happened in the first place if possible. thanks.

Upvotes: 1

Views: 1766

Answers (1)

Thomas Willmann
Thomas Willmann

Reputation: 61

Is your own module, by any chance, called "wx.py"? I got the same error message because of this. Solution: Simply rename your module!

Upvotes: 6

Related Questions