获取函数导入路径 [英] Get function import path

查看:90
本文介绍了获取函数导入路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  from pack.mod import f 

如何获得从对象f有关导入信息 - 'pack.mod'



我可以使用 f .__ module __
但是如果函数def在模块中获得了这个属性( f .__ module __ ),它会返回 '__主__'。但我需要真正的路径在这里 - 'pack.mod'



我发现这种方法来获取这些信息:

  inspect.getmodule(f).__ file__ 

然后我可以从 sys.path 开始路径,替换 / on 并获取路径 - 'pack.mod'
但是可能存在一些更方便的方法吗?

解决方案 inspect.py的来源,是本质上是 sys.modules.get(object .__ module __) - 我不会直接使用该代码更方便,尽管(除了基本部分, code> inspect 有很多有用的捕捉和纠正拐角情况)。

为什么不直接调用 inspect.getsourcefile(f)
$ b 修改:阅读在行之间,似乎OP正在尝试做类似于

  python /foo/bar/baz/bla.py 

以及 bla.py (这就是以 __ main __ 的形式运行)从或 import 语句中确定what 可以用一个不同的主脚本从我内部导入这个函数吗?。

问题是,这个问题是不适当的,因为可能没有任何这样的路径可用于此目的(没有什么能够保证当前主脚本的路径在 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 也存在 - etc等)。



无论这种发现尝试的目的是什么,我建议找到一个替代的体系结构 - 例如,其中来自baz.bla import f 实际上是执行的(正如OP在问题开始部分所述) ,所以 f .__模块__ 被正确设置为 baz.bla


from pack.mod import f

How to get from object f information about import - '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'

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?

解决方案

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

Why not call directly inspect.getsourcefile(f)?

Edit: reading between the lines it seems the OP is trying to do something like

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?".

Problem is, the question is ill-posed, because there might not be any such 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).

Whatever the purpose of this kind of discovery attempt, I suggest finding an alternative architecture -- e.g., one where from baz.bla import f is actually executed (as the OP says at the start of the question), so that f.__module__ is correctly set to baz.bla.

这篇关于获取函数导入路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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