Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When running __main__.py, get current module

I'm creating a program that is executable via the packaged __main__.py and installed so I can run it simply as my_module_name from the command line. That all is working great.

I'm trying to configure a logger from that __main__.py file that is used hierarchically from sub-modules. The loggers in the sub-modules all get their logger by doing:

logger = logging.getLogger(__name__)

Which is great because name is my_module_name.Controllers.A, etc. I'm looking to setup my_module_name.Controllers from __main__.pybut I cant seem to find my_module_name anywhere while I'm debugging and executing __main__.py directly. I understand this is kind of funky because python doesn't really execute __main__.py as a module... but it's in the my_module_name directory.. Is it hacky to just grab that name? Am I going down the right path with this?

Thanks

like image 268
four43 Avatar asked Jul 12 '18 22:07

four43


People also ask

What is the __ main __ module 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.

How do I find the current module name in Python?

A module can find out its own module name by looking at the predefined global variable __name__.

What will happen when Python main py is executed?

So what happens when we execute the command. Python looks for a file named __main__.py to start its execution automatically. If it doesn't find it it will throw an error else it will execute main.py and from the code, you can well understand that it will import the modules from src to find the area.

What does __ name __ do in Python?

The __name__ variable (two underscores before and after) is a special Python variable. It gets its value depending on how we execute the containing script. Sometimes you write a script with functions that might be useful in other scripts as well. In Python, you can import that script as a module in another script.


1 Answers

TL;DR. Yes, you're basically already doing the right thing. :)

You cannot get "current module" when running __main__.py. This boils down to difference between module and (module executed as) script (or also "main module") the details of which are described in PEP-338:

In a main module, the value of __name__ is always '__main__'

There is an extra piece of information in PEP-366 regarding relative imports. __package__ attributed has been introduced. There is also this bit of information:

When the main module is specified by its filename, then the __package__ attribute will be set to None. To allow relative imports when the module is executed directly, boilerplate similar to the following would be needed before the first relative import statement:

if __name__ == "__main__" and __package__ is None: __package__ = "expected.package.name"

In other words. When being directly called, there module __name__ is "__main__" and __package__ attribute is None, you'd have to set it yourself for (relative imports to work or) any (other) use. You can try to guess, what should it based on __file__ or if stable, you can just write the name in.

like image 104
Ondrej K. Avatar answered Nov 15 '22 00:11

Ondrej K.