如何检索模块的路径? [英] How to retrieve a module's path?
本文介绍了如何检索模块的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想检测模块是否发生了变化.现在,使用 inotify 很简单,您只需要知道要从中获取通知的目录即可.
I want to detect whether module has changed. Now, using inotify is simple, you just need to know the directory you want to get notifications from.
如何在python中检索模块的路径?
How do I retrieve a module's path in python?
推荐答案
import a_module
print(a_module.__file__)
实际上会给你加载的 .pyc 文件的路径,至少在 Mac OS X 上.所以我想你可以这样做:
Will actually give you the path to the .pyc file that was loaded, at least on Mac OS X. So I guess you can do:
import os
path = os.path.abspath(a_module.__file__)
你也可以试试:
path = os.path.dirname(a_module.__file__)
获取模块的目录.
这篇关于如何检索模块的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文