43

I have a file named imutils.py that has just one definition namely abc() which returns the sum of 2 integers.

Now I want to use this definition in a separate collab file but I am unable to.

The method I used was to first upload the file imutils.py to drive and then importing it and using the definition. The error says module 'imutils' has no attribute 'abc'

To upload I first used 2 methods : First I uploaded using the drive GUI and then I also tried the above using the code. Uploading in both cases was successful

from google.colab import files
files.upload() 
  • So you are saying that the upload was successful however you still can't execute imutils.py? Can you show the output of `!ls` – jar Oct 10 '18 at 06:43
  • While you are at it, does ```!python imutils.py``` execute without errors? – jar Oct 10 '18 at 07:02
  • 1
    Many more solutions can be found [here](https://stackoverflow.com/questions/48905127/importing-py-files-in-google-colab?noredirect=1&lq=1). I found the `!git clone https://github.com/username/repo_name.git.` from [Aditya Mishra](https://stackoverflow.com/a/49452931/6329629) quite useful. – Roald Apr 12 '20 at 14:01

2 Answers2

57

If your Python file is in Drive, it's likely simpler to mount your Drive than to upload the file, e.g.,

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

Then, if you have a module, you can import it like so:

https://colab.research.google.com/drive/1uvHuizCBqFgvbCwEhK7FvU8JW0AfxgJw

Contents of the Notebook follow:

Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdocs.test%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.photos.readonly%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fpeopleapi.readonly&response_type=code

Enter your authorization code:

··········

Mounted at /content/gdrive

I happen to have an existing .py file in Drive.

!ls /content/gdrive/My\ Drive/*.py
>>> /content/gdrive/My Drive/mylib.py

!cat '/content/gdrive/My Drive/mylib.py'

def MyFunction():
    print ('My imported function')

# We'll need to update our path to import from Drive.

import sys
sys.path.append('/content/gdrive/My Drive')

# Now we can import the library and use the function.

import mylib
mylib.MyFunction()
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Bob Smith
  • 36,107
  • 11
  • 98
  • 91
  • 1
    I tried doing the same for folders I `!git clone` 'd into Colab but it's not working... – mLstudent33 Jun 02 '19 at 10:42
  • 1
    Just make sure to edit the module / libs with a notepad editor, as the colab editor adds lot of metadata and make the import not to find the functions – Gabriel Jun 24 '19 at 15:05
2

Instead of updating the path, I find directly copying the custom module into the Colab temporary drive, say, '/content', much simpler and faster. Since your custom module is in Google Drive, you of course first need to mount your Google Drive before you copy the custom module into your Colab temporary drive. If you include the following steps in your Colab notebook before you import your custom module, then everything will work.

# Mount your google drive in google colab
from google.colab import drive
drive.mount('/content/drive')

# Check your Colab temporary path
!pwd # Returns /content 

# Copy custom module from Google Drive to Colab temporary drive 
# !cp [path of your custom module] [path where you like to copy]
! cp /content/drive/MyDrive/ColabNotebooks/mymodule.py /content/mymodule.py

That's it. If you are now in Colab temporary drive, then you can import your custom module as follows:

import mymodule
Sonjoy Das
  • 121
  • 1
  • 4