Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute a * .PY file from a * .IPYNB file on the Jupyter notebook?

Tags:

python

jupyter

I am working on a Python Notebook and I would like that large input code [input] pack into a [* .PY] files and call this files from the notebook.

The action of running a [.PY] file from the Notebook is known to me and the command varies between Linux or Windows. But when I do this action and execute the [.PY] file from the notebook, it does not recognize any existing library or variable loaded in the notebook (it's like the [.PY] file start from zero...).

Is there any way to fix this?

A possible simplified example of the problem would be the following:

In[1]:
import numpy as np
import matplotlib.pyplot as plt

In[2]:
def f(x):
    return np.exp(-x ** 2)

In[3]:
x = np.linspace(-1, 3, 100)

In[4]:
%run script.py

Where "script.py" has the following content:

plt.plot(x, f(x))
plt.xlabel("Eje $x$",fontsize=16)
plt.ylabel("$f(x)$",fontsize=16)
plt.title("Funcion $f(x)$")
  • In the real problem, the file [* .PY] does not have 4 lines of code, it has enough more.
like image 784
JMSH Avatar asked Feb 10 '17 15:02

JMSH


People also ask

How do I run a Python py file in a Jupyter Notebook?

2. Invoke Python Script File From Ipython Command-Line. Click the green triangle button of the python virtual environment in the Anaconda environments list, then click the Open Terminal menu item. Then go to the python script file saved directory use cd command, and then run command ipython -i list_file.py like below.

Can I run .py file in Jupyter Notebook directly?

py file from jupyter? Some simple options: Open a terminal in Jupyter, run your Python scripts in the terminal like you would in your local terminal. Make a notebook, and use %run <name of script.py> as an entry in a cell.

How do I run a .ipynb Jupyter Notebook from terminal?

To launch Jupyter Notebook App: Click on spotlight, type terminal to open a terminal window. Enter the startup folder by typing cd /some_folder_name . Type jupyter notebook to launch the Jupyter Notebook App The notebook interface will appear in a new browser window or tab.


3 Answers

In the %run magic documentation you can find:

-i run the file in IPython’s namespace instead of an empty one. This is useful if you are experimenting with code written in a text editor which depends on variables defined interactively.

Therefore, supplying -i does the trick:

%run -i 'script.py' 

The "correct" way to do it

Maybe the command above is just what you need, but with all the attention this question gets, I decided to add a few more cents to it for those who don't know how a more pythonic way would look like.
The solution above is a little hacky, and makes the code in the other file confusing (Where does this x variable come from? and what is the f function?).

I'd like to show you how to do it without actually having to execute the other file over and over again.
Just turn it into a module with its own functions and classes and then import it from your Jupyter notebook or console. This also has the advantage of making it easily reusable and jupyters contextassistant can help you with autocompletion or show you the docstring if you wrote one.
If you're constantly editing the other file, then autoreload comes to your help.

Your example would look like this:
script.py

import matplotlib.pyplot as plt  def myplot(f, x):     """     :param f: function to plot     :type f: callable     :param x: values for x     :type x: list or ndarray      Plots the function f(x).     """     # yes, you can pass functions around as if     # they were ordinary variables (they are)     plt.plot(x, f(x))     plt.xlabel("Eje $x$",fontsize=16)     plt.ylabel("$f(x)$",fontsize=16)     plt.title("Funcion $f(x)$") 

Jupyter console

In [1]: import numpy as np  In [2]: %load_ext autoreload  In [3]: %autoreload 1  In [4]: %aimport script  In [5]: def f(x):       :     return np.exp(-x ** 2)       :       :  In [6]: x = np.linspace(-1, 3, 100)  In [7]: script.myplot(f, x)  In [8]: ?script.myplot Signature: script.myplot(f, x) Docstring: :param f: function to plot :type f: callable :param x: x values :type x: list or ndarray File:      [...]\script.py Type:      function 
like image 58
swenzel Avatar answered Sep 28 '22 03:09

swenzel


the below lines would also work

!python script.py 
like image 36
braj Avatar answered Sep 28 '22 02:09

braj


Maybe not very elegant, but it does the job:

exec(open("script.py").read())
like image 44
cyprieng Avatar answered Sep 28 '22 01:09

cyprieng