How to prevent __init__ from running in a directory?

Viewed 586

Sorry if this question is impossible, I'm sort of new to python. When I run the code from outer import inner in a separate program, both outer and inner are printed(as expected). Is there any way to prevent this, without changing the __init__ methods? I'm asking this question in case I'm ever working with a __init__ statement in a built-in module, and need to overwrite it /stop it from running for some reason. All the other questions on SO(that I found, at least) were related to classes, not modules. Thanks in advance!

My module structure:

outer
├── __init__.py (prints out "outer")
├── inner
│   ├── __init__.py (prints out "inner")
│   └── other_code.py 

1 Answers

Warning: Answer kinda useless

I think this would help (I'm ok at python):

In your outer's __init__.py (the one you don't want to call print('outer'), replace the code with this:

if __name__ == '__main__':
    print('outer')

That's kinda useless since then __init__.py would never be able to run. (I tried)

So the easy way is to leave it blank :|.

Related