Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python __main__ and __init__ proper usage

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

like image 803
im_infamous Avatar asked Feb 07 '16 20:02

im_infamous


People also ask

What is the use of __ main __ in Python?

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

Is __ init __ py necessary?

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.

What are two reasons for using the IF _name_ _main _' syntax?

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.

What is _name_ and _main_ in Python?

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.

What is the use of __ init __ In Python package?

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.


1 Answers

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.

like image 139
hpaulj Avatar answered Sep 22 '22 10:09

hpaulj