How to monitor module loading and obtain version number

I tried the following code,I am trying to add a custom MetaPathFinder to listen for module loading

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import importlib.util
import sys
from importlib.abc import MetaPathFinder
class ModuleLoadListener(MetaPathFinder):
def find_spec(self, fullname, path, target=None):
for finder in sys.meta_path:
if finder == self:
continue
spec = finder.find_spec(fullname, path, target)
if spec is not None:
spec.loader = CustomLoader(spec.loader)
return spec
return None
class CustomLoader(importlib.abc.Loader):
def __init__(self, loader):
self.loader = loader
def create_module(self, spec):
return self.loader.create_module(spec)
def exec_module(self, module):
return self.loader.exec_module(module)
sys.meta_path.insert(0, ModuleLoadListener())
import requests
</code>
<code>import importlib.util import sys from importlib.abc import MetaPathFinder class ModuleLoadListener(MetaPathFinder): def find_spec(self, fullname, path, target=None): for finder in sys.meta_path: if finder == self: continue spec = finder.find_spec(fullname, path, target) if spec is not None: spec.loader = CustomLoader(spec.loader) return spec return None class CustomLoader(importlib.abc.Loader): def __init__(self, loader): self.loader = loader def create_module(self, spec): return self.loader.create_module(spec) def exec_module(self, module): return self.loader.exec_module(module) sys.meta_path.insert(0, ModuleLoadListener()) import requests </code>
import importlib.util
import sys
from importlib.abc import MetaPathFinder


class ModuleLoadListener(MetaPathFinder):
    def find_spec(self, fullname, path, target=None):
        for finder in sys.meta_path:
            if finder == self:
                continue
            spec = finder.find_spec(fullname, path, target)
            if spec is not None:
                spec.loader = CustomLoader(spec.loader)
                return spec

        return None


class CustomLoader(importlib.abc.Loader):
    def __init__(self, loader):
        self.loader = loader

    def create_module(self, spec):
        return self.loader.create_module(spec)

    def exec_module(self, module):
        return self.loader.exec_module(module)


sys.meta_path.insert(0, ModuleLoadListener())

import requests

But he had an abnormality,But there are some modules that do not report errors when imported

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> File "D:appdeveloperpy3python3.11Libsite-packagesrequests-2.31.0-py3.11.eggrequestsutils.py", line 63, in <module>
DEFAULT_CA_BUNDLE_PATH = certs.where()
^^^^^^^^^^^^^
File "D:appdeveloperpy3python3.11Libsite-packagescertifi-2023.11.17-py3.11.eggcertificore.py", line 37, in where
_CACERT_PATH = str(_CACERT_CTX.__enter__())
^^^^^^^^^^^^^^^^^^^^^^^
File "D:appdeveloperpy3python3.11Libcontextlib.py", line 137, in __enter__
return next(self.gen)
^^^^^^^^^^^^^^
File "D:appdeveloperpy3python3.11Libimportlibresources_common.py", line 80, in _tempfile
os.write(fd, reader())
^^^^^^^^
File "D:appdeveloperpy3python3.11Libimportlibresourcesabc.py", line 76, in read_bytes
with self.open('rb') as strm:
^^^^^^^^^^^^^^^
File "D:appdeveloperpy3python3.11Libimportlibresources_adapters.py", line 141, in open
raise FileNotFoundError("Can't open orphan path")
FileNotFoundError: Can't open orphan path
</code>
<code> File "D:appdeveloperpy3python3.11Libsite-packagesrequests-2.31.0-py3.11.eggrequestsutils.py", line 63, in <module> DEFAULT_CA_BUNDLE_PATH = certs.where() ^^^^^^^^^^^^^ File "D:appdeveloperpy3python3.11Libsite-packagescertifi-2023.11.17-py3.11.eggcertificore.py", line 37, in where _CACERT_PATH = str(_CACERT_CTX.__enter__()) ^^^^^^^^^^^^^^^^^^^^^^^ File "D:appdeveloperpy3python3.11Libcontextlib.py", line 137, in __enter__ return next(self.gen) ^^^^^^^^^^^^^^ File "D:appdeveloperpy3python3.11Libimportlibresources_common.py", line 80, in _tempfile os.write(fd, reader()) ^^^^^^^^ File "D:appdeveloperpy3python3.11Libimportlibresourcesabc.py", line 76, in read_bytes with self.open('rb') as strm: ^^^^^^^^^^^^^^^ File "D:appdeveloperpy3python3.11Libimportlibresources_adapters.py", line 141, in open raise FileNotFoundError("Can't open orphan path") FileNotFoundError: Can't open orphan path </code>
  File "D:appdeveloperpy3python3.11Libsite-packagesrequests-2.31.0-py3.11.eggrequestsutils.py", line 63, in <module>
    DEFAULT_CA_BUNDLE_PATH = certs.where()
                             ^^^^^^^^^^^^^
  File "D:appdeveloperpy3python3.11Libsite-packagescertifi-2023.11.17-py3.11.eggcertificore.py", line 37, in where
    _CACERT_PATH = str(_CACERT_CTX.__enter__())
                       ^^^^^^^^^^^^^^^^^^^^^^^
  File "D:appdeveloperpy3python3.11Libcontextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "D:appdeveloperpy3python3.11Libimportlibresources_common.py", line 80, in _tempfile
    os.write(fd, reader())
                 ^^^^^^^^
  File "D:appdeveloperpy3python3.11Libimportlibresourcesabc.py", line 76, in read_bytes
    with self.open('rb') as strm:
         ^^^^^^^^^^^^^^^
  File "D:appdeveloperpy3python3.11Libimportlibresources_adapters.py", line 141, in open
    raise FileNotFoundError("Can't open orphan path")
FileNotFoundError: Can't open orphan path

Am I doing this right?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật