How do I import a module and use it as if I installed it without actually installing it? More specifically, when I do imports on an installed module, all the imports are relative to the module. However when I just import it, all the imports are relative to the module it is called from.
As a specific example, in the below I would like run_import.py
to print module
not parent
. But because it imports cfg
from the parent not the module, it prints the wrong one. I would like to be able to run module1
on a standalone basis and have it isolated from any code that is calling it, much like you would in an installed package.
Directory structure is
module1/
__init__.py
cfg.py
tasks.py
run_module.py
utils.py
cfg.py
run_import.py
utils.py
Code
# cfg.py
level = 'parent'
# module1/cfg.py
level='module'
# module1/tasks.py
import cfg
def run_it():
print(cfg.level)
# module1/run_module.py
import tasks
tasks.run_it() # prints 'module'
# module1/utils.py
def util_fun():
print('util module')
# utils.py
def util_fun():
print('util parent')
# run_import.py
import module1.tasks as tasks
tasks.run_it() # prints 'parent', would like it to print 'module'
import utils
utils.util_fun() # prints util parent, should not print util module
This give me what I was looking for but has the unintended consequence that everything gets imported relative to that module
import sys
sys.path.insert(0, "module1")
tasks.run_it() # prints 'parent'
import utils
utils.util_fun() # prints util module, not util parent
Here are a couple of answers I have looked it, they didn't seem to solve it.
What's the purpose of the "__package__" attribute in Python? this sounds like what is needed but not sure how that can be set
Python3 importlib.util.spec_from_file_location with relative path? look potentially promising but i'm not sure how to use it, particularly not sure what the file path should be
How to use a packed python package without installing it
Import a module from a relative path