Reputation: 1665
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.
Upvotes: 12
Views: 33160
Reputation: 41
I had this issue after saving a checkpoint in my notebook. I spent hours trying to find the position of the invalid token in the JSON, but all I needed to do was open the .ipynb in PyCharm, make a new blank notebook, and copy the cells of the affected notebook to the new notebook. Saved it and it’s worked perfectly since.
Upvotes: 1
Reputation: 1239
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.
Upvotes: 0
Reputation: 41
"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!
Upvotes: 1
Reputation: 1
Resaving the document using another encoding format solved my problem. A regular.ipynb file (left image) is saved using Unix(LF) format, but the file that couldn't open was saved using UTF (right image)
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 31
I had the same issue and for me that problem was solved simply by deleting the underscore (_) from the file name. I don't know why but it works.
Upvotes: 3
Reputation: 81
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.
<<<<<<< HEAD
...
=======
...
>>>>>>> ...
Cleaning up these, allowed jupyter to parse the file and run the notebook.
Upvotes: 8
Reputation: 523
.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
Upvotes: 1
Reputation: 9451
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.
Upvotes: 3