之前整理的几种方法,mark一下。
### how to import my custom module (eg. import helper.py)
# solution1
import importlib.machinery
loader = importlib.machinery.SourceFileLoader( 'helper', '/content/drive/MyDrive/Colab Notebooks/helper.py')
helper = loader.load_module()
from helper import unregularized_model
# solution2
import importlib.machinery
import importlib.util
loader = importlib.machinery.SourceFileLoader( 'helper', '/content/drive/MyDrive/Colab Notebooks/helper.py' )
spec = importlib.util.spec_from_loader('helper',loader)
mymodule = importlib.util.module_from_spec(spec)
loader.exec_module(helper)
# Add system path(It’s temporary!)
# Not necessary but I don’t know what it is :(
import sys
sys.path.append('/content/drive/MyDrive/Colab Notebooks/helper.py')
sys.path.remove(path)