In Google's Colab notebook, How do I call a function from a Python file?
Asked Answered
T

3

21

From a Colab notebook, I would like to call a python function that I wrote in a separate python file. How do I do that?

Tree answered 17/11, 2017 at 7:1 Comment(1)
I meant, if I am running a notebook from Google Drive and I would like to call a function from folder "../tools/my_lib.py", In Jupyter I would normally do: import sys sys.path.append("../tools/") from my_lib import my_function How do we do this in Colab?Tree
R
20

Edit: If you would like to import a local module, you'll want to edit your sys.path to point to that new directory. Here's an example notebook: https://colab.research.google.com/notebook#fileId=1PtYW0hZit-B9y4PL978kV2ppJJPhjQua

Original reply: Sure, here's an example notebook: https://colab.research.google.com/notebook#fileId=1KBrq8aAiy8vYIIUiTb5UHG9GKOdEMF3n

There are two cells: the first defines a .py file with a function to be imported.

%%writefile example.py
def f():
  print 'This is a function defined in a Python source file.'

The second cell uses execfile to evaluate that .py file in the notebook's Python interpreter.

# Bring the file into the local Python environment.
execfile('example.py')

# Call the function defined in the file.
f()
Razor answered 17/11, 2017 at 17:48 Comment(5)
Thanks! Your method does work. But, I actually meant, if I am running a notebook from Google Drive and I would to call a function from folder "../tools/my_lib.py", In Jupyter I would normally do: import sys sys.path.append("../tools/") from my_lib import my_function How do we do this in Colab?Tree
Thanks for clarifying -- I updated the response to describe how to import locally defined modules. Let me know if that doesn't clear things up.Razor
Thanks again, I think we are almost there! :) Is there a way for us to used our existed .py files from Google Drive or directly uploading it ... somehow without using the "%%writefile" magic? Or, do we have to write a script to read from Google Drive and write them into the local path?Tree
A script that synchronizes is one option. Another is to mount your Drive as a filesystem using FUSE. For more details, see my response to this questionRazor
I tried your FUSE solution. It kinda works as long as I use absolute path, but not relative path. I'll count that as working. Thanks, buddy!Tree
B
3

Please, try this function to Import a function from your drive to your colab notebook:

from google.colab import files
import zipfile, io, os

def upload_dir_file(case_f):
    # author: yasser mustafa, 21 March 2018  
    # case_f = 0 for uploading one File or Package(.py) and case_f = 1 for uploading one Zipped Directory
    uploaded = files.upload()    # to upload a Full Directory, please Zip it first (use WinZip)
    for fn in uploaded.keys():
        name = fn  #.encode('utf-8')
        #print('\nfile after encode', name)
        #name = io.BytesIO(uploaded[name])
    if case_f == 0:    # case of uploading 'One File only'
        print('\n file name: ', name)
        return name
    else:   # case of uploading a directory and its subdirectories and files
        zfile = zipfile.ZipFile(name, 'r')   # unzip the directory 
        zfile.extractall()
        for d in zfile.namelist():   # d = directory
            print('\n main directory name: ', d)
            return d
print('Done!')

Then follow the following two steps: 1- If you have a file called (package_name.py), to upload it to your colab notebook call:

file_name = upload_dir_file(0)

2- Then, import your package:

import package_name

Note: you can use the same function to: 1- uploading file(csv, excel, pdf, ....):

file_name = upload_dir_file(0)

2- uploading Directory and its subdirectories and files:

dir_name = upload_dir_file(1)

Enjoy it!

Blairblaire answered 17/11, 2017 at 7:1 Comment(0)
D
0

Bob Smith's answer couldn't be run in the colab. the simplest way is :

exec(open(filename).read())

suitable for all versions. good luck!

Divinize answered 11/8, 2020 at 11:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.