33import os
44import subprocess
55import sys
6- import venv
76from pathlib import Path
7+ import site # <-- Импортируем новый модуль
88
99BASE_DIR = Path (__file__ ).parent .resolve ()
10- VENV_DIR = BASE_DIR / ".analytics_venvs"
10+ # VENV_DIR больше не нужен
1111SRC_DIR = BASE_DIR / "python_src"
1212SRC_FILE = SRC_DIR / "analytics.py"
1313MODEL_FILE = SRC_DIR / "yolov8n.onnx"
2222 BUILDS ["dml" ] = "requirements_dml.txt"
2323
2424def run_command (command , cwd = None ):
25- """Runs a command, ensuring shell is used for strings."""
26- is_string = isinstance (command , str )
27- print (f"--- Running command: { command } " )
25+ """Runs a command as a list of arguments, which is safer."""
26+ print (f"--- Running command: { ' ' .join (command )} " )
2827 process = subprocess .Popen (
2928 command ,
3029 stdout = subprocess .PIPE ,
3130 stderr = subprocess .STDOUT ,
32- shell = is_string ,
3331 cwd = cwd ,
3432 text = True ,
35- encoding = 'utf-8' ,
36- # VVVVVV --- ИЗМЕНЕНИЕ 1: Добавляем executable для Linux/macOS --- VVVVVV
37- # Это заставляет Popen использовать /bin/bash, который лучше понимает команду 'source' (или '.'),
38- # чем стандартный /bin/sh.
39- executable = "/bin/bash" if not is_string and sys .platform != "win32" else None
40- # ^^^^^^ --- КОНЕЦ ИЗМЕНЕНИЯ 1 --- ^^^^^^
33+ encoding = 'utf-8'
4134 )
4235 for line in process .stdout :
4336 print (line , end = '' )
4437 process .wait ()
4538 if process .returncode != 0 :
4639 raise subprocess .CalledProcessError (process .returncode , command )
4740
48- def get_onnx_libs_path (venv_path ):
49- if sys .platform == "win32" :
50- return venv_path / "Lib" / "site-packages" / "onnxruntime" / "capi"
51- else :
52- py_version = f"python{ sys .version_info .major } .{ sys .version_info .minor } "
53- return venv_path / "lib" / py_version / "site-packages" / "onnxruntime" / "capi"
41+ # VVVVVV --- ИЗМЕНЕНИЕ: Ищем библиотеки в системном site-packages --- VVVVVV
42+ def get_onnx_libs_path ():
43+ """Finds the onnxruntime/capi path in the main Python environment."""
44+ # site.getsitepackages() возвращает список путей, обычно один
45+ for site_path in site .getsitepackages ():
46+ potential_path = Path (site_path ) / "onnxruntime" / "capi"
47+ if potential_path .exists ():
48+ return potential_path
49+ # Если не нашли, возвращаем None
50+ return None
51+ # ^^^^^^ --- КОНЕЦ ИЗМЕНЕНИЯ --- ^^^^^^
52+
5453
5554def create_and_build (name , req_file ):
5655 print (f"\n { '=' * 20 } Building: { name .upper ()} { '=' * 20 } " )
5756
58- venv_path = VENV_DIR / name
59-
60- if sys .platform == "win32" :
61- python_executable = venv_path / "Scripts" / "python.exe"
62- else :
63- python_executable = venv_path / "bin" / "python"
64-
65- if not venv_path .exists ():
66- print (f"Creating virtual environment for { name } ..." )
67- venv .create (venv_path , with_pip = True )
57+ # VVVVVV --- ИЗМЕНЕНИЕ: Используем Python, который запустил этот скрипт --- VVVVVV
58+ python_executable = sys .executable
59+ # ^^^^^^ --- КОНЕЦ ИЗМЕНЕНИЯ --- ^^^^^^
6860
6961 print (f"Installing dependencies for { name } from { req_file } ..." )
7062
71- # VVVVVV --- ИЗМЕНЕНИЕ 2: Формируем команду с активацией venv --- VVVVVV
72- if sys .platform == "win32" :
73- activate_script = venv_path / "Scripts" / "activate.bat"
74- # На Windows используем `call` и `&&` для последовательного выполнения
75- cmd_as_string = f'call "{ activate_script } " && python -m pip install -r "{ req_file } "'
76- else :
77- # На Linux используем `.` (аналог `source`) и `&&`
78- activate_script = venv_path / "bin" / "activate"
79- cmd_as_string = f'. "{ activate_script } " && pip install -r "{ req_file } "'
80-
81- run_command (cmd_as_string )
82- # ^^^^^^ --- КОНЕЦ ИЗМЕНЕНИЯ 2 --- ^^^^^^
63+ # VVVVVV --- ИЗМЕНЕНИЕ: Простая и надежная команда установки --- VVVVVV
64+ install_command = [
65+ python_executable ,
66+ "-m" , "pip" ,
67+ "install" ,
68+ "-r" , str (req_file )
69+ ]
70+ run_command (install_command )
71+ # ^^^^^^ --- КОНЕЦ ИЗМЕНЕНИЯ --- ^^^^^^
8372
8473 print (f"Running PyInstaller for { name } ..." )
8574
8675 pyinstaller_command = [
87- str ( python_executable ) , "-m" , "PyInstaller" ,
76+ python_executable , "-m" , "PyInstaller" ,
8877 "--noconfirm" , "--onefile" ,
8978 f"--name=analytics_{ name } " ,
9079 f"--distpath={ DIST_PATH } " ,
9180 f"--add-data={ MODEL_FILE } { os .pathsep } ." ,
9281 "--hidden-import=numpy.core._multiarray_umath" ,
9382 ]
9483
95- onnx_libs_path = get_onnx_libs_path (venv_path )
84+ # VVVVVV --- ИЗМЕНЕНИЕ: Используем новую функцию поиска библиотек --- VVVVVV
85+ onnx_libs_path = get_onnx_libs_path ()
86+ # ^^^^^^ --- КОНЕЦ ИЗМЕНЕНИЯ --- ^^^^^^
9687 binary_sep = os .pathsep
9788
98- if name == "dml" and onnx_libs_path .exists ():
89+ if name == "dml" and onnx_libs_path and onnx_libs_path .exists ():
9990 print ("Adding DirectML provider binaries..." )
10091 for lib in ["onnxruntime_providers_shared.dll" , "onnxruntime_providers_dml.dll" , "DirectML.dll" ]:
10192 if (onnx_libs_path / lib ).exists ():
@@ -117,7 +108,7 @@ def create_and_build(name, req_file):
117108 print ("Please make sure 'yolov8n.onnx' is placed in the 'python_src' directory." )
118109 sys .exit (1 )
119110
120- VENV_DIR .mkdir (exist_ok = True )
111+ # VENV_DIR.mkdir(exist_ok=True) <-- Больше не нужно
121112 DIST_PATH .mkdir (parents = True , exist_ok = True )
122113
123114 for name , req_filename in BUILDS .items ():
0 commit comments