Want to save & run Python script in Jupyter Notebook (Anaconda)

19,309

Solution 1

Make sure your ipython notebook is in the same folder as your python script. Also, you may have to create an empty __init__.py file in the same folder as your python script to make the import work.

Since you will probably be modifying your python script and test it directly on your notebook, you may be interested in the autoreload plugin, which will automatically update the imported modules with the changes you have just made in your python scripts:

%load_ext autoreload
%autoreload 2

Note that you need to place your imports after having called the autoreload plugin.

Note also that in some cases you may need to add this in your IPython notebook at the very top of the first cell (after the % magics):

from __future__ import absolute_import

Limitations: autoreload works well in general to reload any part of a module's code, but there are some exceptions, such as on new class methods: if you add or change the name of a method, you have to reload the kernel! Else it will continue to either load the old definition of this method or fail in the resolution (method not found), which can be particularly confusing when you are overloading magic methods (so in this case the default magic method will be called instead of your definition!). Then once the method name is defined and the kernel reloaded, you can freely modify the code of this method, the new code will be automagically reloaded.

Also it will fail if you use super() calls in the classes you change, you will also have to reload the kernel if this happens.

Solution 2

Oh I found the solution what I want.

In Jupyter Notebook, you can transfer a cell into python script directly.

I found it on ipython tutorial, you can refer this [.ipynb file]

By executing below cell in your notebook, you can directly transfer your

code into .py file in the same directory.

%%writefile textproc.py

def plural(word):
    if word.endswith('y'):
        return word[:-1] + 'ies'
    elif word[-1] in 'sx' or word[-2:] in ['sh', 'ch']:
        return word + 'es'
    elif word.endswith('an'):
        return word[:-2] + 'en'
    else:
        return word + 's'

Then, you can retrieve it by importing this .py file like this.

from textproc import plural
plural('wish')

However, I couldn't figure out how to access any .py file saved in another directory (Importing procedure .. or etc)

Share:
19,309
SUNDONG
Author by

SUNDONG

Updated on June 21, 2022

Comments

  • SUNDONG
    SUNDONG almost 2 years

    Another python newbie here. Currently, I'm using Jupypter notebook in anaconda framework.

    In order to proceed my projects using iPython Notebook,

    I need to run some of my python scripts (tp.py file) on the notebook.

    from tp import wordtoplural  
    

    Since, it makes life a lot easier instead of defining all function in notebook itself.

    How can I do it, currently importerrors occurs on my code.

    ImportError: cannot import name wordtoplural
    

    1. iPython notebook and python script(.py) are in the same folder.
    2. Added empty __init.py__file on that directory too.
  • gaborous
    gaborous over 8 years
    You need to add a __init__.py file into each diretory where there are .py files in order to be able to import them (adding this file transform the directory into a python package).
  • gaborous
    gaborous over 8 years
    @RovinBhandari try to 1- restart the kernel, 2- execute the autoreload code only, without any other code in the cell, 3- execute your code with your imports. You then need to re-execute the import everytime you want to update your script, the autoreload will then kick in automatically to force the re-import.