python:检查是否有重复的库文件

2023-12-20 15:30:14

检查是否有重复的库文件,可能造成运行时冲突。

编写?check_lib_dup.py 如下

# -*- coding: utf-8 -*-
""" 搜索 Windows lib 目录中的某个.pyd """
import os
import sys
 
# Change this based on the library you wish to test
test_lib = "_socket.pyd"
 
def GetSystemDirectory():
    from ctypes import windll, create_string_buffer, sizeof
    GetSystemDirectory = windll.kernel32.GetSystemDirectoryA
    buffer = create_string_buffer(260)
    GetSystemDirectory(buffer, sizeof(buffer))
    return os.fsdecode(buffer.value)
 
def library_search_paths():
    return (
        # Windows search paths
        os.path.dirname(sys.argv[0]),
        os.getcwd(),
        GetSystemDirectory(),
        os.environ["WINDIR"],  # GetWindowsDirectory
        *os.environ["PATH"].split(";"),
 
        # regular Python search paths
        *sys.path,
        )
 
def check_library_duplicate(libname):
    paths = [p for p in library_search_paths()
             if os.path.exists(os.path.join(p, libname))]
 
    print("Library %r found in %d locations:" % (libname, len(paths)))
    for p in paths:
        print("- %r" % p)
 
check_library_duplicate(test_lib)

运行 python?check_lib_dup.py

文章来源:https://blog.csdn.net/belldeep/article/details/135101158
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。