I have a file somehash-mylib.py
that contains, say:
def hello():
print("hello world")
and I’d like to dynamically import this library globally (like in from mymodule import *
) using only its path. Following How can I import a module dynamically given the full path? I can do it locally using:
from pydoc import importfile
module = importfile('somehash-mylib.py')
module.hello()
but I don’t know how load it globally to turn this into:
# somehow (??) load somehash-mylib.py
hello()
Any idea? Ideally I’d like the code to be simple, and make it work across multiple python versions (only the 3.x branch).
Edit As far as I can see, none of the linked answers that justified to close this question actually answer my question: I have as input only the path to the module, and I want to include it globally. Hopefully, the comment solved my issue (thanks!):
from pydoc import importfile
module = importfile('somehash-mylib.py')
locals().update(vars(module)) # make it global
# Both work now
module.hello()
hello()