2

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.

user3448011
  • 1,469
  • 1
  • 17
  • 39

1 Answers1

1

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.

Colab before restart

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.

colab after restart

anarchy
  • 3,709
  • 2
  • 16
  • 48
  • please see my updates in OP. I have tried this but it still does not work. thanks – user3448011 Aug 15 '21 at 03:57
  • Okay hold on let me try something – anarchy Aug 15 '21 at 11:32
  • @user3448011 hey i have updated my answer, i dont think you can make a change to the file like that in google drive. and you shouldnt have to. you should just create the module you need before you load the whole file. – anarchy Aug 15 '21 at 13:15
  • whenever I change book2, I have to restart book1 (it will reload snapshots of all imported modules ?) so that book1 can get the most recent snapshot of book2 ? – user3448011 Aug 15 '21 at 15:41
  • 1
    yes, u have to save the changes to book2.py and reload book1.ipydb – anarchy Aug 15 '21 at 15:42