I know this is possible, because I've done it before. For various reasons, I no longer have access to the code where I did it before.
My code looks like this:
transformationsDirectory = os.path.join('..', 'transformations')
for transformationName in requestedTransformationNames.keys():
if os.path.isdir(os.path.join(transformationsDirectory, transformationName)):
sys.stdout.write('\t' + transformationName + '...\n')
sys.stdout.flush()
sys.path.append(os.path.join(transformationsDirectory, transformationName))
mainTransformationPythonModule = importlib.import_module('Transformation')
print(str(sys.path))
print(dir(mainTransformationPythonModule))
sys.path.remove(os.path.join(transformationsDirectory, transformationName))
There are two folders in the "transformations" directory getting imported, one called "stats" and one called "strings". Both contain a file called "Transformation.py" that contains a Transformation class.
The output from running the code:
['/Users/<working directory>', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python35.zip', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/lib-dynload', '/Users/Eddie/Library/Python/3.5/lib/python/site-packages', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages', '../transformations/stats']
['AbstractTransformation', 'Output', 'Transformation', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'copy', 'os']
['/Users/<working directory>', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python35.zip', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/lib-dynload', '/Users/Eddie/Library/Python/3.5/lib/python/site-packages', '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages', '../transformations/strings']
['AbstractTransformation', 'Output', 'Transformation', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'copy', 'os']
So the output for the sys.path is correct, but the output for the loaded modules is not. They should not be the same (strings contains many more things than are listed), yet they are. From that and the behaviour of the rest of the program, it is clear that python is importing stats/Transformation.py twice instead of importing it once and strings/Transformation.py once. Why? stats isn't even in the sys.path when the second import occurs.
If I change the names of stats/Transformation.py and strings/Transformation.py to be distinct, it works. But I know it can work with them being the same (And for various reasons, I want them to be the same), yet it won't. Why?