SafeImport 0.8.9__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
SafeImport/__init__.py ADDED
@@ -0,0 +1,76 @@
1
+
2
+
3
+
4
+ def safe_import(module_name, pipName=None, log=False, ret=True, verifyOnly=False):
5
+ import subprocess as sub
6
+ import importlib
7
+ import sys
8
+ from inspect import stack
9
+
10
+ pipName = pipName or module_name
11
+
12
+ def pip_installed():
13
+ try:
14
+ sub.check_call(
15
+ [sys.executable, '-m', 'pip', '--version'],
16
+ stdout=sub.DEVNULL,
17
+ stderr=sub.DEVNULL
18
+ )
19
+ return True
20
+ except (sub.CalledProcessError, FileNotFoundError):
21
+ return False
22
+
23
+ def install_pip():
24
+ if log: print('PIP NOT FOUND. INSTALLING...')
25
+ try:
26
+ sub.check_call(
27
+ [sys.executable, '-m', 'ensurepip', '--upgrade'],
28
+ stdout=sub.DEVNULL,
29
+ stderr=sub.DEVNULL
30
+ )
31
+ return pip_installed()
32
+ except Exception as e:
33
+ return False
34
+
35
+ if not pip_installed():
36
+ if not install_pip():
37
+ if log: print('[safe_import] Failed to install PIP!')
38
+ return None
39
+
40
+ try:
41
+ imported = importlib.import_module(module_name)
42
+ if __name__ == '__main__':
43
+ if verifyOnly:
44
+ pass
45
+ else:
46
+ globals()[module_name] = imported
47
+ else:
48
+ if verifyOnly:
49
+ pass
50
+ else:
51
+ stack()[1].frame.f_globals[module_name] = imported
52
+ if ret: return imported
53
+ except ImportError:
54
+ if log: print(f'MODULE {module_name} NOT FOUND. INSTALLING...')
55
+ try:
56
+ sub.check_call(
57
+ [sys.executable, '-m', 'pip', 'install', pipName],
58
+ stdout=sub.DEVNULL,
59
+ stderr=sub.DEVNULL
60
+ )
61
+ imported = importlib.import_module(module_name)
62
+ if __name__ == '__main__':
63
+ if verifyOnly:
64
+ pass
65
+ else:
66
+ globals()[module_name] = imported
67
+ else:
68
+ if verifyOnly:
69
+ pass
70
+ else:
71
+ stack()[1].frame.f_globals[module_name] = imported
72
+ if log: print(f'MODULE {module_name} IMPORTED SUCCESSFULLY!')
73
+ if ret: return imported
74
+ except Exception as e:
75
+ if log: print(f'[safe_import] Failed to import {module_name}: {e}')
76
+ if ret: return None
@@ -0,0 +1,13 @@
1
+ Metadata-Version: 2.4
2
+ Name: SafeImport
3
+ Version: 0.8.9
4
+ Summary: An automated package to install modules at runtime.
5
+ Requires-Python: >=3.6
6
+ Description-Content-Type: text/markdown
7
+ Dynamic: description
8
+ Dynamic: description-content-type
9
+ Dynamic: requires-python
10
+ Dynamic: summary
11
+
12
+ # Package SafeImport
13
+ Auto-generated by script
@@ -0,0 +1,5 @@
1
+ SafeImport/__init__.py,sha256=H2p5pZLN06nEX7iaBEI3-9wBwXKYjmQ_8jXMWq9YWek,1885
2
+ safeimport-0.8.9.dist-info/METADATA,sha256=i1xHSGKwf5rMUx4uvvKql5r7qdtkjbTOQ1KZ16oV3k4,335
3
+ safeimport-0.8.9.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
4
+ safeimport-0.8.9.dist-info/top_level.txt,sha256=LnlRb9jiT_U16EllITqbuuQZDh5raKy-orHIj1uzOuo,11
5
+ safeimport-0.8.9.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (83.0.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ SafeImport