Reputation: 121
I am working on a python program in colab. I need to import another file here. The file is saved by the name "base_positioner.ipynb" in google drive.... I have gone through multiple resources to see how to do this import and I have done the following:
from google.colab import drive
drive.mount('/content/gdrive')
%cd /content/gdrive/My Drive
On running !ls
, I see 'base_positioner.ipynb' in the list but
still running : import base_positioner
throws the module not found error
I had also tried the following but with no success in importing the desired file:
sys.path.append('/content/gdrive/My Drive/Colab Notebooks')
What else should I try??
Upvotes: 1
Views: 4180
Reputation: 7744
This could happen if you haven't mounted your Drive on Colab to the backend properly and also possibly if your file layout in Drive is distinct from the file layout in Colab. Are you running the import command without running the following code?
from google.colab import drive
drive.mount('/content/gdrive')
%cd /content/gdrive/My Drive
If you are doing that then this won't work, as this is a pre-requisite for the mounting to take place (i.e. not running the cells sequentially). You can also try restarting Google Colab and this often fixes any strange errors.
Update:
As you mentioned, the import error likely happens due to its configuration in the main file (i.e. it requires the file to be in the .py
format to be imported just as import base_positioner
).
To import .ipynb
extension file you will need to follow the following process:
If you want to import A.ipynb
in B.ipynb
write
import import_ipynb
import A
The import_ipynb
module can be installed via pip or any other relevant ways.
pip install import_ipynb
Upvotes: 1