英文:
How to import a class/function in google Colab from a .py file on google drive
问题
from google.colab import drive
drive.mount('/content/drive')
%cd "/content/drive/MyDrive/Autonomous_Driving/GRTP/clstm_lifelong/us1011/sgan-nsl"
!ls
from model_wcgan_decompose import highwayNet_d
英文:
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?
答案1
得分: 1
这部分代码描述了如何打开一个文件,使用codecs
来读取它,然后使用json
加载文件内容,并将其转换为字符串以执行其中的代码。关键部分是将'execution_count'
在dict
中的值从None
更改为1
(或任何其他想要执行的次数)。以下是这个过程的描述:
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)
最后执行代码,并输出结果:
init SomeClass
如果some_file
是'SomeClass.py'
文件,代码会稍微复杂一些,因为需要找到代码的起始位置。以下是这个情况的代码:
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)
执行后,输出结果也为:
init SomeClass
无论如何,最终目标是获取正确格式的some_code_in_string
,以便执行其中的代码。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论