9

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.

channa ly
  • 8,543
  • 13
  • 46
  • 75

6 Answers6

15

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

channa ly
  • 8,543
  • 13
  • 46
  • 75
11

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.

Klaus-Dieter Warzecha
  • 2,155
  • 2
  • 25
  • 32
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

channa ly
  • 8,543
  • 13
  • 46
  • 75
Raj006
  • 563
  • 4
  • 17
10

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

enter image description here

Biranchi
  • 15,670
  • 23
  • 121
  • 160
  • 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 – Jehan Feb 21 '20 at 23:07
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.

Haik
  • 493
  • 1
  • 4
  • 5
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

Pluviophile
  • 1,860
  • 7
  • 12
  • 28