print('==', end='', flush=True)
the above code give the error "SyntaxError: invalid syntax" even though it was working fine? All the variables seem to have turned white rather than blue (which is what it used to be) in Visual Studio Code
Help please?!
Here is the whole source code (it's for a hangman game): https://www.codepile.net/pile/QmJE5BYO
I have tried to run your code, except for needing to add alreadyGuessedLetters = [], it works well.
It has not given me the error message of SyntaxError: invalid syntax. So could you provide the traceback of it? Such as a screenshot. And have you tried to restart the VSCode again?
I can't see the color of your code, Have you tried to switch the color theme?
You can take the command of Developer:Inspect Editor Tokens and Scopes to check the colorization ruler of the semantic token like this:
The module I am trying to import is pyinputplus. I do not know why it shows an error if the program imports the module and uses all of its functionality just fine.
This is the error message:
Cannot import 'pyinputplus' due to syntax error 'invalid syntax (<unknown>, line 268)'pylint(syntax-error)
I can do with it because it does not impair functionality but it really annoys me and distracts me when working in my IDE. Plus, it could be a sign that something is not setup right.
The problem is the order of the lines in the file.
The type of the function inputStr() is recognized correctly
..... means [snip] of the line(s)
def inputStr(
prompt="",
.....):
# type: (str, .....) -> Any
"""Prompts the user to enter .....
"""
For inputCustom() the type recognition is incorrect because the type line is incorrect
def inputCustom(
# type: (Callable, str, .....) -> Any
customValidationFunc,
prompt="",
.....):
"""Prompts the user to enter input. ....."""
it should be AFTER the argument list.
def inputCustom(
customValidationFunc, prompt="", .....):
# type: (Callable, str, .....) -> Any
"""Prompts the user to enter input. ....."""
Do the same for the function inputNum()
You can edit the __init__.py file and move the 2 comment lines to the correct position.
Then VSC will show the type in the tooltip.
I have created an Issue at pyinputplus for it.
Edit:
I have looked at why pylint shows this cryptic error and the problem is that the Python3.8 compiler reports this as a syntax error when you compile with type_comments=True. Pylint first tries to compile with type_comments=True but does not test the Python3.8 exception correct.
I have written a replacement for astroid/builder.py::_parse_string(). The line is not reported as an error anymore. Maybe when they use the rewrite, they will add an error for a misplaced type annotation.
I'm the creator of PyInputPlus. Thanks for pointing this out. I've corrected the type hints typo as of PyInputPlus 0.2.11. The solution is to upgrade by running pip install -U pyinputplus.
When importing pygame pylint is going crazy:
E1101:Module 'pygame' has no 'init' member
E1101:Module 'pygame' has no 'QUIT' member
I have searched the net and I have found this:
"python.linting.pylintArgs": ["--ignored-modules=pygame"]
It solves the problem with pygame, but now pylint is going crazy in other way: crazy_pylint.png.
Then I have found "python.linting.pylintArgs": ["--ignored-files=pygame"], but what it does is completely disabling pylint for the whole directory I am working in.
So how do I say pylint that everything is OK with pygame?
For E1101:
The problem is that most of Pygame is implemented in C directly. Now, this is all well and dandy in terms of performance, however, pylint (the linter used by VSCode) is unable to scan these C files.
Unfortunately, these same files define a bunch of useful things, namely QUIT and other constants, such as MOUSEBUTTONDOWN, K_SPACE, etc, as well as functions like init or quit.
To fix this, first things first, stop ignoring the pygame module by removing all your arguments in "python.linting.pylintArgs". Trust me, the linter can come in handy.
Now to fix the problems. For your constants (anything in caps), manually import them like so:
from pygame.constants import (
MOUSEBUTTONDOWN, QUIT, MOUSEMOTION, KEYDOWN
)
You can now use these without prepending them with pygame.:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
if event.type == KEYDOWN:
# Code
Next, for your init and other functions errors, you can manually help the linter in resolving these, by way of 2 methods:
Either add this somewhere in your code: # pylint: disable=no-member. This will deactivate member validation for the entire file, preventing such errors from being shown.
Or you can encase the line with the error:
# pylint: disable=no-member
pygame.quit()
# pylint: enable=no-member
This is similar to what the first method does, however it limits the effect to only that line.
Finally, for all your other warnings, the solution is to fix them. Pylint is there to show you places in which your code is either pointless, or nonconforming to the Python specs. A quick glance at your screenshot shows for example that your module doesn't have a docstring, that you have declared unused variables...
Pylint is here to aid you in writing concise, clear, and beautiful code. You can ignore these warnings or hide them (with # pylint: disable= and these codes) or spend a little time cleaning up everything.
In the long run, this is the best solution, as it'll make your code more readable and therefore maintainable, and just more pleasing to look at.
For a specific binary module you can whitelist it for pylint. For the pygame module it would be as follows:
{
"python.linting.pylintArgs": [
"--extension-pkg-whitelist=pygame"
]
}
OP You can also maintain the pylint pygame fix you found in vscode by including the vscode default arguments yourself.
The linter is going nuts (crazy_pylint.png) because you were clobbering the default pylint arguments with your own custom python.linting.pylintArgs.
The pygame module ignore fix does work, and the linter can return to non-crazy mode by also including the clobbered default arguments in your own custom python.linting.pylintArgs.
From the docs:
These arguments are passed whenever the python.linting.pylintUseMinimalCheckers is set to true (the default).
If you specify a value in pylintArgs or use a Pylint configuration file (see the next section), then pylintUseMinimalCheckers is implicitly set to false.
The defaults vscode passes according to this: https://code.visualstudio.com/docs/python/linting are:
--disable=all,
--enable=F,E,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode
So, here is how to pass all those defaults as well as the --ignored-modules=pygame in user settings within vscode:
"python.linting.pylintArgs": [
"--disable=all",
"--enable=F,E,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode",
"--ignored-modules=pygame"
]
Per #C._ comment above, he's definitely speaking truth; the linter will help!
I'm writing better code with it enabled for sure.
Also, I discovered that you can further fine-tune your pylinter with the enable line and comma delimited "readable pylint messages"
listed here: https://github.com/janjur/readable-pylint-messages/blob/master/README.md
So to not ignore also trailing-newlines, you would append the enable= list argument to include simply trailing-newlines.
I really hope this helps you OP :) It helped me!
Thanks for asking the question, and sharing --ignored-modules.
I am very new to python programming and debugging in pycharm. I want to find the value of a particular variable inside a if condition and have set a breakpoint , however the debugger shows that the variables are not available. Below is the screenshot of the code and the debugger:
I was facing the same issue and I fixed it by going through following steps :-
On the bottom left corner of your pycharm IDE, you will a settings option, click there and then go to "variable loading policy" and set this option to "on demand".
Hope it helps you.
It is because you're code isn't running yet. The print(...) line is inside the function, instead of outside the function. Your code can only run if something outside a function does something.
Fix: Just remove the tab in front of the print statement. Like so:
def factor(n):
# Your function body ...
for (...)
# Somewhere here is the return statement
print(factor(25)) # See the start of this `print` is inline with the `def`
I'm just getting started with Python and trying to get some easy code-examples to compile. I am using the 'Spyder' Editor and everytime I run code it shows 'runfile(...)' before the actual compiled code in the console.
Is there a way to prevent this behaviour?
Try including this instead immediately prior to your code. The terminal will now return a clean code only response:
cls = lambda: print("\033[2J\033[;H", end='')
cls()
you are trying to run the code, instead go to settings, keyboard shortcuts, and look for "run selection" it will have a shortcut assigned to it
Now select all the code and use the shortcut
it will only give you in and out