0

It's pretty well-known (and documented in PEP8) that import statements should be placed at the top of a Python module, which is what I usually do. However, in my current case, I need to import a module (call it module_a) that gets created when another module (call it module_b) gets run, so it won't work to have import module_a at the top of module_b (which needs to create module_a before importing it).

The solution that I have implemented to solve this is to import module_a inside of a function, after declaring global module_a. As far as I can tell, this works as I want it to.

# In module_b
def create_module_a():
    # Creates module_a.py

def import_module_a():
    global module_a
    import module_a

Part 1 of my question, then, is: Is there a way to do what I want to do and not have an import in the function? Is there a way to adhere to PEP8? One potential solution that I've thought of so far is to have another module, module_c, create module_a before module_b is run and imports module_a... but I'm not even sure that this would even work in my situation, and it would most likely complicate things, at least in the short term.

Part 2 of my question has to do with this statement found in the Python documentation:

Names listed in a global statement must not be defined as formal parameters or in a for loop control target, class definition, function definition, or import statement.

Does the last part of this statement mean that a global name must not be imported, or does it mean that one must not do something like import some_module as module_a, where module_a is declared global?

If the former is true, then what is an alternative to having import_module_a() in module_b?

theo1010
  • 127
  • 10
  • 2
    PEP-8 is just a convention. Don't follow it blindly if it prevents the correct functioning of your code. Call `create_module_a()`, then import the module. – chepner Jul 22 '17 at 00:44
  • @chepner thank you for your comment. I totally agree with you that "correct functioning" should take priority over convention, but I was a bit wary of taking what might be considered an "anti-PEP8" stance. I'm glad that you helped alleviate my doubts. – theo1010 Jul 22 '17 at 00:53
  • @chepner Another thing: Part 2 of my question also deals with convention, but there's an interesting statement in the Python documentation (see above for cited link), right after the quote that I cited above, that reads: "The current implementation does not enforce the latter two restrictions, but programs should not abuse this freedom, as future implementations may enforce them or silently change the meaning of the program." So, beyond convention, is there a potential "danger" to having `global module_a` followed by `import module_a`? It seems odd to me that that could cause problems. – theo1010 Jul 22 '17 at 00:56
  • 1
    Why do you think you need the statement `global module_a`? – Stephen Rauch Jul 22 '17 at 02:26
  • @StephenRauch so that `module_a` can be accessed in other parts of `module_b`. (Edit for clarification: without `global`, `module_a` would be local to the function `import_module_a()`) See the answer in [this SO question](https://stackoverflow.com/questions/11990556/python-how-to-make-global-imports-from-a-function). Also, note that the first comment on the accepted answer relates directly to **Part 2** of my question. – theo1010 Jul 22 '17 at 16:03

0 Answers0