6

With apologies in advance for the "I can't get it to work" question: How should I load a .py file into ipython notebook? I want to convert python code to notebooks (first simple scripts and later scripts that include nbconvert directives embedded as comments-- see bottom of the linked file.)

Perhaps I'm doing it wrong, but perhaps there's something wrong with my set-up. When I drag a .py file to the Notebook's file list, I get the message

Invalid file type: Uploaded notebooks must be .ipynb files.

I even tried changing the extension to .ipynb (keeping the python script unmodified); reasonably enough, I got an error:

Error loading notebook: Bad request

Any idea what's going wrong?

System information: I'm on OS X (10.8, Mountain Lion), using Firefox 28.0 and Anaconda 1.9.2 (x86_64), which supplies python 2.7.6 and ipython 2.0. Anaconda is not on the default PATH; I add it in a bash session from which I then launch notebook with ipython notebook, and I'm able to open and edit .ipynb files normally in the browser.

But I do get some curious behavior: When exporting from notebook as a .py file, I don't get the control comments documented here but a simpler format, without version number:

# coding: utf-8

# In[ ]:

print "This is a slide"

## Top-level title 

### Second-level heading 

#### Third-level heading

# This is some `markdown` text. 
# 
# And some more here.

Any idea what's going on here?

The same format is generated by ipython nbconvert. However, if I start the notebook server with ipython notebook --script (which exports the notebook as a python script every time it is saved), the result contains the nbconvert directives we need to convert back to a notebook!

3
  • possible duplicate of Converting to (not from) ipython Notebook format Commented Aug 5, 2014 at 3:30
  • @Cristian, the questions are related but they're not duplicates; that's why I asked both of them. The answer to this one is that there's no way to do it in the GUI, which is the only reason the other question becomes relevant. Commented Aug 8, 2014 at 19:32
  • Ok, I've retracted my close vote. Commented Aug 8, 2014 at 21:38

3 Answers 3

10

I had the same problem. This post helped: How to load/edit/run/save text files (.py) into an IPython notebook cell?

Basically, we just have to use the following command in the cell. And the .py file has to be in the same directory.

%load filename.py
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that's a useful feature to know about but not what I needed. I've solved the problem in the meantime, I should have self-answered sooner.
5

I'm not sure why notebook doesn't support this natively, but I've concluded that the answer is: It can't be done from the command line or notebook GUI.

Control comments like <markdowncell> can only be interpreted by accessing notebook's API through python, as shown by @CliffordVienna in this answer to my related question.

import IPython.nbformat.current as nbf
nb = nbf.read(open('test.py', 'r'), 'py')
nbf.write(nb, open('test.ipynb', 'w'), 'ipynb')

Edit: The above method does not work with the current version (v4) of the Notebook API, so I have added this self-answer to show how it's done.

Comments

-2

If you only need to import a local file, first use:

sys.path.append(os.getcwd())

to place the .pynb file's directory in sys.path, and then import the local file.

2 Comments

Thanks for trying to help, but do you know what an ipython notebook is? sys.path is irrelevant to the question.
Actuallly, it is relevant. If the python file is in a different directory, you need to use sys.path. For your specific case, where the file is in the same directory, you don't need to use sys.path.

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.