pythonarduinopyfirmata

Import pyfirmata could not be resolved


I tried importing pyfirmata module and it does not recognise it. This is th error i get when hovering over it: "Import "pyfirmata" could not be resolved Pylance(reportMissingImports)" This is the code:

from pyfirmata import Arduino, util
import time

board = Arduino("COM5")

# print(board.get_firmata_version())

for x in range(10):
    board.digital[13].write(1)
    time.sleep(0.2)
    board.digital[13].write(0)
    time.sleep(0.5)


I tried reinstalling the module, changing the com, running it with the board plugged and unplugged.


Solution

  • Assuming that you have installed pyFirmata successfully using the command pip install pyFirmata, it seems that you may have encountered a misconfiguration issue with the Pylance extension in Visual Studio Code related to the Python path.

    To resolve this issue, you can set the correct path by opening the command palette in Visual Studio Code using the shortcut Ctrl+Shift+P. Next, type in settings.json and select it from the search results. This will open the settings.json file where you can add the following code snippet:

    {
        "python.defaultInterpreterPath": "C:/Users/Maisu/AppData/Local/Programs/Python/Python311/python.exe"
    }
    

    Make sure to replace the path in the code snippet with the correct path where your Python interpreter is installed. After adding this code to the settings.json file and saving it, Pylance should be able to detect your Python installation correctly.

    Finally, you should install pyFirmata again, and the error should be gone.