3737BASE_DIR = os .path .dirname (__file__ )
3838
3939
40- def check_vcomp_available (vcomp_dll_name : str = "vcomp140.dll" ) -> tuple [bool , str ]:
41- """
42- check if the current Python environment can load the specified vcomp DLL
43- :param vcomp_dll_name: The name of the vcomp DLL to detect (e.g., vcomp140.dll, vcomp143.dll)
44- :return: (Availability, Description of detection results)
45- """
46- search_paths = []
47- search_paths .append (os .getcwd ())
48- search_paths .append (os .path .dirname (sys .executable ))
49- search_paths .append (os .path .join (os .environ ["WINDIR" ], "System32" ))
50- search_paths .extend (os .environ ["PATH" ].split (os .pathsep ))
51-
52- # 步骤3:遍历路径检查DLL文件是否存在
53- dll_full_path = None
54- for path in search_paths :
55- candidate = os .path .join (path , vcomp_dll_name )
56- candidate = os .path .normpath (candidate )
57- if os .path .exists (candidate ) and os .path .isfile (candidate ):
58- dll_full_path = candidate
59- break
60-
61- if not dll_full_path :
62- return (False , f"Not found: { vcomp_dll_name } ! serch path: { search_paths } \n " )
63-
64- try :
65- from ctypes import WinDLL
66- vcomp_dll = WinDLL (dll_full_path )
67- has_core_func = hasattr (vcomp_dll , "_vcomp_get_thread_num" )
68- if has_core_func :
69- return (True , f"Success loaded { vcomp_dll_name } ! path: { dll_full_path } \n " )
70- else :
71- return (False , f"{ vcomp_dll_name } The file exists, but there is no core export function (it may be a tampered/incorrect version)" )
72- except OSError as e :
73- error_code = e .winerror if hasattr (e , "winerror" ) else - 1
74- if error_code == 126 :
75- msg = "The specified module cannot be found (DLL file is corrupted/missing dependencies)"
76- elif error_code == 193 :
77- msg = "%1 is not a valid Win32 application (bit mismatch, such as a 32-bit DLL loaded in 64-bit Python)"
78- else :
79- msg = f"Failed to load, Windows error code: { error_code } , description: { e .strerror } "
80- return (False , f"Failed load { vcomp_dll_name } ! path: { dll_full_path } \n errmsg: { msg } \n " )
81- except Exception as e :
82- return (False , f"Failed load { vcomp_dll_name } ! { str (e )} " )
83-
84-
8540if sys .platform == 'win32' :
8641 # add_dll_directory() 有时不生效
8742 os .add_dll_directory (os .path .join (os .path .dirname (__file__ ), 'cpp' ))
@@ -90,11 +45,6 @@ def check_vcomp_available(vcomp_dll_name: str = "vcomp140.dll") -> tuple[bool, s
9045 dll_directory = os .path .join (BASE_DIR , 'cpp' )
9146 new_path = f"{ dll_directory } ;{ current_path } "
9247 os .environ ['PATH' ] = new_path
93-
94- available , message = check_vcomp_available ("vcomp140.dll" )
95- if not available :
96- print (message )
97- print ("缺失 vcomp140.dll, 需要安装VC运行时, Microsoft 地址:https://aka.ms/vc14/vc_redist.x64.exe" )
9848else :
9949 current_path = os .environ .get ('LD_LIBRARY_PATH' , '' )
10050 dll_directory = os .path .join (BASE_DIR , 'cpp' )
0 commit comments