-2

I tried searching around but didn't seem to find an answer to my problem, so I'm sorry if I missed something and it actually has been answered before.

So basically I have main.py and another file called check.py (both in same directory)

In my main.py I have:

from check import checkfunction

I have a small function inside main.py that I MUST call inside check.py, but I can't seem to get this import working on my check.py:

from main import mainfunction

How can I get the mainfunction to work inside check.py?

Thanks!

4

2 回答 2

1

You've got a design with a circular dependency which is usually a bad thing as your two python modules are tightly coupled.

Consider refactoring your code. But if you must stick with your design please see the following SO question for more info on how circular imports work in Python and the various gotchas to look out for.

于 2013-10-12T09:24:48.430 回答
0

Several options:

  • Move the common function to a module imported by both other modules.
  • Merge both modules into one.
  • Pass the function from main to the code that needs to call it.
  • Monkey patch the function into the check module after importing it.
  • Refactor the whole thing so that you don't have circular dependencies.

If you actually explained why you have this design, someone could possibly propose a better way.

于 2013-10-12T09:18:35.397 回答