how to import python library in colab code example

Example 1: how to import custom libraries in colab

#NOTE:- FILE NAMES ARE ALWAYS CASE SENSITIVE
##In colab cell,insert the following code..

from google.colab import files
uploaded = files.upload()
for file in uploaded:
  print("file name : {} , length: {}".format(file,len(file)))
  
##after you run the cell simply select the file you want to upload
#after you have selected the correct file you will see the following:
'''
chosen_file_name.py(text/x-python) - 1842 bytes, last modified: 09/06/2020 - 100% done
Saving chosen_file_name.py to chosen_file_name.py
file name : chosen_file_name.py , length:(the length of file)
'''
#Now simply write in the cell 
import chosen_file_name
#You are now done :)

Example 2: colab import python file

# In case anyone else is interested to know how to import files/packages 
# from gdrive inside a google colab. The following procedure worked for 
# me:

# 1) Mount your google drive in google colab:

from google.colab import drive
drive.mount('/content/gdrive/')

# 2) Append the directory to your python path using sys:

import sys
sys.path.append('/content/gdrive/mypythondirectory')

# Now you should be able to import stuff from that directory!