The Jupyter notebook is also a very handy Python Web IDE, in which, besides creating and running .ipynb files, one can create Python (.py) files, display and run them very conveniently. I showed how to create a Python (.py) file in the Jupyter notebook using different methods in a previous post. I will continue to show to how to display, edit, run Python (.py) file and an external .ipynb file in Jupyter note in this post.
1. How to Display a .py
file in Jupyter Notebook
There are three commands, namely !cat
, %cat
, cat
to show a Python code snippet in a code cell in Jupyter notebook on the Linux system, while we use !type
on the Widows’ system to do the same work. For example, let’s create a hello.py
file first and put the following code snippet in the file. If you are not sure how to create it, please read the post. Then put the file in the working directory.
def hello(name):
print(f"Hello, {name}!")
On Linux System
Type one of the following codes in a code cell of Jupyter notebook and then run it.
!cat hello.py
Or
%cat hello.py
Or
cat hello.py
The output looks as follows:
For Windows Sytem:
Just use the following command code instead, we will get the similar output as above.
!type hello.py
Besides, there is another magic command %pycat
, which works for both Linux and Windows. Whereas, Jupyter opens a pager and prints the output there.
It is good to know all these commands; but I suggest you using %cat
on Linux and !type
on Windows.
2. Load .py
File
The magics %load
and %loadpy
are used to load a .py
file into the current code cell, and then you can edit and run it.
For example, let’s load the “hello.py” file in the working directory, just run either of the following commands.
%load hello.py
or
%loadpy hello
After running the command, Jupyter notebook loads python code immediately in the cell as follows, and then you edit it.
3. Append to .py
File
The magic command %%writefile -a
can be used to append codes to the end of the code in an existing file. For example, we want to add hello("Sigmund")
at the end of the function.
The above output of Appending to hello.py indicates code appending is successful. We can check the code by %cat
, !cat (Linux) or !type
(Windows).
4. Update .py
File
If you would like to add code or edit the code in the file rather than to append code to the end, you can run %%writefile
again, which will update your code by overwriting the old file. For example,
5. Run .py
File
To run the Python file, we use %run filename.py
. For example, let’s run “hello.py” by:
%run hello.py
or
run hello.py
The output looks as follows:
For a function, there are other two convenient ways to run it.
Method 1: run the function, and then call it
# run the function
%run greeting.py
Then
# call the function
hello("Sigmund")
Method 2: run the function as module
# import the function as a module
import greeting
# call the function
greeting.hello("Sigmund")
6. Run External Jupyter Notebook .ipynb
files
Besides, we can also run another Jupyter notebook inside the current working on using%run filename.ipynb
For example, let’s create a Jupyter notebook named “myname.ipynb” and run it in a new Jupyter notebook.
7. Run Codes from a Python or IPython Shell Session
Another awesome feature of the Jupyter Notebook is that you can run scripts directly copied from a Python shell or IPython shell session. For example:
(1) Code from a Python Shell session: https://docs.python.org/3/tutorial/interpreter.html
(2) Code from a IPython Shell session: https://jakevdp.github.io/PythonDataScienceHandbook/01.02-shell-keyboard-shortcuts.html
It need not remove these prompts, >>> and In [ ], whereas it has to remove them in other IDEs, or it will produce errors.
For example, a code snippet is taken from Python Shell session:
For example, a code snippet is taken from IPython Shell session:
This feature of Jupyter notebook is very helpful for us to have a quick test for some codes online from a Python shell or IPython shell session.
8. Brief Summary
This article presents how to edit and run Python files in Jupyter Notebook, which includes how to display a .py
file in Jupyter Notebook, load .py
file in Jupyter Notebook, append .py
file in Jupyter Notebook, update .py
file in Jupyter Notebook, run .py
file in Jupyter Notebook, run an external Jupyter Notebook .ipynb
file in a new Jupyter notebook as well run a Python scripts directly copied from a Python shell or IPython shell session.