httpimport 简单试用

以前简单说明过httpimport 的功能,以下尝试下使用,同时对于问题做一些简单的说明

参考代码

  • app.py
import httpimport
with httpimport.pypi_repo():
    from hashids import Hashids
    print(Hashids.__dict__)
    hashids = Hashids()
    hashid = hashids.encode(123) 
    print(hashid)
  • 问题

因为我使用的python 3.11 对于模块加载处理上有一些问题,修改如下

def find_spec(self, fullname, path, target=None):
    loader = self.find_module(fullname, path)
    if loader is not None:
        return importlib.util.spec_from_loader(fullname, loader)

        # return importlib.machinery.ModuleSpec(
        # fullname, loader)
    return None
  • 运行效果

说明

httpimport 实际上就是实现了python 的import 语义,可以方便的在自己的上下文中进行模块加载,值得尝试下

参考资料

https://github.com/operatorequals/httpimport/blob/master/httpimport.py#L569

posted on 2025-04-06 08:00  荣锋亮  阅读(16)  评论(0)    收藏  举报

导航