Issue
I am working on some coding work with google colab ipython notebook. From book1, I need to call some functions that are defined in book2.
Book1 and book2 are all located in the same google drive and same folder.
book1:
from google.colab import drive
drive.mount('/content/drive', force_remount=True)
from kora import drive as kora_drive
kora_drive.link_nbs()
import book2 as b2
b2.function1()
book2:
def function1():
print('function1 called')
When I link the book2 from book, function1() can be called successfully. But, if I changed some code in book2, for example, added a new function in book2
def function2():
print('function2 called')
After making the changes in book2, I clicked "save" and also refreshed the drive and the folder and the book2 and book1. But, when I tried to call function2 from book1, I got error:
AttributeError: module 'book2' has no attribute 'function2'
I have tried to remount the drive and reimport the book2 and also tried the following two posts, but none of them work.
https://stackoverflow.com/questions/53358250/google-colaboratory-how-to-refresh-google-drive
https://stackoverflow.com/questions/59020008/how-to-import-functions-of-a-jupyter-notebook-into-another-jupyter-notebook-in-g
Could anyone point out what I have missed here ?
thanks
UPDTAE
In book1, I have tried
!pip install import-ipynb
import import_ipynb
import sys
sys.path.insert(1, r'/content/drive/MyDrive/Colab Notebooks/book2.ipynb')
I have also tried
sys.path.insert(1, r'/content/drive/MyDrive/Colab Notebooks/')
But, the most recent updates of "book2" still cannot be found from book1.ipynb.
Solution
Just to make sure, your functions are located in py files not ipydb files correct?
If they are in py files, you can do one of the two methods below.
You need to change the directory to where the functions are located %cd "mnt/My Drive/Pyfunctions"
. Make sure you know where the python files you are trying to import are located.
You can also achieve this using the sys library,
import sys
sys.path.insert(1, r'/mnt/My Drive/pyfunctions')
It seems that you have successfully already loaded the function the first time around, and then you make a change to it. I believe that is not how google drive works. Once you load the drive, all the files are a snapshot of the files at that point in time. Making a change will not affect the files that are already loaded.
This is what it looked like after I added the function and tried to reload the module.
The only way to make a change register is to click Runtime / Restart Runtime or CTRL + M followed by . (the fullstop) and reload the module.
I have successfully done this in my colab runtime.
Answered By - anarchy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.