I have a module (some_module
) with a file (somefile
), containing an object I want to instantiate (foo
).
from collections import namedtuple
foo = namedtuple('Foo', ['a', 'b'])
In certain situations I only know the path to this file so have been importing like so:
import importlib.util as iu
spec = iu.spec_from_file_location("some_module.somefile", 'path/to/some_module/somefile.py')
mod = iu.module_from_spec(spec)
spec.loader.exec_module(mod)
var1 = mod.foo(1 ,2)
This worked fine at first but in my unit testing I discovered that type(var)
was not equal to the type of the original ie:
from some_module.somefile import foo
var2 = foo(1, 2)
print(type(var1) is type(var2))
print(isinstance(var1, var2.__class__)
Prints only False
's, despite the fact that it appears that both var1
& var2
are functionally equivalent.
EDIT:
I'm using python 3.9.5 and in my test code, the construction functions for var1
& var2
are in separate .py files, they get imported into a unit test which tries to check if both the objects are the same.