0

Everything in my code was working perfectly fine up until I decided I wanted to utilize match-case in Python. Find out its only a thing in 3.10+, so I quickly install it and change it to be the interpreter in command palette.

Then I try to run my code same as before, and I'm not sure what changed but my Keyboard import is giving me 'Import "keyboard" could not be resolved'. Issues. The same issue was actually present as well with the 'from nis import match' module.

I installed keyboard initially using 'pip install keyboard' when running my 3.9 version, and legit everything was fine. This all started after I installed 3.10 (which I did from Pythons website yes); and I did add to PATH, but I dont think that would have any impact on my imports in VScode.

Confused as heck right now, please look at my screenshots for clearest explanation of what I'm facing. Import could not be resolved . Module Not Found Error . pip3.10 show keyboard . My interpreter list from command palette

Any and all help appreciated, I'm extremely confused and think I've tried it all now

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Saad.A
  • 33
  • 1
  • 9
  • Have you tried re-installing keyboard using `pip install keyboard`? – The Amateur Coder Jan 24 '22 at 04:18
  • Does this answer your question? [Python 3.10 won't import modules](https://stackoverflow.com/questions/70696871/python-3-10-wont-import-modules) – Bibhav Jan 24 '22 at 04:40
  • Of course, your libraries are tied to the specific interpreter – juanpa.arrivillaga Jan 24 '22 at 04:42
  • 2
    @TheAmateurCoder yes I did try that in fact, but it just confirms that I already have it. ```Requirement already satisfied: keyboard in c:\users\myname\appdata\local\packages\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\localcache\local-packages\python310\site-packages (0.13.5)``` – Saad.A Jan 24 '22 at 05:15

3 Answers3

1

New Python versions use new site-packages folders. You need to reinstall everything. This is why poetry, pipenv, or requirements.txt are used

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 2
    So do you suggest I uninstall python from the ground up? In Vscode too? Or are you specifically saying the modules? Since those I have already tried to do, but as mentioned in the above comments below my post, It just confirms those module installs already exist – Saad.A Jan 24 '22 at 05:18
  • You shouldn't need to uninstall anything. All I'm saying is that new Python installations don't carry over previously installed modules. If anything, I'd suggest creating a venv for your projects. Do you actually get the same error when running the code and ignoring what vscode linter says? – OneCricketeer Jan 24 '22 at 14:39
  • Well considering its showing 'modulenotfounderror', and not running my code, I can confirm ignoring it is not an option. Ill look into a venv i guess, but thats not an optimal solution to this bizarre problem. Was working just fine prior to installing 3.10.2 – Saad.A Jan 25 '22 at 01:05
1

I think the linter-like extension/whatever Pylance is is the problem. Why? Because as we've tried, pip install keyboard and pip install --upgrade keyboard just confirm that the requirement is satisfied and the latest version of keyboard is already installed.

What I did was change Pylance's settings. Yes, it's not an actual error (with the code), but Pylance's problem. It just didn't see enough files in the keyboard package to satisfy its hunger, or Microsoft was just too lazy to pass more checks and update it accordingly, because however it happened, when I tried changing the setting to display an "error" at "missing imports" for fun, it didn't show errors or the default "warning" but "none", because I had run the file before I changed it back to display an error (maybe Pylance observed that when the file was run, there was no ModuleNotFoundError, and so stopped showing the warning).

I better shut now, because the setting I changed/overrode by adding it in settings.json is:

"python.analysis.diagnosticSeverityOverrides": {"reportMissingImports":"none"}

which is in:

{
    ...,
    ...,
    ...,
    "python.analysis.diagnosticSeverityOverrides": {"reportMissingImports":"none"}
}

Here is a list of keys like "reportMissingImports" whose values you can change. These are the allowed values for the keys in python.analysis.diagnosticSeverityOverrides:

  • error (red squiggle)
  • warning (yellow squiggle)
  • information (blue squiggle)
  • none (disables the rule)

You would want to change their values only if you want to change their behaviour.

You can find settings.json by its path or by going to the settings GUI and clicking on any "Edit in settings.json" link-like button. You can also make settings.json open instead of the GUI by default.

Now, you won't see any more warnings when you import modules and can code without being anxious about the stupid warning.

The Amateur Coder
  • 789
  • 3
  • 11
  • 33
  • 1
    Thanks for the lengthy comment, however this is from my understanding and following of steps listed above, just a way to 'hide' the problem. I did change the setting in settings.json as mentioned, and it did get rid of the 'yellow squiggle' underneath the keyboard import. However running it provides the same issue regardless, of 'ModuleNotFoundError: No Module named 'keyboard''. So the problem at hand is flawed to its core. – Saad.A Jan 25 '22 at 13:13
1

Wow, So as it turns out, a solution I tried previously, to no avail is now working to solve this bizarre issue.

Simply put, I once again went to Command Palette (Ctrl+Shft+P), and looked through my interpreters I had to see what could be wrong.

Decided to click on the 'recommended' option I assumed I had been running this whole time. Turns out I was using this 'third' option as shown in the screenshot (the one not highlighted ofcourse); and it's the reason my module installs were being found on my machine, but not by the interpreter; as such giving me errors and not running the module for the program.

Simple error, but thanks to those who did help. Interpreter I'm Using Now

Saad.A
  • 33
  • 1
  • 9