12

I am new to Python. I start learning it with Jupyter notebook. It is very useful to test python code at the same time I can document what I've learned with markdown supported by Jupiter.

Until I started with module/package I noticed that every file ends with " notebook extension .ipynb. I understand that in order for Jupyter to have this good-looking visualization it has to store the file in some kind of format.

is there any solution to create a raw python file using Jupyter?

I am ok if I have to install other plugins to accomplish this.

7 Answers 7

16

Actually jupyter allows to create plain-text file:

Create a new text file

Create a Text Field

Save the text file with python extension

Save with python file extension

Sign up to request clarification or add additional context in comments.

1 Comment

There is also the option to change the Language in order to get syntax highlighting.
14

In order to create a python file from an existing notebook (somenotebook.ipynb), please run

jupyter nbconvert somenotebook.ipynb --to script

This will create somenotebook.py.

Comments

14

Another way of creating a python file and executing it from within Jupyter notebook cell is as below:

enter image description here

1 Comment

Thanks! This is perfect! Despite 8 years developing software in JS, Go, and Rust, I can't get a Python environment to work reliably on my machine, so I'm just doing everything on FloydHub 😂
11

If you created a jupyter notebook (.ipynb), and your goal is to create a python executable file (.py) from it, you can directly use the menu option from "File > Download as > Python (.py)" as shown below.

enter image description here

Comments

0

I didn't find such option in jupyter notebook, but you can create empty *.py file and then open with jupyter. It is better then plain text, because you get colored text.

2 Comments

Python file is a plain text with extension .py.
FYI: source code is a plain text and a Python file is no exception.
0

Another approach of adding code from jupyter notebook cells to a .py is by using the built-in Magic command %logstart.

The %writefile saves the current cell code to a .py file.

%logstart

From the Documentation

Start logging anywhere in a session.

%logstart [-o|-r|-t|-q] [log_name [log_mode]]

If no name is given, it defaults to a file named ipython_log.py in your current directory, in rotate mode (see below).

%logstart name saves to file name in backup mode. It saves your history up to that point and then continues logging.

%logstart takes a second optional parameter: logging mode. This can be one of (note that the modes are given unquoted):

append
Keep logging at the end of any existing file.

backup
Rename any existing file to name~ and start name.

global
Append to a single logfile in your home directory.

over
Overwrite any existing log.

rotate
Create rotating logs: name.1~, name.2~, etc.

Check more options in the documentation

Example:

%logstop
%logstart -ort sample.py append

The above command appends all the Jupyter notebook code to sample.py

Note: Should run in the first cell

Comments

0

Here is how I got a ipynb as a .py script file:

Goto File-> Save And Export Notebook As ->Executable Script .py file will be downloaded to Downloads folder. I run Version 4.0.0 of Jupyterlab in a chromebrowser.

enter image description here

You need to select all cells in the notebook to save all the contents into .py file

1 Comment

And if you want to do it conveniently on the command line, jupytext is a nice tool in the current Jupyter ecosystem that's perhaps a bit easier with more features than jupyter nbconvert.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.