Commit 954753b
committed
replace imp module with importlib
When trying to run cloudbase-init using Python 3.12, it errors out
ModuleNotFoundError: No module named 'imp'.
The 'imp' module was replaced with similar functionality from module
importlib.
These two implementations are equivalent:
```python
import imp
import os
import site
wmi_path = None
for packages_path in site.getsitepackages():
path = os.path.join(packages_path, "wmi.py")
if os.path.isfile(path):
wmi_path = path
break
wmi_module_name = "wmi"
wmi_module = imp.load_source(wmi_module_name, wmi_path)
```
```python
import importlib.util
import os
import site
wmi_path = None
for packages_path in site.getsitepackages():
path = os.path.join(packages_path, "wmi.py")
if os.path.isfile(path):
wmi_path = path
break
wmi_module_name = "wmi"
wmi_module_spec = importlib.util.spec_from_file_location(wmi_module_name, wmi_path)
wmi_module = importlib.util.module_from_spec(wmi_module_spec)
wmi_module_spec.loader.exec_module(wmi_module)
```
Fixes: #139
Change-Id: I6490c6d9922efea26ab8d167a0d6e41ce34d6c2c
Signed-off-by: Adrian Vladu <avladu@cloudbasesolutions.com>1 parent 6c1dc5b commit 954753b
4 files changed
Lines changed: 21 additions & 15 deletions
File tree
- cloudbaseinit
- tests/utils
- windows
- utils
- windows
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
38 | | - | |
39 | | - | |
40 | | - | |
| 38 | + | |
| 39 | + | |
41 | 40 | | |
42 | 41 | | |
43 | | - | |
| 42 | + | |
44 | 43 | | |
45 | 44 | | |
46 | 45 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
| 35 | + | |
36 | 36 | | |
37 | | - | |
| 37 | + | |
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | | - | |
| 43 | + | |
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
| 51 | + | |
| 52 | + | |
52 | 53 | | |
53 | 54 | | |
54 | 55 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
24 | 32 | | |
25 | 33 | | |
26 | 34 | | |
| |||
32 | 40 | | |
33 | 41 | | |
34 | 42 | | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
| 43 | + | |
| 44 | + | |
39 | 45 | | |
40 | 46 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
16 | 15 | | |
17 | 16 | | |
18 | 17 | | |
19 | 18 | | |
20 | 19 | | |
21 | 20 | | |
| 21 | + | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
44 | | - | |
| 44 | + | |
0 commit comments