Visual Studio Code "editor.showUnused": true not working - python

I'm trying to make Visual Studio Code detect unused Python imports. One of the ways is to type "editor.showUnused": true into the setting.json file.
{
"editor.showUnused": true
}
But it just doesn't work for me, even if I reloaded VSCode. I also went into VSCode settings and checked the box for "control fading out for unused code", which also did not work.
PyCharm automatically fades out unused imports. Is there a way to do the same in VSCode?

{"editor.showUnused": true} is VS Code default setting. The value controls fading out of unused code and decides if the unused modules are displayed solidly:
To fix your question, you can reset the VS Code. Delete everything between the two curly braces in User settings.json, save the file, and VS Code will go back to using the default values.
If the question still exists, it's recommended to uninstall VS Code completely:
Delete all settings: turn to directory \userfolder\AppData\Roaming\Code and delete the whole folder.
Delete all extensions: \userfolder\.vscode\extensions and delete the whole folder.
Then install VS Code again.

Related

'Invalid Python SDK' error right after creating a new project in PyCharm

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.

Trying to learn pygame but I bump into an unexpected error [duplicate]

This is the code I have:
import pygame
pygame.init()
I'm very confused because if I try to run the file, then there seems to be no issue, but pylint says the following:
E1101:Module 'pygame' has no 'init' member
I have searched thoroughly for a solution to this "error". In every relevant case I found, the solution was to make sure that I have not made another file or folder with the name "pygame", because in that case, I would just be importing my own file or folder.
However, I have not made a folder or file with a name even close to "pygame", so I don't know what the problem is.
As said earlier, it seems like I'm able to run the file without any issues and having errors like this confuses me in my learning process.
I write code in Visual Studio Code, I'm using python 3.6, I'm using pygame 1.9.3 and have updated my pylint. Any help would be appreciated.
Summarizing all answers.
This is a security measure to not load non-default C extensions.
You can white-list specific extension(s).
Open user settings and add the following between {}:
"python.linting.pylintArgs": [
"--extension-pkg-whitelist=extensionname" // comma separated
]
You can allow to "unsafe load" all extensions.
Open user settings and add the following between {}:
"python.linting.pylintArgs": [
"--unsafe-load-any-extension=y"
]
If you have VS code, go in your .vscode folder > settings.json or search for python.linting.mypyArgs Under user settings tab paste inbetween curly braces
"python.linting.pylintArgs": [
"--extension-pkg-whitelist=lxml" // The extension is "lxml" not "1xml"
]
I no longer see the pyinit error.
I had the same issue when I started using Visual Studio Code with Python. It has nothing to do with having another pygame.py or not installing it properly. It has to do with the fact that Visual Studio Code takes your code literally, and since you cannot import pygame.init(), it thinks that it isn't a correct module.
To fix this, open up settings.json (go into your settings, and click the {} icon) and paste
"python.linting.pylintArgs": [
"--extension-pkg-whitelist=pygame"
]
to it.
I had the same issue with one of my modules. This is what I did to resolve the problem. (I'm using visual studio on windows 10)
Press CTRL+SHIFT+P in visual studio
Choose "Preferences: Open Settings (JSON)"
Add "python.linting.pylintArgs": ["--generate-members"] below one of the lines (put a comma if necessary)
Save the .json file (CTRL+S)
For me, the code looks like this :
{
"breadcrumbs.enabled": false,
"editor.minimap.enabled": false,
"python.pythonPath": "C:\\Users\\xxx\\Anaconda3",
"terminal.integrated.rendererType": "dom",
"window.menuBarVisibility": "default",
"workbench.activityBar.visible": false,
"workbench.statusBar.visible": true,
"python.linting.pylintArgs": ["--generate-members"], //line to add
"[json]": {
}
}
Hope it helps.
Credit to #Alamnoor on github
This answer includes the answer to your question. In short it explains:
Pylint imports modules to effectively identify valid methods and attributes. It was decided that importing c extensions that are not part of the python stdlib is a security risk and could introduce malicious code.
and as a solution it mentions, among others:
Disable safety using the .pylintrc setting unsafe-load-any-extensions=yes.
See here for more information about pylint.rc. Quickest method is to just create the file .pylintrc in your project directory or your home directory.
I found adding this in settings.json() solves the problem.
"python.linting.pylintArgs":[
"--extension-pkg-whitelist=pygame",
"--erros-only"
]
I find an answer and it really works for me.
See the accepted answer and change it to extension-pkg-whitelist=lxml
pylint 1.4 reports E1101(no-member) on all C extensions
I recommend going to the view tab, clicking command palette and searching preferences: open settings.json. Then add a comma on the last line of code.Below that paste this:
"python.linting.pylintArgs": [
"--extension-pkg-whitelist=extensionname" // comma separated
]
Then save your document (ctrl + s).
Check if you have a python file named pygame.py created by you in your directory. If you do, then the import pygame line is importing your own file instead of the real Pygame module. Since you don't have an init() function in that file, you're seeing this particular error message.
I found a solution, modifying the most voted answer:
"python.linting.pylintArgs": [
"--extension-pkg-whitelist=pygame"
]
Replaced the "lxml" with "pygame".
Disable Pylint
1.Press ctrl + shift + p
2.Then type Disable Pylint
If you are using vscode then you can go to settings:
python.linting.pylintEnabled = False
It will fix the problem. If you aren't using vscode then you can go the command prompt and manually uninstall pylint with the command
pip uninstall pylint.

How to fix problem with Pygame in VB Code? [duplicate]

This is the code I have:
import pygame
pygame.init()
I'm very confused because if I try to run the file, then there seems to be no issue, but pylint says the following:
E1101:Module 'pygame' has no 'init' member
I have searched thoroughly for a solution to this "error". In every relevant case I found, the solution was to make sure that I have not made another file or folder with the name "pygame", because in that case, I would just be importing my own file or folder.
However, I have not made a folder or file with a name even close to "pygame", so I don't know what the problem is.
As said earlier, it seems like I'm able to run the file without any issues and having errors like this confuses me in my learning process.
I write code in Visual Studio Code, I'm using python 3.6, I'm using pygame 1.9.3 and have updated my pylint. Any help would be appreciated.
Summarizing all answers.
This is a security measure to not load non-default C extensions.
You can white-list specific extension(s).
Open user settings and add the following between {}:
"python.linting.pylintArgs": [
"--extension-pkg-whitelist=extensionname" // comma separated
]
You can allow to "unsafe load" all extensions.
Open user settings and add the following between {}:
"python.linting.pylintArgs": [
"--unsafe-load-any-extension=y"
]
If you have VS code, go in your .vscode folder > settings.json or search for python.linting.mypyArgs Under user settings tab paste inbetween curly braces
"python.linting.pylintArgs": [
"--extension-pkg-whitelist=lxml" // The extension is "lxml" not "1xml"
]
I no longer see the pyinit error.
I had the same issue when I started using Visual Studio Code with Python. It has nothing to do with having another pygame.py or not installing it properly. It has to do with the fact that Visual Studio Code takes your code literally, and since you cannot import pygame.init(), it thinks that it isn't a correct module.
To fix this, open up settings.json (go into your settings, and click the {} icon) and paste
"python.linting.pylintArgs": [
"--extension-pkg-whitelist=pygame"
]
to it.
I had the same issue with one of my modules. This is what I did to resolve the problem. (I'm using visual studio on windows 10)
Press CTRL+SHIFT+P in visual studio
Choose "Preferences: Open Settings (JSON)"
Add "python.linting.pylintArgs": ["--generate-members"] below one of the lines (put a comma if necessary)
Save the .json file (CTRL+S)
For me, the code looks like this :
{
"breadcrumbs.enabled": false,
"editor.minimap.enabled": false,
"python.pythonPath": "C:\\Users\\xxx\\Anaconda3",
"terminal.integrated.rendererType": "dom",
"window.menuBarVisibility": "default",
"workbench.activityBar.visible": false,
"workbench.statusBar.visible": true,
"python.linting.pylintArgs": ["--generate-members"], //line to add
"[json]": {
}
}
Hope it helps.
Credit to #Alamnoor on github
This answer includes the answer to your question. In short it explains:
Pylint imports modules to effectively identify valid methods and attributes. It was decided that importing c extensions that are not part of the python stdlib is a security risk and could introduce malicious code.
and as a solution it mentions, among others:
Disable safety using the .pylintrc setting unsafe-load-any-extensions=yes.
See here for more information about pylint.rc. Quickest method is to just create the file .pylintrc in your project directory or your home directory.
I found adding this in settings.json() solves the problem.
"python.linting.pylintArgs":[
"--extension-pkg-whitelist=pygame",
"--erros-only"
]
I find an answer and it really works for me.
See the accepted answer and change it to extension-pkg-whitelist=lxml
pylint 1.4 reports E1101(no-member) on all C extensions
I recommend going to the view tab, clicking command palette and searching preferences: open settings.json. Then add a comma on the last line of code.Below that paste this:
"python.linting.pylintArgs": [
"--extension-pkg-whitelist=extensionname" // comma separated
]
Then save your document (ctrl + s).
Check if you have a python file named pygame.py created by you in your directory. If you do, then the import pygame line is importing your own file instead of the real Pygame module. Since you don't have an init() function in that file, you're seeing this particular error message.
I found a solution, modifying the most voted answer:
"python.linting.pylintArgs": [
"--extension-pkg-whitelist=pygame"
]
Replaced the "lxml" with "pygame".
Disable Pylint
1.Press ctrl + shift + p
2.Then type Disable Pylint
If you are using vscode then you can go to settings:
python.linting.pylintEnabled = False
It will fix the problem. If you aren't using vscode then you can go the command prompt and manually uninstall pylint with the command
pip uninstall pylint.

Disable Python Error Underlining in VS Code

I don't know why but while using VS Code I can't seem to disable pylint or any linting for python.
:
I dont want any of the underlining
I don't like having it show errors in the text editor, only after running the code. I installed the Python extension for VS Code and included all of the settings to disable python linting in the settings.json file:
"python.linting.enabled": false,
"python.linting.lintOnSave": false,
"python.linting.pylintEnabled": false,
"python.linting.pylintUseMinimalCheckers": false
However, I still want to be able to turn it on and off so I don't just want to change the colour to transparent (also since the errors still show up in the scroll bar).
I want to be able to toggle the setting, 'Python > Linting: Enabled' but nothing happens when I toggle it, the linting is always there
Any help would really be appreciated
It was prompted by 'Microsoft Language Server' not the linters such as pylint. You can change the language server to remove these prompts:
In settings.json change from:
"python.languageServer": "Microsoft",
to
"python.languageServer": "Jedi",
Not only the linters but also the language server can linting your code. Such as Pyright extension starts the Pyright language server can also do this.
Well, I don't really understand why you're trying to disable something that helps a lot especially in python which is a interpreted language unlike javascript. But the main reason which comes to my mind about this error is because you're just disabling and sharing the settings.json of the user that is like a global configuration for all projects opened with VSCode, but you have another settings.json file inside the directory of the file(code shared) named as workspace configuracion that has pylint activated and it always overrides the user configuration as this official documentation points. So you should check both of them and if you have to disable pylint workspace configuration. Hope this can help you to understand what's going on.

VS Code: Analyzing in the background

When I start a python project in vs code (with the microsoft python extension) it starts "Analyzing in the background" and python keep crashing. It also uses a ton of memory.
Anyone knows how to fix this? Or is it supposed to do this?
This seems to have fixed it for me: https://github.com/Microsoft/vscode-python/issues/4990#issuecomment-477628947
You can disable the new Python Language Server by opening settings in VSCode (Ctrl+, ) and set "python.jediEnabled": true. Then reload the window and/or restart VSCode.
I had the same issue; the only solution that worked for me was to open settings.json (ctrl + Shift + P) and change
"python.languageServer": "Microsoft"
to
"python.languageServer": "Pylance"
Then one gets a pop-up that asks whether one wants to reload the window which one should confirm by pressing "OK".
Then everything works normally again (IntelliJ, autocomplete etc).
As #Vasco pointed out in the comments Microsoft is no longer supported as explained in this thread.
High memory usageļ¼š https://github.com/Microsoft/python-language-server/issues/832
Jedi is an autocompletion tool for Python that can be used in IDEs/editors. Jedi works. Jedi is fast. It understands all of the basic Python syntax elements including many builtin functions. So you can switch Jedi instead of Python Language Server.
Process:
set "python.jediEnabled": true
disable the Visual Studio IntelliCode plugin
delete the .vscode directory
Can solve this by either disabling the extension as suggested in previous answers, or excluding large directories (e.g. containing data) from its search path, by adding to your workspace settings an python.workspaceSymbols.exclusionPatterns key, like so:
settings.json:
{
"python.workspaceSymbols.exclusionPatterns": [
"**/site-packages/**",
"your_pattern_or_directory_to_exclude"
]
}
See also the vscode extension docs.
Here is how I solved this issue:
Go to File > Preferences > Settings > TYPE "python.language server"
If set to 'Microsoft' change your language server to 'Pylance'
Python: Language Server
Define Type of the Language Server
SELECT: Pylance
Reload your Visual Studio Code
Try importing your libraries causing the issues again:
import numpy as np
import pandas as pd

Categories

Resources