Skip to content Skip to sidebar Skip to footer

Python Spyder Debug Freezes With Circular Importing

I have a problem with the debugger when some modules in my code call each other. Practical example: A file dog.py contains the following code: import cat print('Dog') The file cat

Solution 1:

When I run dog.py (or cat.py) I don't have any problem and the program runs smoothly.

AFAICT that's mostly because a script is imported under the special name ("__main__"), while a module is imported under it's own name (here "dog" or "cat"). NB : the only difference between a script and a module is actually loaded - passed an argument to the python runtime (python dog.py) or imported from a script or any module with an import statement.

(Actually circular imports issues are a bit more complicated than what I describe above, but I'll leave this to someone more knowledgeable.)

To make a long story short: except for this particular use case (which is actually more of a side effect), Python does not support circular imports. If you have functions (classes, whatever) shared by other scripts or modules, put these functions in a different module. Or if you find out that two modules really depends on each other, you may just want to regroup them into a single module (or regroup the parts that depend on each other in a same module and everything else in one or more other modules).

Also: unless it's a trivial one-shot util or something that only depends on the stdlib, your script's content is often better reduced to a main function parsing command-line arguments / reading config files / whatever, importing the required modules and starting the effective process.

Post a Comment for "Python Spyder Debug Freezes With Circular Importing"