添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a ipynb file (a jupyter notebook) which I am opening in vscode with python extension. I receive the error in the title

Unexpected token # in JSON at position 0

which I dont understand at all, since the file is supposed to be interpreted as a python file.

I can change the extension to .py and its opened fine by vscode, but I dont have the decorators to run/debug cells like define here ( https://code.visualstudio.com/docs/python/jupyter-support-py ).

I know the file is correct because I have use it in another vscode installation in another computer and works fine.

I have no idea what might be misconfigured in my environment... Any tops would be really helpful.

Here is the actual python code I have that its producing the mentioned error my actual environment.

issue.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. Don't you think would be useful share the code? I mean, although you can run in another pc it could help to debug and find out the problem. Brad Figueroa Aug 3, 2020 at 23:45

I had a similar problem and when I opened the notebook with an editor I saw I had merge markings that git had put into the file. e.g.

=======

Cleaning up these, allowed jupyter to parse the file and run the notebook.

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

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.

[edit]

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.

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

OR reinstall python extension and try again.

I have tried to restart jupyter kernel and vs code and I have the same error. Also tried to create a new file. How can I check jupyter network? jjcasmar Aug 4, 2020 at 6:45 Yes, it shows connected and the python interpreter. I have also tried with other interpeeters jjcasmar Aug 4, 2020 at 6:59 ok, i've tested it again, there's nothing wrong with #. it's more like the function code in your file leading this error. so post your complete code and error screenshot. Molly Wang-MSFT Aug 4, 2020 at 7:45 It has nothing to do with my code. As said in the question, the code works fine in another environment. I can even execute my code, just it doesnt show Run Cell/Debug Cell decorators... jjcasmar Aug 4, 2020 at 7:58 I don't want to debug this further, but changing the filename and removing the last!! underscore worked for me too. I hope someone with more energy will find a proper explanation for this madness. Until then, thumbs up. Fookatchu Jul 5, 2021 at 15:01

.ipynb files aren't actually Python source code files - they're encoded as JSON files. If you create a new notebook, then rename the file extension or open it in some text editor, you'll see the structure of the underlying JSON file.

When VS Code tries to interpret your file, it's trying to parse Python source as a JSON object, which will obviously fail, and lead to the otherwise cryptic error of an unexpected token.

In other words, it's not possible to convert a Python script into a notebook just by changing the file extension. Manually copying & pasting the code around will work, or you could try googling for some tool, e.g. https://github.com/remykarem/python2jupyter

"Unable to open 'XXX.ipynb'" "Unexpected token < in JSON at position XXX"

For me, I have similar issues when I am using git and reopen ipynb files in vscode.

To fix it, pretty easy!

(1) Open and edit the file in json format and ACCEPT the current
changes or incoming changes.

(2) Save and Close the edited file and reopen the file. Everything works fine!

Good luck!

I had a similar issue when creating a new file in VS Code which I saved as .ipynb. After closing the file, I was not able to reopen and I received the same error message as above.

For me, simply closing and restart VS Code did the trick. Afterwards, the .ipynb-file opened as expected.

In case you downloaded from Github, you can:

-Open notebook in Github
-On editor find the icon "< >" on upper toolbar, its tip is: "Display the source blob".
-You will get the expected JSON file. Copy it with other icon in toolbar
-Then paste in your editor (VSCode) and save as ipynb notebook file.

It should work.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question . Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers .