Jupyter (iPython) 魔术方法被注册为 str 而不是可调用
Jupyter (iPython) magic methods getting registered as str rather than callable
我今天安装了 Jupyter (iPython),能够在浏览器中调出 Notebook 并将其连接到笔记本服务器。我也能够在单元格中执行 Python 代码,但是当我尝试使用诸如 %timeit 或 %hist 之类的魔术方法时,我收到错误
'str' object is not callable
.
我在 MagicsManager 中打印出 magics['line'] 对象,它表明魔法方法对的值是一个字符串而不是可调用的东西。为什么会这样?
Looking for line magic timeit
{'load': 'load', 'psource': 'psource', 'lsmagic': 'lsmagic', 'logstart': 'logstart', 'popd': 'popd', 'loadpy': 'loadpy', 'ls': <alias ls for 'ls -F --color'>, 'install_ext': 'install_ext', 'cd': 'cd', 'pastebin': 'pastebin', 'colors': 'colors', 'prun': 'prun', 'cp': <alias cp for 'cp'>, 'lk': <alias lk for 'ls -F -o --color %l | grep ^l'>, 'who_ls': 'who_ls', 'lf': <alias lf for 'ls -F -o --color %l | grep ^-'>, 'install_profiles': 'install_profiles', 'autocall': 'autocall', 'ldir': <alias ldir for 'ls -F -o --color %l | grep /$'>, 'bookmark': 'bookmark', 'connect_info': 'connect_info', 'll': <alias ll for 'ls -F -o --color'>, 'pprint': 'pprint', 'mkdir': <alias mkdir for 'mkdir'>, 'system': 'sx', 'automagic': 'automagic', 'whos': 'whos', 'rerun': 'rerun', 'rmdir': <alias rmdir for 'rmdir'>, 'save': 'save', 'tb': 'tb', 'lx': <alias lx for 'ls -F -o --color %l | grep ^-..x'>, 'more': 'less', 'pylab': 'pylab', 'pdef': 'pdef', 'killbgscripts': 'killbgscripts', 'precision': 'precision', 'quickref': 'quickref', 'pinfo': 'pinfo', 'pwd': 'pwd', 'psearch': 'psearch', 'autosave': 'autosave', 'reset': 'reset', 'magic': 'magic', 'dhist': 'dhist', 'edit': 'edit', 'logstop': 'logstop', 'gui': 'gui', 'xdel': 'xdel', 'xmode': 'xmode', 'cat': <alias cat for 'cat'>, 'alias_magic': 'alias_magic', 'store': 'store', 'man': 'man', 'recall': 'recall', 'debug': 'debug', 'logon': 'logon', 'page': 'page', 'history': 'history', 'profile': 'profile', 'logstate': 'logstate', 'ed': <IPython.core.magic.MagicAlias object at 0x7f7a7113d6d0>, 'pycat': 'pycat', 'pushd': 'pushd', 'timeit': 'timeit', 'set_env': 'set_env', 'unalias': 'unalias', 'rehashx': 'rehashx', 'hist': <IPython.core.magic.MagicAlias object at 0x7f7a7113d490>, 'install_default_config': 'install_default_config', 'mv': <alias mv for 'mv'>, 'env': 'env', 'qtconsole': 'qtconsole', 'load_ext': 'load_ext', 'rm': <alias rm for 'rm'>, 'config': 'config', 'dirs': 'dirs', 'run': 'run', 'reset_selective': 'reset_selective', 'pfile': 'pfile', 'pinfo2': 'pinfo2', 'who': 'who', 'matplotlib': 'matplotlib', 'less': 'less', 'notebook': 'notebook', 'unload_ext': 'unload_ext', 'doctest_mode': 'doctest_mode', 'sx': 'sx', 'macro': 'macro', 'clear': 'clear', 'pdb': 'pdb', 'alias': 'alias', 'logoff': 'logoff', 'reload_ext': 'reload_ext', 'time': 'time', 'sc': 'sc', 'rep': <IPython.core.magic.MagicAlias object at 0x7f7a7113d550>, 'pdoc': 'pdoc'}
built_in trap:
args= [u'']
kwargs= {}
fn= timeit <type 'str'>
str object timeit is not callable...
我删除了所有单独安装的软件包,而是执行了以下操作:
- 在具有互联网连接的机器上创建 pip2pi 本地存储库
pip2pi . --no-use-wheel jupyter
- scp-ed 将步骤 1 中创建的 repo 的 zip 文件下载到我想要的机器上(这台机器没有互联网访问权限,因此我不得不在另一台机器上使用 pip2pi)(scp-ed as ipython_stuff.zip)
- 使用
pip install --index-url=file:///var/tmp/ipython_stuff/simple jupyter
安装
运行 jupyter notebook
这次工作了所以我猜问题是缺少依赖项或版本不正确,因为我已经手动安装了所有依赖项,一个接一个。使用 pip2pi 可以避免因缺乏互联网连接而无法使用 pip 的问题,还可以确保下载所有正确版本的包。
我今天安装了 Jupyter (iPython),能够在浏览器中调出 Notebook 并将其连接到笔记本服务器。我也能够在单元格中执行 Python 代码,但是当我尝试使用诸如 %timeit 或 %hist 之类的魔术方法时,我收到错误
'str' object is not callable
.
我在 MagicsManager 中打印出 magics['line'] 对象,它表明魔法方法对的值是一个字符串而不是可调用的东西。为什么会这样?
Looking for line magic timeit
{'load': 'load', 'psource': 'psource', 'lsmagic': 'lsmagic', 'logstart': 'logstart', 'popd': 'popd', 'loadpy': 'loadpy', 'ls': <alias ls for 'ls -F --color'>, 'install_ext': 'install_ext', 'cd': 'cd', 'pastebin': 'pastebin', 'colors': 'colors', 'prun': 'prun', 'cp': <alias cp for 'cp'>, 'lk': <alias lk for 'ls -F -o --color %l | grep ^l'>, 'who_ls': 'who_ls', 'lf': <alias lf for 'ls -F -o --color %l | grep ^-'>, 'install_profiles': 'install_profiles', 'autocall': 'autocall', 'ldir': <alias ldir for 'ls -F -o --color %l | grep /$'>, 'bookmark': 'bookmark', 'connect_info': 'connect_info', 'll': <alias ll for 'ls -F -o --color'>, 'pprint': 'pprint', 'mkdir': <alias mkdir for 'mkdir'>, 'system': 'sx', 'automagic': 'automagic', 'whos': 'whos', 'rerun': 'rerun', 'rmdir': <alias rmdir for 'rmdir'>, 'save': 'save', 'tb': 'tb', 'lx': <alias lx for 'ls -F -o --color %l | grep ^-..x'>, 'more': 'less', 'pylab': 'pylab', 'pdef': 'pdef', 'killbgscripts': 'killbgscripts', 'precision': 'precision', 'quickref': 'quickref', 'pinfo': 'pinfo', 'pwd': 'pwd', 'psearch': 'psearch', 'autosave': 'autosave', 'reset': 'reset', 'magic': 'magic', 'dhist': 'dhist', 'edit': 'edit', 'logstop': 'logstop', 'gui': 'gui', 'xdel': 'xdel', 'xmode': 'xmode', 'cat': <alias cat for 'cat'>, 'alias_magic': 'alias_magic', 'store': 'store', 'man': 'man', 'recall': 'recall', 'debug': 'debug', 'logon': 'logon', 'page': 'page', 'history': 'history', 'profile': 'profile', 'logstate': 'logstate', 'ed': <IPython.core.magic.MagicAlias object at 0x7f7a7113d6d0>, 'pycat': 'pycat', 'pushd': 'pushd', 'timeit': 'timeit', 'set_env': 'set_env', 'unalias': 'unalias', 'rehashx': 'rehashx', 'hist': <IPython.core.magic.MagicAlias object at 0x7f7a7113d490>, 'install_default_config': 'install_default_config', 'mv': <alias mv for 'mv'>, 'env': 'env', 'qtconsole': 'qtconsole', 'load_ext': 'load_ext', 'rm': <alias rm for 'rm'>, 'config': 'config', 'dirs': 'dirs', 'run': 'run', 'reset_selective': 'reset_selective', 'pfile': 'pfile', 'pinfo2': 'pinfo2', 'who': 'who', 'matplotlib': 'matplotlib', 'less': 'less', 'notebook': 'notebook', 'unload_ext': 'unload_ext', 'doctest_mode': 'doctest_mode', 'sx': 'sx', 'macro': 'macro', 'clear': 'clear', 'pdb': 'pdb', 'alias': 'alias', 'logoff': 'logoff', 'reload_ext': 'reload_ext', 'time': 'time', 'sc': 'sc', 'rep': <IPython.core.magic.MagicAlias object at 0x7f7a7113d550>, 'pdoc': 'pdoc'}
built_in trap:
args= [u'']
kwargs= {}
fn= timeit <type 'str'>
str object timeit is not callable...
我删除了所有单独安装的软件包,而是执行了以下操作:
- 在具有互联网连接的机器上创建 pip2pi 本地存储库
pip2pi . --no-use-wheel jupyter
- scp-ed 将步骤 1 中创建的 repo 的 zip 文件下载到我想要的机器上(这台机器没有互联网访问权限,因此我不得不在另一台机器上使用 pip2pi)(scp-ed as ipython_stuff.zip)
- 使用
pip install --index-url=file:///var/tmp/ipython_stuff/simple jupyter
安装
运行 jupyter notebook
这次工作了所以我猜问题是缺少依赖项或版本不正确,因为我已经手动安装了所有依赖项,一个接一个。使用 pip2pi 可以避免因缺乏互联网连接而无法使用 pip 的问题,还可以确保下载所有正确版本的包。