I have several Python packages that I'd like to keep on separate filesystems but which unfortunately share the same top-level module name.
To illustrate, the directory structure looks like this:
/fs1
/top
__init__.py
/sub1
__init__.py
/fs2
/top
__init__.py
/sub2
__init__.py
In Python 2.7, is there any way I can set up my PYTHONPATH so that I could import both top.sub1 and top.sub2 into the same script? Adding both /fs1 and /fs2 doesn't work, since it only allows one of the two submodules to be imported (whichever comes first on PYTHONPATH).
I could copy/symlink the two trees into one, but for practical reasons I'd rather not do that.
There are several options, one of which is imp:
import imp
foo = imp.load_source('module.name', '/path/to/file.py')
foo.MyClass()
(my source)
Another is with importlib
Relative:
importlib.import_module('.sub1', 'fs1.top')
Absolute:
importlib.import_module('fs1.top.sub1')
(my source)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With