Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

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 Answer

0 votes
by (36.8k points)

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

If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch

Related questions

Browse Categories

...