In VSCode, my syntax highlighting for variables occasionally disables. After experimentation, it seems to happen mostly when I'm working on WSL. I haven't found a solid way to recreate it, but I've noticed it happening (not all the time, but sometimes) when performing any action that would temporarily render the variable as plaintext/not a variable. For example, temporarily removing the "=" when defining the variable.
To fix it, I can restart/reload VSCode, but it just gets pretty inconvenient considering it happens once every 30 minutes, if not more often. My theme is Dark+ and it normally looks like the first image (blue highlighted variables), but whenever this bug happens, it looks like the second (unhighlighted; white text).
It also happens when defining functions. Not sure if it happens to anything else at this point.
I've tried resetting my VSCode, reinstalling all extensions, deleting the "Code" folder in %appdata%, as well as deleting ".vscode" in %userprofile%.
When I hover over the variable, it is still recognized as a variable; it just is not highlighted.
My extensions are:
C/C++
C#
Discord Presence
GitHub Pull Requests and Issues
gitignore
HTML CSS Support
isort
Jupyter
Jupyter Cell Tags
Jupyter Keymap
Jupyter Notebook Renderers
Jupyter Slide Show
Live Server
Pylance
Python
GitHub Codespaces
WSL
What it looks like when the bug happens
What it should look like
Related
Background
Some time ago I seriously crashed my Windows computer while using PyCharm - I remember some errors about memory and then a hard crash with no blue screen - just black with some thin vertical lines and reboot to Windows installation / fixing screen. Since then, I had this problem, with no way I found online to fix this.
Edit : Apparently, this has nothing to do with the problem.
The problem
Whenever I open a project, or create a new one, an error appears with the Invalid Python SDK error message.
**Invalid Python SDK**
Cannot set up a Python SDKat Python 3.9 (%projectName%) (%projectPath%).The SDK seems to be invalid.
Also, this is what the work environment looks like the moment I close this message. In the Project window, the venv directory (and every directory under it) is marked as an Exclusion, and in the code, the print(f'Hi, {name}') function is marked as an unresolved reference error shown below. The program, however, executes flawlessly.
What's more, when I go to Python Interpreter settings at File -> Settings -> Project -> Python Interpreter there's a yellow bar on the bottom which says:
Non-zero exit code (4).
which after some time says:
Python packaging tools not found.
Upon installing, nothing changes, and I can't add packages from this screen (the '+' button is greyed out):
When I try to check Python interpreter paths, there are no paths shown, and I don't know what that means:
In short, all of the default Python functions like print are marked as errors, even though they work when executed. This makes coding extremely confusing, as I can't quickly distinguish between real errors and 'errors'.
The search for solution
Normally this would be a problem with interpreter set-up or path, but I've tried most of the methods proposed in other answers to similar questions. To name a few :
PyCharm shows unresolved references error for valid code
'Cannot setup a Python SDK' in PyCharm project using virtualenv after OS reinstallation
Why do I get an 'SDK seems invalid' error when setting up my Project Interpreter in PyCharm?
Invalid Python SDK Error while using python 3.4 on PyCharm
Invalid Python SDK when setting a venv
There were supposed to be links, but I don't have enough reputation on Stack Overflow to post them with the questions. These, however, can be easily looked up in Google, all of them are posted to Stack Overflow.
What I tried
I should mention that the first things I tried were removing and installing PyCharm, all user configurations and Python itself as well. I installed Python from the official site, and from the PyCharm application, both methods ended with the same result.
File -> Invalidate Caches... -> Invalidate and restart. Didn't work.
Checking file interpreter in Edit Configurations. Don't know what to make of it. The result:
Refreshing the interpreter paths. Even now, the paths yield no results.
Removing the interpreter and adding it again. No result.
Deleting the .idea folder. No result.
Deleting PyCharm user preferences under %homepath%/.PyCharm50. I don't have that folder though.
Switching interpreter back and forth. No result.
Creating a new interpreter in a different location. No result.
Marking project directory as root ProjectName -> Mark Directory as -> Sources Root and unmarking other directories as Excluded. No result.
Using no interpreter. Yeah, it doesn't mark non-errors as errors anymore. But the code doesn't work. That's not a solution for me.
Checking if venv/pyvenv.cfg has paths set correctly. These look fine to me.
Checking Windows environment variables - Path variable. It was in the user section, but wasn't in the system section. I added it, restarted but still no result.
Changing account name in Windows. My account name was 'username' and that's how my User folder is called `C:\Users\username', but I later connected it to Microsoft account and my user name is now User Name with a space and I can't really change it. My folder stayed the same. Not sure if I can fix it that way.
To the two last things I tried I should also add that I changed my Windows username from 'username' to 'user name' with a space, but that wasn't until recently.
I'm attaching the idea.log file for you to check. I replaced my real username with 'User Name' to highlight the existence of a space.
OK, that was a lucky one! I'm thus posting my comment as an answer:
The problem is caused by the non-ASCII characters in the path, and the solution is to remove them. As indicated by #TheLazyScripter this is a known issue.
It's been a little over a month since I'm using Jupyter notebooks, as an inbuilt extension within Visual Studio Code, and am pretty satisfied with it. However, recently I've been noticing that sometimes for no particular reason, I get a "direct kernel connection broken" error message when I attempt to run the specific cell upon which I'm working at that moment.
I do understand that this might be due to some errors that regularly creep up in my code, but something that I've noticed is that, say for instance that a particular snippet of the code that I've written is buggy. When I run the snippet in the traditional Jupyter Notebook (via my power shell, hosted as a web-based browser application) it shows the nature of the error, and where exactly has it occurred in the body of the program. For the VS Code based Jupyter Notebook extension, it does nothing of that sort and just displays a "Direct kernel connection broken" error message.
I have tried everything - from interrupting the kernel by toggling the red square key to restarting the kernel with the help of the green 'reload' key, but nothing seems to change. Occasionally during this issue, If close and open VS Code, or start coding in a new blank notebook, then this small trick does seem to return the kernel state back to normal. Nevertheless, I'm growing increasingly tired of this temporary fixture and was hoping to fix this issue permanently.
Edit: For further clarification, I have added two pictures for comparison. Both the snapshots are of the same snippet of code. The first picture is of my code running on a traditional Jupyter Notebook ( and it seems to work fine ).
The second picture is a snapshot of the same code snippet, running on VS Code's Extension for Jupyter Notebook.
For further information, kindly refer to the comments below.
Lately, I've been running some Jupyter notebooks in VS Code, and I've been encountering a strange issue: whenever I open such a file, I am bombarded with pop-ups that look like this:
Sometimes a few will pop up; other times it can be upwards of 10 pop-ups. What's bizarre about this is that I already have my VS Code set up properly, and I can run my Jupyter notebooks just fine. I've tried selecting the 'Modify' option and going with the default selections just to make it go away, but no dice. How do I prevent these annoying pop-ups?
Per your new comments, can you check your default settings to see which application is targeted to open .ipynb files? Perhaps .ipynb files are linked to open (strangely) via the Setup exe.
I am more used to Spyder and very new in vsc. In Spyder you have a python console where you can quickly try a codesnipped like:
print(len(a))
without having to run the entire code.
Is that also possible in vsc? I have not seen anything.
In addition, I can only see the variables in the debug mode. In Spyder I have all variables after one run at hand and can try and experiment on the fly with them. Is there another Variable-viewer plugin or how could that be enabled?
You can use this extensions of python. It has a Jupyter Notebook feature that comes with a variable explorer. It is recommended to also add the Jupyter extension as well (they work together just fine), you can find it here.
You can read this documentary about how to use Jupyter with visual studio code, you can see that it shows how to display the variables the way you wanted.
I was recently introduced to JupyterLab and I have seen this video from the documentation, where they introduce the synchronised "output view" of a cell:
https://youtu.be/Asa_ML45HP8
I wonder if it was possible to have "only" this output view as the result of executing a script. So that I could run a Python script, and a browser window would open up and I would see only the output view without the Jupyter interface itself.
Is this or something similar easily feasible already? E.g. by configuring JupyterLab in some way and then starting it?
If I wanted to write an application that does that, do you have hints on where to start from? I presume, that there is a lot of code already present that could be reused.