使用 ipython2 笔记本获取“SyntaxError:无效语法”

Posted

技术标签:

【中文标题】使用 ipython2 笔记本获取“SyntaxError:无效语法”【英文标题】:Getting "SyntaxError: invalid syntax" with ipython2 notebook 【发布时间】:2015-12-10 16:21:29 【问题描述】:

通过最近的升级,我现在在使用$ ipython2 notebook 启动笔记本的 python2.7 实例时遇到了SyntaxError: invalid syntax 的严重问题。

错误似乎源自使用aynciotornado,并且在该库中有python3.4 语法。

我可以从$ ipython3 notebook 开始,但我正在做的应用程序使用的是mayavi,我相信它仍然只适用于python2。

讨厌的错误信息是:

$ ipython2 notebook
Traceback (most recent call last):
  File "/usr/local/bin/ipython2", line 11, in <module>
    sys.exit(start_ipython())
  File "/usr/local/lib/python2.7/dist-packages/IPython/__init__.py", line 118, in start_ipython
    return launch_new_instance(argv=argv, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/traitlets/config/application.py", line 591, in launch_instance
    app.initialize(argv)
  File "<string>", line 2, in initialize
  File "/usr/local/lib/python2.7/dist-packages/traitlets/config/application.py", line 75, in catch_config_error
    return method(app, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/IPython/terminal/ipapp.py", line 305, in initialize
    super(TerminalIPythonApp, self).initialize(argv)
  File "<string>", line 2, in initialize
  File "/usr/local/lib/python2.7/dist-packages/traitlets/config/application.py", line 75, in catch_config_error
    return method(app, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/IPython/core/application.py", line 386, in initialize
    self.parse_command_line(argv)
  File "/usr/local/lib/python2.7/dist-packages/IPython/terminal/ipapp.py", line 300, in parse_command_line
    return super(TerminalIPythonApp, self).parse_command_line(argv)
  File "<string>", line 2, in parse_command_line
  File "/usr/local/lib/python2.7/dist-packages/traitlets/config/application.py", line 75, in catch_config_error
    return method(app, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/traitlets/config/application.py", line 487, in parse_command_line
    return self.initialize_subcommand(subc, subargv)
  File "<string>", line 2, in initialize_subcommand
  File "/usr/local/lib/python2.7/dist-packages/traitlets/config/application.py", line 75, in catch_config_error
    return method(app, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/traitlets/config/application.py", line 418, in initialize_subcommand
    subapp = import_item(subapp)
  File "/usr/local/lib/python2.7/dist-packages/ipython_genutils/importstring.py", line 31, in import_item
    module = __import__(package, fromlist=[obj])
  File "/usr/local/lib/python2.7/dist-packages/notebook/notebookapp.py", line 48, in <module>
    from tornado import httpserver
  File "/usr/local/lib/python2.7/dist-packages/tornado/httpserver.py", line 34, in <module>
    from tornado.http1connection import HTTP1ServerConnection, HTTP1ConnectionParameters
  File "/usr/local/lib/python2.7/dist-packages/tornado/http1connection.py", line 28, in <module>
    from tornado import gen
  File "/usr/local/lib/python2.7/dist-packages/tornado/gen.py", line 1236, in <module>
    import tornado.platform.asyncio
  File "/usr/local/lib/python2.7/dist-packages/tornado/platform/asyncio.py", line 33, in <module>
    import asyncio
  File "/usr/local/lib/python2.7/dist-packages/asyncio/__init__.py", line 9, in <module>
    from . import selectors
  File "/usr/local/lib/python2.7/dist-packages/asyncio/selectors.py", line 39
    "!r".format(fileobj)) from None
                               ^
SyntaxError: invalid syntax

什么对我有帮助?降级龙卷风?我的设置有问题吗。为什么python2.7下完全可以安装asyncio?

【问题讨论】:

能否请您打开有关 Tornado 项目的错误报告?如果这是一个足够常见的问题,Tornado 的补丁可能能够同时支持 Python 2.7 和 3.x。 Tornado 支持 Python 2.7 和 3.x。 AFAIU(见下面我的回答)。我现在的理解是,这是特定于我的系统(即我的错误)或其他地方的需求问题。阅读 github.com/tornadoweb/tornado/blob/master/setup.py 我看不到 asyncio/trollius 是如何包含在内的。我想知道这是否是龙卷风问题。 【参考方案1】:

降级龙卷风作品:

$ sudo pip install tornado==4.1
$ ipython2 notebook
[I 17:22:31.800 NotebookApp] Serving notebooks from local directory: /home/fnielsen/fnielsen/ipynb
...

【讨论】:

请注意,这并不能解决根本问题。更好的是,它似乎将asynciotrollius 交换:$ sudo pip2 uninstall asyncio 后跟$ sudo pip2 install trolliustornado(4.1 和 4.3)和 notebook 在此之后工作。

以上是关于使用 ipython2 笔记本获取“SyntaxError:无效语法”的主要内容,如果未能解决你的问题,请参考以下文章

利用virtualenv实现Python2和Python3共存,且ipython2和ipython3共存

第一周Python学习7-30

使用 redshift listagg 函数的语法错误

使用 postgres/Python 选择撇号

第十节:numpy之数组文件操作

Visual Studio 不允许我使用某些变量名