user61244 user61244 - 6 months ago 11
Python Question

executing .py file in IPython

Is there a way to run a

file containing functions etc. from within the notebook such that the content of the
file is treated as if it were directly in that cell?

For instance, say I have the file
in which a function
is defined that calls a function
which is defined in the ipython notebook. If I run
is unkown to
. Can this be circumvented?


In Python2 the execfile function would do what you want. In Python 3 you have to emulate that by reading the contents of the file, compiling it and then making sure that it executes in the right namespace.

In a notebook cell I defined:

def f4(x):
    return x+x

I have a file containing the following code:


The following code cell appears to correctly execute the code in the file:

with open("") as f:
    code = compile(, "", 'exec')
    exec(code, globals(), locals())

It does indeed print "somethingsomething" as expected.

As mentioned in the comments, for debugging a changed module you can re-import it with reload() (Python2) or importlib.reload(). But the coupling of your code is rather tight - it's always going to be looking for the same function in the global namespace. This makes it very difficult to test, and it might be better to consider a dependency injection approach. Suppose your function looks as follows:

def my_func(a, b):
    return result

This has to find the f4 function in its global namespace. A better-coupled solution would look like this:

def my_func(a, b, f):
    return result

You then call my_func with f4 as its third argument - you are in absolute control over the function that it calls, and can use other functions during testing should you want to instrument the code more carefully, and your code no longer requires the function to be in the global namespace of the calling module - you can pass it any function that you like.