Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. from os.path.join(path1,path2,filename) import *
  2.  
  3. from importlib import import_module
  4.  
  5. module = importmodule('.'.join([path1, path2, filename[:-3]])
  6. for attr in dir(module):
  7. globals()[attr] = getattr(module, attr)
  8.  
  9. namespace = {}
  10.  
  11. for attr in dir(module):
  12. namespace[attr] = getattr(module, attr)
  13.  
  14. def import_path(path1, path2, module_name, namespace=None):
  15. import sys
  16. from importlib import import_module
  17. from pathlib import Path # new code should use this instead of "os.path" stuff
  18. path = Path(path1).joinpath(path2)
  19. name = module_name[-3:] if module_name.endswith(".py") else module_name
  20. try:
  21. sys.path.insert(0, str(path))
  22. module = import_module(name)
  23. finally:
  24. sys.path.remove(str(path))
  25. if namespace is None:
  26. namespace = sys._getframe().f_back.f_globals()
  27. for name in dir(module):
  28. namespace[name] = getattr(module, name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement