Since I'm rather new to python this particular aspect of language still opaque for me.
So, assume that my project contains many files with code that does stuff and two "service" files: __init__.py
and __main__.py
In __init__.py
there is only:
if __name__ == "__main__":
import package.__main__
__main__.main()
And in __main__.py
as follows:
import package # ok
import package2 # ok
def main():
package.myfunc1() # can't find reference to myfunc1
package2.myfunc2() # can't find reference to myfunc2
So my question is: why both packages are visible while functions inside are not? I've read some source code from google and still can't spot the difference between it and my code. I'm using Python 3.5.1
I assume, that code in __init__
will launch __main__
and __main__
will launch the rest of my functions.
UPD
Well, my apologies if I confused someone with my code.
The idea that stand behind __init__.py
is that file was created by IDE when the first package was added so I decided to fill it with code found on first github entry(my fault, I though it can be re-used by copy-paste).
Strictly speaking I just need python construction, that equivalent this 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 the following code
import package
import package2
if __name__ == "__main__":
package.myfunc1()
package2.myfunc2()
has exactly same issue that stated above, so the matter is not in __init__.py
__main__ is the name of the environment where top-level code is run. “Top-level code” is the first user-specified Python module that starts running. It's “top-level” because it imports all other modules that the program needs. Sometimes “top-level code” is called an entry point to the application.
The __init__.py files are required to make Python treat directories containing the file as packages. This prevents directories with a common name, such as string , unintentionally hiding valid modules that occur later on the module search path.
In Short: It Allows You to Execute Code When the File Runs as a Script, but Not When It's Imported as a Module. For most practical purposes, you can think of the conditional block that you open with if __name__ == "__main__" as a way to store code that should only run when your file is executed as a script.
Every Python module has it's __name__ defined and if this is '__main__', it implies that the module is being run standalone by the user and we can do corresponding appropriate actions. If you import this script as a module in another script, the __name__ is set to the name of the script/module.
If a file named __init__.py is present in a package directory, it is invoked when the package or a module in the package is imported. You can use this to execute package initialization code, for example for the initialization of package-level data.
I made:
foo/
__init__.py
__main__.py
with the 2 files being:
# __init__.py
import __main__
print('in init')
print('init name',__name__)
and
# __main__.py
print('main name',__name__)
print('in main')
if __name__=='__main__':
print('in main main block')
If I run __main__
directly:
1538:~/mypy$ python foo/__main__.py
('main name', '__main__')
in main
in main main block
It does same thing if I invoke the directory. It does not import __init__.py
.
1541:~/mypy$ python foo
('main name', '__main__')
in main
in main main block
But from a shell, it loads both files
1542:~/mypy$ python
....
>>> import foo
('main name', 'foo.__main__')
in main
in init
('init name', 'foo')
But it does not use the if __name__
block of __main__
- the name isn't right, it's now foo.__main__
.
relevant docs
https://docs.python.org/2/using/cmdline.html#interface-options
Execute the Python code contained in script, which must be a filesystem path (absolute or relative) referring to either a Python file, a directory containing a
__main__.py
file, or a zipfile containing a__main__.py
file.
https://docs.python.org/2/library/\__main__.html
This module represents the (otherwise anonymous) scope in which the interpreter’s main program executes — commands read either from standard input, from a script file, or from an interactive prompt. It is this environment in which the idiomatic “conditional script” stanza causes a script to run:
I created another directory with an __init__.py
but no main:
1558:~/mypy$ python foo1
/usr/bin/python: can't find '__main__' module in 'foo1'
1558:~/mypy$ python
...
>>> import foo1
('in init', 'foo1')
>>>
import
works, but I can't run
the directory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With