Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
204 views
in Technique[技术] by (71.8m points)

python 3.x - How do I import local modules stored in my Google Drive to Google Collab?

I did connect Google Collab with my Google Drive

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

and I can already see my local Google Drive folders and files

pwd
>> /content

ls drive/MyDrive/
>> MyFolder

But now, how can I import local modules I have installed within /MyFolder/SubFolder/Source? Will I have to mark all the directories in-between as python modules adding __init__.py to all of them?

Currently, my notebook is located within /MyFolder, so I can easily import my modules with

from SubFolder.source.mypersonalmodule import *

ALTERNATIVELY

Is there a way to run my notebook from content/drive/MyDrive/MyFolder/?

Thank you for your help!

question from:https://stackoverflow.com/questions/66055111/how-do-i-import-local-modules-stored-in-my-google-drive-to-google-collab

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just specify where your file is starting from drive.MyDrive.

For example, if I had a file test_function.py in the root of my Google Drive with the function square in it, I can import it by

from drive.MyDrive.test_function import square

Example image of execution and printing of paths:

Colab notebook example

And yes, you are able to run your Jupyter Notebooks from anywhere in your Google Drive. Just find the file, click on it, and click on "Open with Google Colaboratory" at the top when Google says there is "No preview available".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...