Skip to content

Instantly share code, notes, and snippets.

@poxip poxip/loading.py

Last active Aug 29, 2015
Embed
What would you like to do?
Loading a global module in Python3
import sys
from os import getcwd
from importlib import import_module
def import_global_module(name):
old_path = sys.path
try:
sys.path.remove(getcwd())
sys.path.remove('')
except ValueError:
pass
module = import_module(name)
sys.path = old_path
return module
yourmodule = import_global_module('yourmodule')
# print(yourmodule)
# bug found: If it's imported in other script, import_global_module will load local module
# (even if sys.path doesn't contain any local paths)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.