Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use code from external Python files in Jupyter notebook?

I have a pet project which I started as a Jupyter notebook. So far, I put all the Python code in the notebook.

At the start everything was fine. But over time the code I wrote in the notebook became more and more complex. Now it is close to unmanagenable: When I find an error, I need

  1. to navigate to the code part with the error (usually at the beginning of the notebook),
  2. fix the error there,
  3. go to (usually) the bottom of the notebook to trigger the execution of the code I changed.

I want to separate the code in two parts:

  1. One that will be stored as Python files and which I will edit using an editor (and/or an IDE).
  2. The code in the Jupyter notebook that calls code parts 1 and presents its output (i. e. use the Jupyter notebook as a user interface for the Python code from step 1).

Let's assume that the notebook runs on my local machine (Windows 7; Jupyter runs in Anaconda) and the Python files are also stored locally.

What are good ways to use code from IPython files such that I can modify this code frequently and fast?

By "frequently and fast" I mean "with as little steps as possible that are necessary to propagate changes from Python files to the notebook". The ideal solution would be something where I change one of the Python files, run one command, and then the changes are available in the Jupyter notebook. Or, to use an older analogy, I want it to be like PHP -- you change the code often and immediately see the results of your changes.

Update 1: I tried to use the solution with %load TestClass.py in a cell.

The problem is that the cell contents is not updated, if the file changes.

Example:

Let's say I put the text

class TestClass:
    def __init__(self):
        print("TestClass constructor")

into TestClass.py. Then I create a cell in Jupyter notebook with %load TestClass.py. When I execute that cell, the code from TestClass.py is imported and the line %load TestClass.py gets commented out.

Now I change TestClass.py to

class TestClass:
    def __init__(self):
        print("TestClass constructor")
        print("change")

When I execute the cell, its contents has not changed.

like image 276
Dmitrii Pisarenko Avatar asked Jun 06 '19 15:06

Dmitrii Pisarenko


People also ask

How do I run an existing Python code in a Jupyter Notebook?

Inside the Notebook. When you open a new Jupyter notebook, you'll notice that it contains a cell. Cells are how notebooks are structured and are the areas where you write your code. To run a piece of code, click on the cell to select it, then press SHIFT+ENTER or press the play button in the toolbar above.


1 Answers

Sounds like the autoload extension of IPython is exactly what you need. Simply plug

%load_ext autoreload
%autoreload 2

in one of the first cells of your Jupyter notebook and imported python modules are automatically reloaded on change. You also can do changes to installed python packages, provided you have installed them editable.

like image 121
Tobias Windisch Avatar answered Sep 29 '22 17:09

Tobias Windisch