Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access the current executing module or class name in Python?

Tags:

python

module

I would like to be able to dynamically retrieve the current executing module or class name from within an imported module. Here is some code:

foo.py:

def f():     print __name__ 

bar.py:

from foo import f  def b(): f() 

This obviously does not work as __name__ is the name of the module that contains the function. What I would like to be access inside the foo module is the name of the current executing module that is using foo. So in the case above it would be bar but if any other module imported foo I would like foo to dynamically have access to the name of that module.

Edit: The inspect module looks quite promising but it is not exactly what I was looking for. What I was hoping for was some sort of global or environment-level variable that I could access that would contain the name of the current executing module. Not that I am unwilling to traverse the stack to find that information - I just thought that Python may have exposed that data already.

Edit: Here is how I am trying to use this. I have two different Django applications that both need to log errors to file. Lets say that they are called "AppOne" and "AppTwo". I also have a place to which I would like to log these files: "/home/hare/app_logs". In each application at any given point I would like to be able to import my logger module and call the log function which writes the log string to file. However what I would like to do is create a directory under app_logs that is the name of the current application ("AppOne" or "AppTwo") so that each application's log files will go in their respective logging directories.

In order to do this I thought that the best way would be for the logger module to have access to some sort of global variable that denotes the current application's name as it is responsible for knowing the location of the parent logging directory and creating the application's logging directory if it does not yet exist.

like image 803
Andrew Hare Avatar asked Mar 02 '09 15:03

Andrew Hare


People also ask

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

How do I access a Python module?

In Python, modules are accessed by using the import statement. When you do this, you execute the code of the module, keeping the scopes of the definitions so that your current file(s) can make use of these.

How do you access a class from a module in Python?

Python modules can get access to code from another module by importing the file/function using import. The import statement is that the commonest way of invoking the import machinery, but it's not the sole way. The import statement consists of the import keyword alongside the name of the module.


1 Answers

From the comment -- not the question.

I am simply curious to see if what I am trying to do is possible.

The answer to "is it possible" is always "yes". Always. Unless your question involves time travel, anti-gravity or perpetual motion.

Since the answer is always "yes", your question is ill-formed. The real question is "what's a good way to have my logging module know the name of the client?" or something like that.

The answer is "Accept it as a parameter." Don't mess around with inspecting or looking for mysterious globals or other tricks.

Just follow the design pattern of logging.getLogger() and use explicitly-named loggers. A common idiom is the following

logger= logging.getLogger( __name__ ) 

That handles almost all log naming perfectly.

like image 157
S.Lott Avatar answered Oct 06 '22 01:10

S.Lott