Is there any way to upload my code in .py files and import them in colab code cells?
The other way I found is to create a local Jupyter notebook then upload it to C
A easy way is
We can do so.
import sys
import os
py_file_location = "/content/drive/My Drive"
sys.path.append(os.path.abspath(py_file_location))
Now you can import it as module in notebook for that location.
import whatever
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!
Based on the answer by Korakot Chaovavanich, I created the function below to download all files needed within a Colab instance.
from google.colab import files
def getLocalFiles():
_files = files.upload()
if len(_files) >0:
for k,v in _files.items():
open(k,'wb').write(v)
getLocalFiles()
You can then use the usual 'import' statement to import your local files in Colab. I hope this helps