如何在 Python 中访问当前执行的模块或类名? [英] How can I access the current executing module or class name in Python?

查看:33
本文介绍了如何在 Python 中访问当前执行的模块或类名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够从导入的模块中动态检索当前执行的模块或类名.这是一些代码:

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()

这显然不起作用,因为 __name__ 是包含该函数的模块的名称.我想在 foo 模块中访问的是使用 foo 的当前执行模块的名称.所以在上面的例子中它会是 bar 但如果任何其他模块导入 foo 我希望 foo 动态地访问那个的名称模块.

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.

inspect 模块看起来很有前途,但它并不是我想要的.我希望的是某种全局或环境级别的变量,我可以访问它,其中包含当前执行模块的名称.并不是我不愿意遍历堆栈来查找该信息 - 我只是认为 Python 可能已经公开了这些数据.

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.

这是我尝试使用它的方式.我有两个不同的 Django 应用程序,它们都需要将错误记录到文件中.假设它们被称为AppOne"和AppTwo".我还有一个地方想记录这些文件:/home/hare/app_logs".在任何给定点的每个应用程序中,我希望能够导入我的记录器模块并调用将日志字符串写入文件的日志函数.但是,我想要做的是在 app_logs 下创建一个目录,该目录是当前应用程序的名称(AppOne"或AppTwo"),以便每个应用程序的日志文件都将进入各自的日志记录目录.

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.

推荐答案

来自评论——不是问题.

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.

只需遵循 logging.getLogger() 的设计模式并使用显式命名的记录器.一个常见的成语如下

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.

这篇关于如何在 Python 中访问当前执行的模块或类名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆