0

My project contains only four modules in single directory. Everything worked fine before I tried to add additional setup menu. Now I get ImportError when class exists in this module:

python3 -m project
Traceback (most recent call last):
  File "/usr/lib64/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib64/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/project/__main__.py", line 1, in <module>
    from project.gui import Window
  File "/home/project/gui.py", line 4, in <module>
    from project.document import Document
  File "/home/project/document.py", line 3, in <module>
    from project.salary import Salary
  File "/home/project/salary.py", line 3, in <module>
    from project.gui import Setup
ImportError: cannot import name 'Setup' from 'project.gui' (/project/gui.py)

Here is structure (modules, imports, classes, data) of my project:

* document.py
    from project.salary import Salary

    class Record
    class Document

* gui.py
    from project.document import Document
    from project.predefined import VALUES

    class Gui(tk.Tk)
    class Window(Gui)
    class Setup(Gui)

* __main__.py
    from project.gui import Window

* salary.py
    from project.gui import Setup

    dataclass Record
    VALUES = (Records, )
    class Loader
    class Salary

* predefined.py
    dataclass Record
    VALUES = (Records, )

I don't understand why it is happening, everything seems to be allright for me. Could you explain me please this behaviour and how can I fix it?

Edit: I have changed structure of project to avoid circular import dependency as suggested in comments. Updated structure is up (dataclass Record and VALUES are temporary duplicated in salary.py and predefined.py). I still get the same error.

Ethr
  • 431
  • 1
  • 3
  • 17
  • Does this answer your question? [Python circular importing?](https://stackoverflow.com/questions/22187279/python-circular-importing) – FlyingTeller Feb 27 '20 at 14:51
  • @FlyingTeller @WGriffing Thank you for pointing me this out. It's first time when I hear about circular import dependency. I have tried to move `VALUES` to new module `predefined.py`, but still get `ImportError`. – Ethr Feb 27 '20 at 14:57

1 Answers1

1

You are using circular dependency which is causing this error. I think the steps in your program from the code you provided are as below:

  1. documents.py runs and hits 'import Salary'
  2. Compiler goes to salary.py to import Salary and in first line in hits:

    from project.gui import setup

  3. It goes to gui.py to import setup.

  4. in gui.py it will run

    from project.document import Document

Now as of step one, it is still waiting for Salary to be imported and has not built the Document class this prevents gui.py to run 'class SetUp' and therefore you are getting an error that it is not found.

The best solution is to avoid circular importing or break it by just using 'import name_of_module'. However, again, it is best to avoid this practice.

For future reference I suggest you read:https://stackabuse.com/python-circular-imports/

  • You are right. I edited my code once again and found loop I missed first time. Now everything is working, thank you for useful lecture. – Ethr Feb 27 '20 at 16:17