3
3
import os
4
4
import subprocess
5
5
import sys
6
- import venv
7
6
from pathlib import Path
7
+ import site # <-- Импортируем новый модуль
8
8
9
9
BASE_DIR = Path (__file__ ).parent .resolve ()
10
- VENV_DIR = BASE_DIR / ".analytics_venvs"
10
+ # VENV_DIR больше не нужен
11
11
SRC_DIR = BASE_DIR / "python_src"
12
12
SRC_FILE = SRC_DIR / "analytics.py"
13
13
MODEL_FILE = SRC_DIR / "yolov8n.onnx"
22
22
BUILDS ["dml" ] = "requirements_dml.txt"
23
23
24
24
def 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 )} " )
28
27
process = subprocess .Popen (
29
28
command ,
30
29
stdout = subprocess .PIPE ,
31
30
stderr = subprocess .STDOUT ,
32
- shell = is_string ,
33
31
cwd = cwd ,
34
32
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'
41
34
)
42
35
for line in process .stdout :
43
36
print (line , end = '' )
44
37
process .wait ()
45
38
if process .returncode != 0 :
46
39
raise subprocess .CalledProcessError (process .returncode , command )
47
40
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
+
54
53
55
54
def create_and_build (name , req_file ):
56
55
print (f"\n { '=' * 20 } Building: { name .upper ()} { '=' * 20 } " )
57
56
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
+ # ^^^^^^ --- КОНЕЦ ИЗМЕНЕНИЯ --- ^^^^^^
68
60
69
61
print (f"Installing dependencies for { name } from { req_file } ..." )
70
62
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
+ # ^^^^^^ --- КОНЕЦ ИЗМЕНЕНИЯ --- ^^^^^^
83
72
84
73
print (f"Running PyInstaller for { name } ..." )
85
74
86
75
pyinstaller_command = [
87
- str ( python_executable ) , "-m" , "PyInstaller" ,
76
+ python_executable , "-m" , "PyInstaller" ,
88
77
"--noconfirm" , "--onefile" ,
89
78
f"--name=analytics_{ name } " ,
90
79
f"--distpath={ DIST_PATH } " ,
91
80
f"--add-data={ MODEL_FILE } { os .pathsep } ." ,
92
81
"--hidden-import=numpy.core._multiarray_umath" ,
93
82
]
94
83
95
- onnx_libs_path = get_onnx_libs_path (venv_path )
84
+ # VVVVVV --- ИЗМЕНЕНИЕ: Используем новую функцию поиска библиотек --- VVVVVV
85
+ onnx_libs_path = get_onnx_libs_path ()
86
+ # ^^^^^^ --- КОНЕЦ ИЗМЕНЕНИЯ --- ^^^^^^
96
87
binary_sep = os .pathsep
97
88
98
- if name == "dml" and onnx_libs_path .exists ():
89
+ if name == "dml" and onnx_libs_path and onnx_libs_path .exists ():
99
90
print ("Adding DirectML provider binaries..." )
100
91
for lib in ["onnxruntime_providers_shared.dll" , "onnxruntime_providers_dml.dll" , "DirectML.dll" ]:
101
92
if (onnx_libs_path / lib ).exists ():
@@ -117,7 +108,7 @@ def create_and_build(name, req_file):
117
108
print ("Please make sure 'yolov8n.onnx' is placed in the 'python_src' directory." )
118
109
sys .exit (1 )
119
110
120
- VENV_DIR .mkdir (exist_ok = True )
111
+ # VENV_DIR.mkdir(exist_ok=True) <-- Больше не нужно
121
112
DIST_PATH .mkdir (parents = True , exist_ok = True )
122
113
123
114
for name , req_filename in BUILDS .items ():
0 commit comments