Skip to content Skip to sidebar Skip to footer

Python: Calling A Function From A File That Has Current File Imported

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 a

Solution 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.

Solution 2:

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.

Post a Comment for "Python: Calling A Function From A File That Has Current File Imported"