How to import a class/function in google Colab from a .py file on google drive
Question:
I am trying to import a class from a .py file which is stored in my google drive.
Initially, I have mounted google drive as follows:
from google.colab import drive
drive.mount('/content/drive')
Then navigate into the destination folder using:
%cd "/content/drive/MyDrive/Autonomous_Driving/GRTP/clstm_lifelong/us1011/sgan-nsl"
!ls
It shows output like:
/content/drive/MyDrive/Autonomous_Driving/GRTP/clstm_lifelong/us1011/sgan-nsl
checkpoint.pkl __pycache__ utils_wcgan_decompose.py
loss.mat trained_models
model_wcgan_decompose.py train_wcgan_decompose.py
Now in that model_wcgan_decompose.py
file of the drive there was some class named highwayNet_d
, highwayNet_g_compose
, highwayNet
. Now I am trying to import the class for my purpose using:
from model_wcgan_decompose import highwayNet_d
but it shows error like:
ImportError Traceback (most recent call last)
<ipython-input-26-256c2191a0a5> in <cell line: 9>()
7 #import model_wcgan_decompose
8
----> 9 from model_wcgan_decompose import highwayNet_d
10
11
ImportError: cannot import name 'highwayNet_d' from 'model_wcgan_decompose' (/content/drive/MyDrive/Autonomous_Driving/GRTP/clstm_lifelong/us1011/sgan-nsl/model_wcgan_decompose.py)
Please suggest how can I fix it?
Answers:
We can open some_file
of code with codecs
, use json
to load
it, and make it str
ing so we can exec
ute some_code_in_string
; the important part is that we change the 'execution_count'
in its dict
ionary from None
to 1
(or however many times we want it to exec
ute the code).
So here in 'SomeClass.ipynb'
I have the following code:
class SomeClass:
def __init__(self):
print(f'init {self.__class__.__name__}')
self.foo = 0
SomeClass()
In another file, we can run:
import codecs
import json
some_file = codecs.open('/content/drive/My Drive/Colab Notebooks/SomeClass.ipynb', 'r')
some_file = some_file.read()
some_file = json.loads(some_file)
some_file['cells'][0]['execution_count'] = 1
some_code_in_string = ''
for code_in_file in some_file['cells']:
for _code_in_file in code_in_file['source']:
some_code_in_string += _code_in_file
some_code_in_string += 'n'
exec(some_code_in_string)
Outputs:
init SomeClass
Now if some_file
is 'SomeClass.py'
it’s a bit trickier since we have to find
where the first line of code actually begins:
import codecs
some_file = codecs.open('/content/drive/My Drive/Colab Notebooks/SomeClass.py', 'r')
some_file = some_file.read()
some_code_in_string = some_file[some_file[some_file.find('https'):].find('"') + 3 + some_file.find('https') + some_file[some_file[some_file.find('https'):].find('"') + 3 + some_file.find('https')].find(' ') + 3:]
exec(some_code_in_string)
Outputs:
init SomeClass
Either way, the goal is to get some_code_in_string
correctly formatted like:
some_code_in_string = '''
class SomeClass:
def __init__(self):
print(f'init {self.__class__.__name__}')
self.foo = 0
SomeClass()
'''
so that we can exec
ute it.
I am trying to import a class from a .py file which is stored in my google drive.
Initially, I have mounted google drive as follows:
from google.colab import drive
drive.mount('/content/drive')
Then navigate into the destination folder using:
%cd "/content/drive/MyDrive/Autonomous_Driving/GRTP/clstm_lifelong/us1011/sgan-nsl"
!ls
It shows output like:
/content/drive/MyDrive/Autonomous_Driving/GRTP/clstm_lifelong/us1011/sgan-nsl
checkpoint.pkl __pycache__ utils_wcgan_decompose.py
loss.mat trained_models
model_wcgan_decompose.py train_wcgan_decompose.py
Now in that model_wcgan_decompose.py
file of the drive there was some class named highwayNet_d
, highwayNet_g_compose
, highwayNet
. Now I am trying to import the class for my purpose using:
from model_wcgan_decompose import highwayNet_d
but it shows error like:
ImportError Traceback (most recent call last)
<ipython-input-26-256c2191a0a5> in <cell line: 9>()
7 #import model_wcgan_decompose
8
----> 9 from model_wcgan_decompose import highwayNet_d
10
11
ImportError: cannot import name 'highwayNet_d' from 'model_wcgan_decompose' (/content/drive/MyDrive/Autonomous_Driving/GRTP/clstm_lifelong/us1011/sgan-nsl/model_wcgan_decompose.py)
Please suggest how can I fix it?
We can open some_file
of code with codecs
, use json
to load
it, and make it str
ing so we can exec
ute some_code_in_string
; the important part is that we change the 'execution_count'
in its dict
ionary from None
to 1
(or however many times we want it to exec
ute the code).
So here in 'SomeClass.ipynb'
I have the following code:
class SomeClass:
def __init__(self):
print(f'init {self.__class__.__name__}')
self.foo = 0
SomeClass()
In another file, we can run:
import codecs
import json
some_file = codecs.open('/content/drive/My Drive/Colab Notebooks/SomeClass.ipynb', 'r')
some_file = some_file.read()
some_file = json.loads(some_file)
some_file['cells'][0]['execution_count'] = 1
some_code_in_string = ''
for code_in_file in some_file['cells']:
for _code_in_file in code_in_file['source']:
some_code_in_string += _code_in_file
some_code_in_string += 'n'
exec(some_code_in_string)
Outputs:
init SomeClass
Now if some_file
is 'SomeClass.py'
it’s a bit trickier since we have to find
where the first line of code actually begins:
import codecs
some_file = codecs.open('/content/drive/My Drive/Colab Notebooks/SomeClass.py', 'r')
some_file = some_file.read()
some_code_in_string = some_file[some_file[some_file.find('https'):].find('"') + 3 + some_file.find('https') + some_file[some_file[some_file.find('https'):].find('"') + 3 + some_file.find('https')].find(' ') + 3:]
exec(some_code_in_string)
Outputs:
init SomeClass
Either way, the goal is to get some_code_in_string
correctly formatted like:
some_code_in_string = '''
class SomeClass:
def __init__(self):
print(f'init {self.__class__.__name__}')
self.foo = 0
SomeClass()
'''
so that we can exec
ute it.