jupyter python查找函数定义_python – 如何在jupyter中定义自定义魔法?

我正在使用Ubuntu 14.04 LTS和Anaconda python安装:

Python 3.5.1 :: Anaconda 2.4.1 (64-bit)

我正在尝试使用this recipe在我的ipython笔记本中启用C交互式编译:

import IPython.core.magic as ipym

@ipym.magics_class

class CppMagics(ipym.Magics):

@ipym.cell_magic

def cpp(self, line, cell=None):

"""Compile, execute C++ code, and return the standard output."""

# Define the source and executable filenames.

source_filename = 'temp.cpp'

program_filename = 'temp.exe'

# Write the code contained in the cell to the C++ file.

with open(source_filename, 'w') as f:

f.write(cell)

# Compile the C++ code into an executable.

compile = self.shell.getoutput("g++ {0:s} -o {1:s}".format(

source_filename, program_filename))

# Execute the executable and return the output.

output = self.shell.getoutput(program_filename)

return output

def load_ipython_extension(ipython):

ipython.register_magics(CppMagics)

无论我是用ipython notebok还是jupyter笔记本启动我的笔记本电脑(我相信第二个的第一个别名),当我执行一个单元格时:

%load_ext cppmagic

我收到以下错误:

---------------------------------------------------------------------------

ImportError Traceback (most recent call last)

in ()

----> 1 get_ipython().magic('load_ext cppmagic')

/home/norah/anaconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py in magic(self, arg_s)

2334 magic_name, _, magic_arg_s = arg_s.partition(' ')

2335 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)

-> 2336 return self.run_line_magic(magic_name, magic_arg_s)

2337

2338 #-------------------------------------------------------------------------

/home/norah/anaconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line)

2255 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals

2256 with self.builtin_trap:

-> 2257 result = fn(*args,**kwargs)

2258 return result

2259

/home/norah/anaconda3/lib/python3.5/site-packages/IPython/core/magics/extension.py in load_ext(self, module_str)

/home/norah/anaconda3/lib/python3.5/site-packages/IPython/core/magic.py in (f, *a, **k)

191 # but it's overkill for just that one bit of state.

192 def magic_deco(arg):

--> 193 call = lambda f, *a, **k: f(*a, **k)

194

195 if callable(arg):

/home/norah/anaconda3/lib/python3.5/site-packages/IPython/core/magics/extension.py in load_ext(self, module_str)

64 if not module_str:

65 raise UsageError('Missing module name.')

---> 66 res = self.shell.extension_manager.load_extension(module_str)

67

68 if res == 'already loaded':

/home/norah/anaconda3/lib/python3.5/site-packages/IPython/core/extensions.py in load_extension(self, module_str)

87 if module_str not in sys.modules:

88 with prepended_to_syspath(self.ipython_extension_dir):

---> 89 __import__(module_str)

90 mod = sys.modules[module_str]

91 if self._call_load_ipython_extension(mod):

ImportError: No module named 'cppmagic'

配方中的代码似乎与the official docs一致(都使用IPython.core.magic.magics_class)我已将cppmagic.py放在以下目录中

~/.ipython/profile_default/startup

让它在笔记本电脑启动时自动加载,但我无法感受到魔力.有人可以帮忙吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值