How to import a module with absolute path and pass parameter to that module so that the called module can actually use that variable inside it? By absolute path I mean something like this "/home/example.com/directory/file.py"
. I get this module name as part of command line argument to my actual script. Inside that script I have to import file.py
and pass certain arguments to them.
Ex Code:
filename = '/home/example.com/directories/file.py'
path = re.split(filename, script_path, maxsplit=1)[0]
print(path) --> /home/example.py/directories/
module_name = filename.rstrip('.py')
print(module_name) --> file.py
spec = importlib.util.spec_from_file_location(module_name, path)
module = importlib.util.module_from_spec(spec)
print(type(module))
# filepath = re.sub('/','.',script_path)
# print(filepath)
# module = importlib.import_module(script_path)
module.testvars = [r_config, t_config]
# spec.loader.exec_module(module)
alltest = unittest.TestSuite()
alltest.addTest(unittest.findTestCases(module))
runner = unittest.TextTestRunner()
But I get the following error with this code:
Traceback (most recent call last): module = importlib.util.module_from_spec(spec)
File "<frozen importlib._bootstrap>", line 568, in module_from_spec
AttributeError: 'NoneType' object has no attribute 'loader'