Cannot import .py file to ipython notebook

早过忘川 提交于 2019-12-18 06:56:44

问题


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!


回答1:


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



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/23303402/cannot-import-py-file-to-ipython-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!