如何访问模块导入的名称?

时间:2017-07-24 16:02:03

标签: python python-2.7 python-3.x import module

我想在我的Python模块foo中编写一个简单的教程。

如果模块导入为foo,我希望教程将其称为foo

>>> import foo
>>> foo.tutorial()
Please run foo.baz().

但是,当模块导入为bar时 - 我希望教程将其称为bar

>>> import foo as bar
>>> bar.tutorial()
Please run bar.baz().

我知道__name____package__变量都不会改变import ... as ...上的值。

Is it possible to detect way of import of a module in python?不同 - 只要我知道导入模块的第一个别名就足够了。

2 个答案:

答案 0 :(得分:1)

它不漂亮,万无一失,或者制作有价值,但是:

import inspect
import sys


thismod = sys.modules[__name__]


def imported_as():
    imported_frame = inspect.currentframe().f_back.f_back
    for k, v in imported_frame.f_locals.items():
        if v is thismod:
            return k
    for k, v in imported_frame.f_globals.items():
        if v is thismod:
            return k


def tutorial():
    print('please call {}.baz()'.format(imported_as()))

一个小小的解释:

  • 跳起两个堆叠帧
  • 查看locals和globals以查找与模块
  • 相同的模块对象

为什么你真的不应该这样做:

  • 不是万无一失的:
    • from导入会破坏此
    • 同一模块的多次导入将不确定地仅提供第一个找到的名称
  • inspect.currentframe超级神奇,通常应该在生产代码中避免使用

答案 1 :(得分:0)

以下是安东尼的类似解决方案。我相信它可以更好地处理多个进口。 from进口仍是一个问题。

import inspect
import re
import readline

def tutorial():
    frame = inspect.currentframe().f_back    
    if "__file__" in frame.f_locals:
        f = inspect.getframeinfo(frame)
        m = re.search(r"(([^\.]*).*)\.tutorial", f.code_context[0].strip())
        if m:
            parent,path = m.group(2),m.group(1)
            if inspect.ismodule(frame.f_locals[parent]):
                print "Please run {}.baz()".format(path)
                return
        print "Verify import and run foo.baz()"
    else:
        loc = frame.f_locals
        glo = frame.f_globals
        local_foo = [(k, v.__name__)for k,v in loc.items() if inspect.ismodule(v) and v.__name__=="foo"]
        global_foo = [(k, v.__name__)for k,v in glo.items() if inspect.ismodule(v) and v.__name__=="foo"]
        if local_foo and set(local_foo)==set(global_foo):
            print "Please run {}.baz()".format(global_foo[-1][0])
        elif local_foo and global_foo:
            print "Please run {}.baz() in local scope or {}.baz()".format(local_foo[-1][0], global_foo[-1][0])
        elif local_foo:
            print "Please run {}.baz() in local scope".format(local_foo[-1][0])
        elif global_foo:
            print "Please run {}.baz()".format(global_foo[-1][0])
        else:
            n = readline.get_current_history_length()
            h = [str(readline.get_history_item(i)) for i in range(n)] + [readline.get_line_buffer()]
            h.reverse()
            for i in h:
                matches = re.findall(r"([A-Za-z0-9_-]*(\.[A-Za-z0-9_-]*)*)\.tutorial", i)
                for match in matches:
                    if _baz_check(glo, match[0]):
                        print "Please run {}.baz()".format(match[0])
                        return
            print "Verify import and run foo.baz()"


def _baz_check(d, path):
    path = path.split(".")
    if path[0] not in d:
        return False
    cur = d[path[0]]
    for i in range(1,len(path)):
        if not hasattr(cur, path[i]):
            return False
        cur = getattr(cur, path[i])
    return hasattr(cur, "__name__") and cur.__name__ == "foo" and hasattr(cur, "baz")

def baz():
    print "Baz"
 
import foo
foo.tutorial()
import foo as bar
bar.tutorial()

def f():
    import foo as local_foo
    local_foo.tutorial()

f()
from foo import tutorial
tutorial()
import mod
mod.foo.tutorial()
from mod import foo as dum
dum.tutorial()
Please run foo.baz()
Please run bar.baz()
Please run local_foo.baz()
Verify import and run foo.baz()
Please run mod.foo.baz()
Please run dum.baz()