It's a good practice to keep your code modular to enhance its value and applicability to a wider range of use cases.
It also helps to make your Python script shorter, reduces clutter and easier to understand. Readers of your code only need to know the purpose of you calling the modular functions and what it outputs rather than how the function works (they can dive deeper themselves).
In regular Python programming, importing your module (i.e. the separate Python file housing the variables, classes or functions) is as easy as doing an import statement, while making sure the module is in your working directory.
For Colaboratory, it's not as straightforward as the working directory is not very obvious. However, there's an easy trick to import your module onto your Colab notebook.
UPDATE: Please find below the updated, easier method to import your module. Both methods still work.
First of all, you need to authenticate Google Drive accessing via Colaboratory.
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
This piece of code is a standard for any accessing of Google Drive files. In fact, this code snippet is accessible from the left sidebar of Colaboratory.
Next, we have to start linking the particular Google Drive file to your Colab notebook.
To do that, you execute the following code:
your_module = drive.CreateFile({'id':'google_drive_file_id'})
The google_drive_file_id is your Google Drive's file's sharing ID. It can be found by right-clicking your file in Google Drive and clicking on the "Get shareable link" option.
Next, enable the Link Sharing option, and the file ID will be in the link that will appear.
Copy the link and remove the front part of the link (i.e. https://drive.google.com/open?id=) to get the file ID.
You're almost there. The next step is to execute the following code after replacing "module_name.py" with your module's name (i.e. file name).
your_module.GetContentFile('module_name.py')
Once you've done all these steps, you can now import your module into your Colab notebook! Simply execute an import statement to do that.
import module_name # or
# from module_name import some_class_or_function
You're all set to go.
There are more steps here than simply moving the file into your working directory and executing the import statement as you would in a regular Python environment.
However, Colaboratory has a few advantages as I had mentioned here that will be really useful if your code is to be used widely by your team. So have fun exploring Colaboratory!
There's an easier method to do this now. First, perform authentication using this code.
from google.colab import drive
drive.mount('/content/drive')
After copying the authentication code and pasting in Colab, next you'll need to find the full path to the folder that contains the module.
To do that, open the sidebar by clicking the Folder icon (third icon), then navigate under "drive" to your file.
Click the triple-dot icon at the end of the folder name and click "Copy path". The sidebar's folder view will be available once you connect to a runtime and your drive should appear after running drive.mount
Once done, paste the full path into the code below and run it.
import sys
sys.path.append('/content/drive/My Drive/full_path')
After that, you can import as usual.
import module_name # or
# from module_name import some_class_or_function
Like the post? Consider donating to fund the maintenance of this website: