'Import serial doesnt work after using pip install pyserial

After installing pyserial, I still get the error: "ModuleNotFoundError: No module named 'serial'"

This is the beginning of the code:

import serial  
import time  
import serial.tools.list_ports  

And these are the problems:

Import "serial" could not be resolved Pylance(reportMissingImports) [1, 8]  
Import "serial.tools.list_ports" could not be ... Pylance(reportMissingImports) [3, 8]  

Does anyone know how I can fix this?
Thanks in advance



Solution 1:[1]

Agree with @quamrana.

You can check which pip you are using through pip --version, and which python you are using through where python(cmd) or get-command python(PowerShell). Make sure pip matches up with the python interpreter you are using.

If it's not, you can try to reopen the terminal with the shortcut 'Ctrl+Shift+`'. And then to check the pip version again.

If it still doesn't matches up with the interpreter. That's maybe this environment hasn't installed the pip in this environment.

Solution 2:[2]

pyserial is an annoying package.
Although you install it using python3 -m pip install pyserial you need to import it using import serial. Linters have a hard time seeing that the installed package pyserial corresponds to the module serial you are trying to import.

In your specific case I'd suggest editing VSCode's settings.json and adding:

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

This suppresses the Missing Imports warning.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Mausy5043