2

我使用Ubuntu 14.04 LTS与水蟒蟒安装:如何在jupyter中定义自定义魔法?

的Python 3.5.1 ::阿纳康达2.4.1(64位)

我试图使用this recipe启用C++互动编译在我的IPython笔记本:

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 notebokjupyter notebook(我相信第一别名山高第二反正),当我执行单元搭配:

%load_ext cppmagic 

我收到以下错误:

--------------------------------------------------------------------------- 
ImportError        Traceback (most recent call last) 
<ipython-input-1-7b90c7a2b808> in <module>() 
----> 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 <lambda>(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 /启动

在笔记本电脑启动时自动加载,但我无法感受到神奇。谁能帮忙?

回答

5

这里有两个不同的东西:

  1. 启动文件脚本~/.ipython/profile_[name]/startup被作为原料IPython中的一部分执行。在第一个In[1]提示之前,他们被视为如果你们每个人都是%run。启动文件无法导入,因为它们不在sys.path上。
  2. 扩展是Python模块,可导入并定义load_ipython_extension函数。您可以在~/.ipython/extensions中放置扩展名,它们将是可导入的,或者您可以将它们作为常规包与pip一起安装。

第一个解决方法是把你的cppmagics~/.ipython/extensions或一些site-packages目录,使其可导入。

如果你真的想永远注册(而不是调用%load_ext cppmagic)的魔法,你可以把它作为一个启动文件,并登记在脚本结束的魔法,而不是def load_ipython_extension

if __name__ == '__main__': 
    from IPython import get_ipython 
    get_ipython().register_magics(CppMagics) 
+0

谢谢非常。这工作!不幸的是配方不....我得到一个'['/ bin/sh:1:temp.exe:not found']错误,但这与配方代码本身有关。我选择这个答案,如果你能帮我解决我最后一个问题。 –

+0

您可能想要添加'。/ temp.exe'来确保它正在本地目录中查找,我不确定。 – minrk

相关问题