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 Colab, is it the only way?
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 Colab, is it the only way?
You can save it first, then import it.
from google.colab import files
src = list(files.upload().values())[0]
open('mylib.py','wb').write(src)
import mylib
Update (nov 18): Now you can upload easily by
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
%load filename.py
.Try this way:
I have a package named plant_seedlings. The package is stored in google drive. What I should do is to copy this package in /usr/local/lib/python3.6/dist-packages/.
!cp /content/drive/ai/plant_seedlings.tar.gz /usr/local/lib/python3.6/dist-packages/
!cd /usr/local/lib/python3.6/dist-packages/ && tar -xzf plant_seedlings.tar.gz
!cd /content
!python -m plant_seedlings
You can upload those .py files to Google drive and allow Colab to use to them:
!mkdir -p drive
!google-drive-ocamlfuse drive
All your files and folders in root folder will be in drive
.