Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current running module path/name

I've searched and this seems to be a simple question without a simple answer.

I have the file a/b/c.py which would be called with python -m a.b.c. I would like to obtain the value a.b.c in the module level.

USAGE = u'''\ Usage:     python -m %s -h ''' % (what_do_i_put_here,) 

So when I receive the -h option, I display the USAGE without the need to actually write down the actual value in each and every script.

Do I really need to go through inspect to get the desired value?

Thanks.

EDIT: As said, there are answers (I've searched), but not simple answers. Either use inspect, use of traceback, or manipulate __file__ and __package__ and do some substring to get the answer. But nothing as simple as if I had a class in the module, I could just use myClass.__module__ and I would get the answer I want. The use of __name__ is (unfortunately) useless as it's always "__main__".

Also, this is in python 2.6 and I cannot use any other versions.

like image 323
Danosaure Avatar asked Mar 03 '11 16:03

Danosaure


People also ask

How do I find the current module name?

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

How do I find the path of a Python module?

We can also use the inspect module in python to locate a module. We will use inspect. getfile() method of inspect module to get the path. This method will take the module's name as an argument and will return its path.

What is __ path __ in Python?

If you change __path__ , you can force the interpreter to look in a different directory for modules belonging to that package. This would allow you to, e.g., load different versions of the same module based on runtime conditions.

What does __ file __ mean in Python?

When a module is loaded from a file in Python, __file__ is set to its path. You can then use that with other functions to find the directory that the file is located in.


2 Answers

This works for me:

__loader__.fullname 

Also if I do python -m b.c from a\ I get 'b.c' as expected.

Not entirely sure what the __loader__ attribute is so let me know if this is no good.

edit: It comes from PEP 302: http://www.python.org/dev/peps/pep-0302/

Interesting snippets from the link:

The load_module() method has a few responsibilities that it must fulfill before it runs any code:

...

  • It should add an __loader__ attribute to the module, set to the loader object. This is mostly for introspection, but can be used for importer-specific extras, for example getting data associated with an importer.

So it looks like it should work fine in all cases.

like image 110
Shawabawa Avatar answered Sep 17 '22 05:09

Shawabawa


I think you're actually looking for the __name__ special variable. From the Python documentation:

Within a module, the module’s name (as a string) is available as the value of the global variable __name__.

If you run a file directly, this name will __main__. However, if you're in a module (as in the case where you're using the -m flag, or any other import), it will be the complete name of the module.

like image 26
Chris Phillips Avatar answered Sep 19 '22 05:09

Chris Phillips