无法将.py文件导入ipython笔记本 [英] Cannot import .py file to ipython notebook

查看:78
本文介绍了无法将.py文件导入ipython笔记本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我无法正常工作"问题,我们事先表示歉意:如何将.py文件加载到ipython Notebook中?我想将python代码转换为笔记本(最初的简单脚本以及后来包含nbconvert指令嵌入为注释-参见链接文件的底部.)

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.)

也许我做错了,但是我的设置可能有问题.当我将.py文件拖到笔记本的文件列表中时,我收到消息

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

无效的文件类型:上传的笔记本必须是.ipynb个文件.

我什至尝试将扩展名更改为.ipynb(保持python脚本未修改);足够合理,我得到了一个错误:

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?

系统信息:我在OS X(10.8,Mountain Lion)上,使用Firefox 28.0和Anaconda 1.9.2(x86_64),后者提供python 2.7.6和ipython 2.0. Anaconda不在默认的PATH上;我将其添加到一个bash会话中,然后从中使用ipython notebook启动笔记本,并且能够正常在浏览器中打开和编辑.ipynb文件.

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.

但是我确实得到了一些奇怪的行为: 从笔记本中导出为.py文件时,我没有得到

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.

知道这是怎么回事吗?

ipython nbconvert生成相同的格式.但是,如果我用ipython notebook --script启动笔记本服务器(每次保存时都会将笔记本作为python脚本导出),结果将包含我们需要转换回笔记本的nbconvert指令!

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!

推荐答案

我不确定笔记本为什么不原生支持此功能,但是我得出的结论是:不能通过命令来完成线或笔记本GUI.

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.

<markdowncell>这样的控件注释只能通过python访问笔记本的API来解释,如@CliffordVienna在此答案中显示的我的相关问题.

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')

上述方法不适用于当前版本的Notebook API(v4),因此我添加了

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.

这篇关于无法将.py文件导入ipython笔记本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆