GSI Object Oriented Online Offline (Go4)  GO4-5.3.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
reimport.py
Go to the documentation of this file.
1 
2 import sys
3 
4 
5 # Try to get a reload function:
6 try:
7  # Built-in (Python 2) or imported before
8  # allow it to be imported from here by assigning it locally
9  reload = reload
10 except NameError:
11  # Python 3
12  try:
13  # New in 3.1, not in 2.7
14  from importlib import reload
15  except ImportError:
16  # Deprecated since 3.4
17  from imp import reload
18 
19 
20 
21 def reimport(modulename):
22  """
23  Reloads modulename if it was imported before, imports it otherwise.
24 
25  Workaround for some implications of the otherwise very useful fact
26  that TPython's interpreter state is preserved in between calls:
27  Assume a main script and a library module. Once the script has
28  imported the library, changes to the library are not considered
29  anymore. A cached version is used instead. To see changes a reload
30  is necessary. This function should simply do the right thing...
31  """
32  try:
33  m = sys.modules[modulename]
34  except KeyError:
35  m = __import__(modulename)
36  else:
37  reload(m)
38  finally:
39  return m
40 
41 
42 
43 # Alternatively the module can be deleted to trigger a clean import:
44 
45 #if 'myModule' in sys.modules:
46 # del sys.modules["myModule"]
47 
48 #for mod in sys.modules.keys():
49 # if mod.startswith('myModule.'):
50 # del sys.modules[mod]
51 
52 
53