importfinder.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:pythonista-scripts 作者: khilnani 项目源码 文件源码
def find_path(name):
    # Reject invalid module names
    if not is_valid_name(name):
        raise ValueError(name + " is not a valid module name")

    name = str(name).strip()

    # Check if built-in
    if name in sys.builtin_module_names:
        return "<built-in>"

    # Check all sys.path locations
    for p in sys.path:
        loc = os.path.abspath(os.path.join(p, name + ".py"))
        if os.path.exists(loc) and os.path.isfile(loc):
            return loc

    # Last resort, import module and check __file__
    try:
        mod = __import__(name)
    except ImportError:
        # Everything failed, assume nonexistant
        return "<string>" 
    return mod.__file__ if hasattr(mod, "__file__") else "<built-in>"
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号