Import serial doesnt work after using pip install pyserial

Viewed 2177

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

2 Answers

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.

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.

Related