1

I want to import a module in google colab.

I did follow the instructions written here: How to import custom modules in google colab?

Nevertheless, I get a message that this module does not exist having followed these steps. I can list the file in the working directory using the ls command. Even specifying the path, where to look for the module did not help.

import sys
sys.path.append('/content/gdrive/My Drive/Colab Notebooks/calculator.py')

Calling import calculatorevaluates to a ModuleNotFoundError.

Miszka_R
  • 119
  • 1
  • 11

2 Answers2

4

You should not set module name in sys.path.append call, only directory containing custom modules:

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

import calculator
Aleksey
  • 775
  • 5
  • 14
  • Hi Aleksey, sure thing. I think that I have found my mistake, as my working directory was not the same, where the file was stored. – Miszka_R Mar 10 '20 at 19:51
0

I believe that I have found the solution. It all comes down to the working directory, which has not been correctly set :(

from google.colab import drive 
drive.mount('/content/drive')
%cd /content/drive/My Drive/Colab Notebooks

Then you can write a file, for example:

%%writefile calc.py
PI = 3.14

def add(a,b):
  return (a + b)
print (add(2,3))

def area(radius):
  return PI * radius * radius

print(area(5))

And lastly, it can be imported:

import ctes3t
Miszka_R
  • 119
  • 1
  • 11