13

I have a problem with import ipynb file from my google drive. I've tried solutions from: Importing .py files in Google Colab How to import custom modules in google colab?

but does not work for me.

# COLAB
from google.colab import files
from google.colab import drive
# SYS
import sys
# IPYNB
!pip install import-ipynb
import import_ipynb
# UTIL
import importlib.util

I've tried something like this:

drive.mount('/content/drive')
sys.path.append('/content/drive/My Drive/Colab Notebooks/')
import Data_Preparation_Library

Or this:

!cp "/content/drive/My Drive/Colab Notebooks/Data_Preparation_Library.ipynb"
import Data_Preparation_Library

Here is how my gdrive structure looks like:

enter image description here

Thank You for answers in advance

Paweł Magdański
  • 189
  • 2
  • 3
  • 11
  • Here is an error: ModuleNotFoundError Traceback (most recent call last) in () ----> 1 import Data_Preparation_Library ModuleNotFoundError: No module named 'Data_Preparation_Library' – Paweł Magdański May 31 '20 at 14:14
  • It is not clear what are you trying to ask. If you need to import ipynb file you can do it directly via colab as colab save all the files in google drive. Just click on the file and then open notebook. – tanmayjain69 May 31 '20 at 14:20
  • 1
    I've tried to import another file as module, so I can use classes and methods in it. – Paweł Magdański May 31 '20 at 14:33
  • Does this answer your question? [How to import functions of a jupyter notebook into another jupyter notebook in Google Colab](https://stackoverflow.com/questions/59020008/how-to-import-functions-of-a-jupyter-notebook-into-another-jupyter-notebook-in-g) – Hoa Nguyen Jun 01 '20 at 20:25
  • Hoa Nguyen I've already found a solution, thank You for your answer – Paweł Magdański Jun 07 '20 at 21:16

4 Answers4

13

SOURCE

Step 1 Primarily, you must Mount your google drive in google colab: Code to below, your files on your google drive is import files/packages inside a google colab.

# Mount your google drive in google colab
from google.colab import drive
drive.mount('/content/drive')

Step 2 Secondly, insert the directory to your python path using sys:

# Insert the directory
import sys
sys.path.insert(0,’/content/drive/My Drive/ColabNotebooks’)

Step 3 Now, you can be able to import your module or file stuff from that directory.

# Import your module or file
import my_module
H.Elci
  • 216
  • 1
  • 5
4

Worked version of Elci's solution for me:

# Mount your google drive in google colab
from google.colab import drive
drive.mount('/content/gdrive')

# Insert the directory
import sys
sys.path.insert(0,'/content/gdrive/My Drive/Colab Notebooks')

# Import your module or file
import my_module
Yusufmet
  • 135
  • 1
  • 1
  • 6
0

Huseyin Elci, I found a solution which worked for me:

drive.mount('/content/drive')
sys.path.append('/content/drive/My Drive/Colab Notebooks/')
!cp -r "/content/drive/My Drive/Colab Notebooks/Data_Preparation_Library.ipynb" '/content/'

But I will try your approach Thank You for answer

Paweł Magdański
  • 189
  • 2
  • 3
  • 11
0
import sys    

path_to_module = '/content/gdrive/My Drive/tmp'
sys.path.append(path_to_module)
print(sys.path)
import tmp
eshirvana
  • 23,227
  • 3
  • 22
  • 38
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 12 '21 at 06:03