Photo by Glen Carrie on Unsplash

Importing Your Own Python Module or Python File into Colab

Huseyin Elci
Analytics Vidhya
Published in
2 min readJun 2, 2020

--

Importing your Python Module or Python File that you wrote for your own code into Google Colaboratory.

I was searching, is there any way to export my Python Module or Python file code (my_module.py or my_file.py) to Google Colab lines?

First of all, I researched on Google like every researcher. I looked Stack Overflow, Youtube, Medium. Yes, I found a lot of code and I tried them.
I applied the codes I found to my own code. I revised it. Unfortunately, I was not successful.

When I was giving up, I revised the code below, which I found to be a solution but doesn’t working. Yes, it’s working.

The following procedure worked for me:

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 colabfrom google.colab import drive
drive.mount('/content/drive')

Step 2

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

# Insert the directoryimport 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 fileimport my_module

Maybe there are a lot of way of solving about this question. If you advice to me other ways, please right in comment.

Also, if you want to use Colab in depth, you should definitely check out the article “Use Google Colab for Deep Learning”.

--

--