-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimport-module-example.py
42 lines (32 loc) · 1.42 KB
/
import-module-example.py
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
"""
Example of loading my own module from an arbitrary location
with minimum of lint complaints.
"""
import importlib.util
import os
def load_my_module( module_name, relative_path ):
"""
Load a module in my own single .py file. Requires Python 3.6+
Give the name of the module, not the file name.
Give the path to the module relative to the calling program.
Requires:
import importlib.util
import os
Use like this:
readgedcom = load_my_module( 'readgedcom', '../libs' )
data = readgedcom.read_file( input-file )
"""
assert isinstance( module_name, str ), 'Non-string passed as module name'
assert isinstance( relative_path, str ), 'Non-string passed as relative path'
file_path = os.path.dirname( os.path.realpath( __file__ ) )
file_path += os.path.sep + relative_path
file_path += os.path.sep + module_name + '.py'
assert os.path.isfile( file_path ), 'Module file not found at ' + str(file_path)
module_spec = importlib.util.spec_from_file_location( module_name, file_path )
my_module = importlib.util.module_from_spec( module_spec )
module_spec.loader.exec_module( my_module )
return my_module
# relative to program making the call, ../libs
readgedcom = load_my_module( 'readgedcom', '..' + os.path.sep + 'libs' )
print( 'indi key=', readgedcom.PARSED_INDI )
print( 'fam key=', readgedcom.PARSED_FAM )