Import Python wildcards from import names

Well, I have some rather strange behavior in one of my projects, and I hope someone will tell me why. My file structure is as follows:

MainApp.py res/ __init__.py elements/ __init__.py MainFrame.py 

Inside MainFrame.py, I defined a class called RPMWindow that extends wx.Frame.

In MainApp.py, this works:

 from res.elements.MainFrame import * 

And this is not so:

 from res.elements.MainFrame import RPMWindow 

I understand that importing a wild card doesn’t hurt anything, but I'm more interested in why named import fails when a remote card succeeds.

When using the class name, I get this trace:

 Traceback (most recent call last): File "C:\myApps\eclipse\plugins\org.python.pydev.debug_1.5.6.2010033101\pysrc\pydevd.py", line 953, in <module> debugger.run(setup['file'], None, None) File "C:\myApps\eclipse\plugins\org.python.pydev.debug_1.5.6.2010033101\pysrc\pydevd.py", line 780, in run execfile(file, globals, locals) #execute the script File "C:\Documents and Settings\Daniel\workspace\RPM UI - V2\src\MainApp.py", line 2, in <module> from res.elements.MainFrame import RPMWindow File "C:\Documents and Settings\Daniel\workspace\RPM UI - V2\src\res\elements\MainFrame.py", line 2, in <module> from res.elements.MenuBar import MenuBarBuilder File "C:\Documents and Settings\Daniel\workspace\RPM UI - V2\src\res\elements\MenuBar.py", line 2, in <module> from MainApp import _, DataCache File "C:\Documents and Settings\Daniel\workspace\RPM UI - V2\src\MainApp.py", line 2, in <module> from res.elements.MainFrame import RPMWindow ImportError: cannot import name RPMWindow 

When using wild card import, I do not get a trace and my application opens.

+4
source share
3 answers

Do you have round import:

MainFrame.py indirectly imports MainApp.py, and MainApp.py imports MainFrame.py. As a result, when MainApp.py imports MainFrame.py, the RPMWindow class is not yet defined, and you get an ImportError.

+8
source

I don’t have time to find out why the template works for you, but what can I say about your error when importing a name directly, is that you have an import loop in your code:

you're trying to import res.elements.MainFrame , but part of this code is trying to import res.elements.MenuBar , which is trying to import res.elements.MainFrame again. IOW, your first attempt to import res.elements.MainFrame has not yet completed before res.elements.MainFrame .

0
source

You have cyclic import in your code: the same module is necessary and requires the use of some other module, which, when you think about it, is clearly unstable. Most problems can be fixed by using import a , and then referring to ab instead of from a import b or from a import * .

In particular, never use from a import * . Importing wildcards will interfere with your namespace and make your code less maintainable, readable, normal, and predictable. The difference between import a and from a import * is the difference between dragging a box into a room and pouring its contents all over the floor.

It would be better if you could move the generic code into your own module or somehow reorganize the need for looping imports. Circular imports always indicate a design problem.

0
source

Source: https://habr.com/ru/post/1307370/


All Articles