-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdependencies.py
More file actions
76 lines (54 loc) · 1.79 KB
/
dependencies.py
File metadata and controls
76 lines (54 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import sys
import importlib
import platform
def _import_module(module_name, attr=None):
try:
module = importlib.import_module(module_name)
return getattr(module, attr) if attr else module
except Exception:
return None
def _install_package(package_name, feedback=None):
system_os = platform.system()
if feedback:
feedback.pushInfo(f'Package "{package_name}" not found. Trying to install...')
try:
if system_os == "Linux":
import subprocess
subprocess.check_call(
[sys.executable, "-m", "pip", "install", package_name],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
else:
# macOS e Windows → funciona melhor
import pip
pip.main(["install", package_name])
if feedback:
feedback.pushInfo(f'Package "{package_name}" installed successfully.')
return True
except Exception as e:
if feedback:
feedback.reportError(f'Failed to install "{package_name}": {e}')
return False
# ==========================
# PILLOW
# ==========================
def ensure_pillow(feedback=None):
Image = _import_module("PIL", "Image")
if Image is not None:
return Image
if not _install_package("Pillow", feedback):
return None
importlib.invalidate_caches()
return _import_module("PIL", "Image")
# ==========================
# MATPLOTLIB
# ==========================
def ensure_matplotlib(feedback=None):
path = _import_module("matplotlib", "path")
if path is not None:
return path
if not _install_package("matplotlib", feedback):
return None
importlib.invalidate_caches()
return _import_module("matplotlib", "path")