1

I have a flask application, and I have split the code into multiple python files, each dedicated to a specific portion of the overall program (db, login, admin, etc). Most of the files have some sort of setup code that creates their respective module's object, which must have access to the Flask object defined in my main file. Some of the modules also need access to variables in other modules, but when importing them, they are run again, even though they were imported in main already.

The following code is an example of the problem.

If I have a main.py like this

import foo
import bar
if __name__ == "__main__":
    foo.foofunc()

foo.py

import bar
@bar.barable
def foo(string):
    print(string)

and bar.py

import foo
foo.foo("hello")
def barable(fun):
    def r(*args, **kwargs):
        print("this function is completely unbarable")
        func(*args, **kwargs)

This code doesn't work because foo imports bar, which imports foo, which runs bar.barable, which hasn't been defined yet.

In this situation (assuming that calling foo.foo is necessary), is my only option to extract bar.barable out of bar and into a seperate module, or is there some other way to fix this?

I know that importing a module in python runs the file, but is there some way to put some of the code into the same sort of check as __name__ == "__main__" but to check if it is being imported by main and not by another module?

Mrab Ezreb
  • 440
  • 5
  • 15
  • Importing a file a second time, does not *run* it again. – Stephen Rauch May 27 '18 at 00:36
  • Sorry, my edit explains the issue. – Mrab Ezreb May 27 '18 at 00:36
  • This code doesn't work because foo imports bar, which imports foo, which runs bar.barable, which hasn't been defined yet. – Mrab Ezreb May 27 '18 at 00:36
  • Have you tried doing the import at the bottom of the file? – Stephen Rauch May 27 '18 at 00:38
  • Which one of the imports? – Mrab Ezreb May 27 '18 at 00:39
  • Seems like you are doing some kind of cyclic import. Have you read [this](https://stackoverflow.com/questions/744373/circular-or-cyclic-imports-in-python)? It might help shine some light on what you are trying to do. Personally, I try to avoid these kind of importing patterns and instead try to work out the structure to import in a simpler fashion, even if it means having to create a few more files and abstracting a few things. – idjaw May 27 '18 at 00:46
  • 1
    You need to get rid of the circular imports and adding modules for common code is a good way to do that. So, I'd say yes, one or more of the descriptors should move. You are doing `foo.foo()` outside of a function which is generally frowned upon (mere import shouldn't run a lot of code). If that can go inside another function then that function can do the `import foo` and remove the problem. – tdelaney May 27 '18 at 00:47
  • So I should have some sort of setup method that main calls after importing all of the files, rather than the files running code when imported? – Mrab Ezreb May 27 '18 at 00:49
  • 1
    I think you need to go a step deeper and solve the foo importing bar and bar importing foo. You should not have that circular dependency. Solve that problem first. When you solve that, it should pretty much solve itself. – idjaw May 27 '18 at 00:52

0 Answers0