在 vscode 中打开 ipynb 文件时,JSON 中位置 0 出现意外标记 #

jjc*_*mar 12 python jupyter visual-studio-code

我有一个 ipynb 文件(一个 jupyter 笔记本),我在 vscode 中使用 python 扩展名打开它。我收到标题中的错误

Unexpected token # in JSON at position 0

我根本不明白,因为该文件应该被解释为 python 文件。

我可以将扩展名更改为 .py 并通过 vscode 很好地打开它,但我没有装饰器来运行/调试单元格,如此处定义的(https://code.visualstudio.com/docs/python/jupyter-support-py) 。

我知道该文件是正确的,因为我已在另一台计算机上的另一个 vscode 安装中使用它并且工作正常。

我不知道我的环境中可能配置错误......任何顶部都会非常有帮助。

这是我的实际 python 代码,它在我的实际环境中产生了上述错误。

问题.ipynb

# %%
import world as w
import world_eg as weg
import world_case1 as wc1
import simulator_static as simulation
import numpy as np
from scipy.optimize import minimize
import matplotlib.pyplot as plt```

From the error, I understand that is parsing the file as a JSON file and the first line, which contains the #, fails. 
Run Code Online (Sandbox Code Playgroud)

小智 7

我遇到了类似的问题,当我用编辑器打开笔记本时,我看到我有 git 放入文件中的合并标记。例如

<<<<<<< HEAD
...
=======
...
>>>>>>> ...
Run Code Online (Sandbox Code Playgroud)

清理这些,允许 jupyter 解析文件并运行笔记本。


Mol*_*SFT 3

This happens when you make a request to the server and parse the response as JSON, but it\xe2\x80\x99s not JSON. JSON should start with a valid JSON value \xe2\x80\x93 an object, array, string, number, or false/true/null. The root cause is that the server returned HTML or some other non-JSON string.

\n

I\'ve tried your code in my project and nothing wrong. everything looks fine. Check the Jupyter Server network, try to restart vscode and recreate a new juypter file, and see if the problem goes away.

\n

在此输入图像描述

\n

[edit]

\n

like the above screenshot shows, type # %% will add a new cell. Equally, when you open a .ipynb file, if python extension distinguishes the # %%, button run cell | debug cell will be displayed automatically for you to do further test.

\n

在此输入图像描述

\n

you can copy your code without # %% to a new created blank juypter file, then\nclick the button export as and select Python Script to got button Run Cell | Debug Cell .

\n

OR reinstall python extension and try again.

\n