python 获取函数导入路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2419416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get function import path
提问by evg
from pack.mod import f
How to get from object f information about import - 'pack.mod'
如何从对象 f 获取有关导入的信息 - 'pack.mod'
I can get it using f.__module__
but if function def in module where i get this attribute (f.__module__
) it return '__main__'
. But i need real path here - 'pack.mod'
我可以使用它,f.__module__
但如果在我获得此属性的模块中使用函数 def ( f.__module__
),它将返回'__main__'
。但我需要真正的路径 -'pack.mod'
I found this way to get this information:
我找到了这种方式来获取这些信息:
inspect.getmodule(f).__file__
then i can sub start path from sys.path
, replace /
on .
and get path like - 'pack.mod'
But may be exist some more convenient way?
然后我可以开始分从路径sys.path
,更换/
上.
并获得类似的路径-'pack.mod'
但可能存在一些更便捷的途径?
采纳答案by Alex Martelli
What inspect.getmodule(f)
does internally, per inspect.py's sources, is essentially sys.modules.get(object.__module__)
-- I wouldn't call using that code directly "more convenient", though (beyond the "essentially" part, inspect
has a lot of useful catching and correction of corner cases).
什么inspect.getmodule(f)
内部确实,每inspect.py的来源,基本上是sys.modules.get(object.__module__)
-我不会把使用代码直接“更方便”,虽然(超越“本质”的一部分,inspect
具有捕捉了很多有用的和角落的情况下校正)。
Why not call directly inspect.getsourcefile(f)?
为什么不直接调用inspect.getsourcefile(f)?
Edit: reading between the lines it seems the OP is trying to do something like
编辑:从字里行间看,OP似乎正在尝试做类似的事情
python /foo/bar/baz/bla.py
and within bla.py
(which is thus being run as __main__
) determine "what from
or import
statement could a different main script use to import this function from within me?".
并在bla.py
(因此作为 运行__main__
)确定“不同的主脚本可以使用什么from
或import
语句从我内部导入此函数?”。
Problem is, the question is ill-posed, because there might not be anysuch path usable for the purpose (nothing guarantees the current main script's path is on sys.path
when that different main script gets run later), there might be several different ones (e.g. both /foo/bar
and /foo/bar/baz
might be on sys.path
and /foo/bar/baz/__init__.py
exist, in which case from baz.bla import f
and from bla import f
might both work), and nothing guarantees that some other, previous sys.path
item might not "preempt" the import attempt (e.g., say /foo/bar/baz
is on sys.path
, but before it there's also /fee/fie/foo
, and a completely unrelated file /fee/fie/foo/bla.py
also exists -- etc, etc).
问题是,这个问题是不恰当的,因为可能没有任何这样的路径可用于此目的(sys.path
当不同的主脚本稍后运行时,无法保证当前主脚本的路径是打开的),可能有几个不同的(例如双方/foo/bar
并/foo/bar/baz
可能在sys.path
和/foo/bar/baz/__init__.py
存在,在这种情况下,from baz.bla import f
和from bla import f
可能都工作),并没有什么保证,一些其他的,以前的sys.path
项目可能没有“抢占”的进口尝试(例如,说/foo/bar/baz
是sys.path
,但在此之前它有还/fee/fie/foo
,且完全不相关的文件/fee/fie/foo/bla.py
也存在——等等等等)。
Whatever the purpose of this kind of discovery attempt, I suggest finding an alternative architecture -- e.g., one where from baz.bla import f
isactually executed (as the OP says at the start of the question), so that f.__module__
is correctly set to baz.bla
.
无论这种发现尝试的目的,我建议找一个替代架构-例如,一个地方from baz.bla import f
的实际执行(作为OP说,在这个问题的开始),所以f.__module__
正确设置baz.bla
。
回答by Will
You want the __name__
attribute from __module__
:
你想要的__name__
属性来自__module__
:
In [16]: import inspect
In [17]: inspect.getmodule(MyObject).__name__
Out[17]: 'lib.objects.MyObject'