Skip to main content
 首页 » 编程设计

python之为什么仅在带有 python 3.5 的 Jupyter notebook 中给出 SyntaxError : invalid syntax with async def

2025年05月04日13insus

我有一个仅使用 python 3.5 设置的 virtualenv。当我在那个环境中时。

$python --version #gives  python 3.5.1 
 
$python  # in the interpreter. 
>>>async def func():pass 
>>>func()  

工作正常。 我正在开发 Linux 操作系统。

$ipython --version gives 4.0.1 
$ipython 
ln [1]: async def func():pass 
ln [2]: func() # gives <coroutine object func at 0x7f57524b59e8> 

效果很好。

但是当我启动 jupyter 笔记本并进入一个新笔记本时。

ln [1]: async def func():pass 
        func()     # gives SyntaxError: invalid syntax and points to the first line. 

我仔细检查了所有间距。当我使用

!which python 

它指向在 virtualenv 终端中运行 interpereter 时使用的同一个文件。

请您参考如下方法:

在下面与 Padric Cunningham 交流之后,我尝试了 Anaconda 并遇到了同样的问题。 我清除了系统中与 jupyter 相关的任何内容。我清理了 .bashrc、.pythonstartup.py 和 .profile 中我认为可能会妨碍的任何内容,包括对 $PATH 和 $PYTHONPATH 的更改

我认为问题所在的文件是:

.local/share/jupyter/kernels/python3.

我还删除了主目录中的一个 .jupyter 文件,该文件似乎是遗留的,以及 Padric 提到的文件:

/usr/local/share/jupyter/kernels/python3 #removed via it's jupyter folder.

清理后,我重新安装了 Anaconda 和 jupyter notebook。 Jupyter notebook 现在可以在 anaconda 虚拟环境中与 python 3.5.1 一起正常工作。