6

Suppose I want use jupiter notebook/ipython as an development environment and copy everything to a python scripts afterwards. In ipython we have commands like

In [1]: cd ..
/Users/myname/Desktop/software

In [2]: ls
  blah_blah_blah/ 

Suppose I finish my ipython notebook and want to copy everything (suppose I have 1000 lines and I could not edit them 1 by 1) to create my python script. Is it possible to enable my python script to understand such lines like "cd .." etc.

1
  • There are os and os.path modules that can do things like cd. Commented Mar 21, 2017 at 23:38

2 Answers 2

4

Any method for running your IPython code using a standard Python interpreter is going to be a little complicated. For example, see this question, with one of the answers illustrating the call to IPython's 'magic' methods to do a shell command:

from IPython.terminal.embed import InteractiveShellEmbed

ipshell = InteractiveShellEmbed()
ipshell.dummy_mode = True
ipshell.magic("%timeit abs(-42)")

The much easier option would be to simply use the IPython interpreter to run your saved script. You need to make sure that each shell command is preceded by a %, as this indicates a 'magic' command. Should be a simple find-and-replace task, as I doubt you are using too many shell commands. If there are a lot of different shell commands to prefix with % you could also write a short script to do this work for you. You also need to ensure that your script has the extension .ipy.

script.ipy:

%cd ..
%ls
x = "My script!"
print(x)

To run script from terminal:

>>> ipython script.ipy
Sign up to request clarification or add additional context in comments.

Comments

0

As said above, we can run ipython scripts using ipython from command line:

ipython my_script.ipy

However, magic cells are currently not working well. For instance, if you have the following script:

%%writefile hello.txt
this is a line
another line

print ('hello')

my_path = '/path/to/folder'
list_of_txt = !ls {my_path}/*.txt
print (list_of_txt)

It will run the initial cell magic %%writefile as if the whole file was a single cell. In order to avoid that, you have to use get_ipython().run_cell_magic ()

Since each ipython command needs a different function, I find it helpful to write ipython commands you want in a notebook, and then in the notebook's menu do File > "Save and Export Notebook As" > "Executable Script" (in jupyter lab, but probably something similar can be done in plain jupyter notebook). It will convert all the ipython-specific code using appropriate functions from the IPython library.

For example, if we copy and paste the previous script in different cells of a notebook and export it as a .py file, it gives us the following python code:

code written in exported script:

get_ipython().run_cell_magic('writefile', 'hello.txt', 'this is a line\nanother line\n')

my_path = '/path/to/folder'
list_of_txt  = get_ipython().getoutput('ls {my_path}/*.txt')
print (list_of_txt)

You can run the exported script directly using ipython from command line:

ipython my_exported_script.py

Or, inside an ipython session, using

%run my_exported_script.py

This works for IPython 1.x

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.