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()
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
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()