-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnotebook_utils.py
More file actions
41 lines (33 loc) · 917 Bytes
/
notebook_utils.py
File metadata and controls
41 lines (33 loc) · 917 Bytes
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
import importlib
import sys
def import_module_by_name(mod_name):
"""
Executes an "import" statement dynamically.
Parameters
----------
mod_name : str
Name of the module to be imported.
Returns
-------
module
The imported module.
"""
return importlib.__import__(mod_name)
def reload_module_by_name(mod_name, var_name):
"""
Deletes and reimports a module (hot-module reloading).
Parameters
----------
mod_name : str
Name of the module to be reloaded (e.g. 'numpy')
var_name : str
Name of the variable referencing the module (e.g. 'np')
Returns
-------
"""
for mod in list(sys.modules.keys()):
if mod_name in mod:
del sys.modules[mod]
if var_name in globals():
del globals()[var_name] # deletes the variable named <var_name>
return importlib.__import__(mod_name)