initialization - python __main__ and __init__ proper usage -


since i'm rather new python particular aspect of language still opaque me.

so, assume project contains many files code stuff , 2 "service" files: __init__.py , __main__.py

in __init__.py there only:

if __name__ == "__main__":     import package.__main__     __main__.main() 

and in __main__.py follows:

import package # ok import package2 # ok  def main():     package.myfunc1() # can't find reference myfunc1     package2.myfunc2() # can't find reference myfunc2 

so question is: why both packages visible while functions inside not? i've read source code google , still can't spot difference between , code. i'm using python 3.5.1

i assume, code in __init__ launch __main__ , __main__ launch rest of functions.

upd

well, apologies if confused code. idea stand behind __init__.py file created ide when first package added decided fill code found on first github entry(my fault, though can re-used copy-paste).

strictly speaking need python construction, equivalent c code:

header.h void func1(){...} //in code1.c void func2(){...} //in code2.c #include "header.h" int main() //in main.c  {     func1();     func2();     return 0; } 

and following code

import package import package2   if __name__ == "__main__":     package.myfunc1()      package2.myfunc2()  

has same issue stated above, matter not in __init__.py

well:

__init__.py useful import (all subdirectories search import)

and

if __name__ == "__main__":

is use run module itself. par of code not executed if imported in other script


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -