Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import module in Google Colab from google drive - python

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

like image 405
Paweł Magdański Avatar asked May 31 '20 14:05

Paweł Magdański


People also ask

How do you import from Google Drive in Colab?

You can import files module from google. colab . Then call upload() to launch a “File Upload” dialog and select the file(s) you wish to upload. Once the upload is complete, your file(s) should appear in “Files explorer” and you can read the file as you would normally.

Can Google colab read file from drive?

Since a Colab notebook is hosted on Google's cloud servers, there's no direct access to files on your local drive (unlike a notebook hosted on your machine) or any other environment by default. However, Colab provides various options to connect to almost any data source you can imagine.

How do I import files into Google Colab?

The simplest way to share files is to mount your Google Drive. It will ask you to visit a link to ALLOW "Google Files Stream" to access your drive. After that a long alphanumeric auth code will be shown that needs to be entered in your Colab's notebook.


Video Answer


1 Answers

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
like image 60
H.Elci Avatar answered Sep 22 '22 01:09

H.Elci