问题
I had this code:
while True:
cmd = input('> ')
if cmd == 'exit':
break
But I wanted to implement advanced text input features like command history so I imported the readline module. Importing the readline module (and not even using it) will unlock these features. This code works perfectly:
import readline
while True:
cmd = input('> ')
if cmd == 'exit':
break
My problem (or maybe just annoyance) is that PyCharm gives me a non-fatal warning that I have an unused import statement. I assume this is just a simple fault in PyCharm for not realizing that the readline import WILL be used if you use the builtin input function.
What is the cleanest way for me to get rid of this warning? Also, is this a bug that PyCharm ought to fix?
回答1:
PyCharm linting only goes so far, but you can at least supress the warning by writing:
# noinspection PyUnresolvedReferences
import readline
You can access general warning configurations in Settings > Editor > Inspections
.
Also if you ever need to suppress a warning type, but just for a single line/function instead of the whole file, do this:
- place the cursor where the warning is
- press Alt+Enter
- press right arrow key on the "light bulb icon"
- press Enter on the "suppress for class/function/statement"
PyCharm automatically adds the appropriate comment that ignores the warning type caused by that statement.
来源:https://stackoverflow.com/questions/61279148/pycharm-says-readline-import-not-being-used