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 calculator
evaluates to a ModuleNotFoundError.
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.