pyFirmata gives error: module 'inspect' has no attribute 'getargspec'

Question:

I’m trying to use pyFirmata, but I can’t get it to work. Even the most basic of the library does not work. I guess there is something wrong with the library code.

from pyfirmata import Arduino,util
import time

port = 'COM5'
board = Arduino(port)

I get this error:

Traceback (most recent call last):
  File "c:UsersPublicpythonpublicarduino.py", line 5, in <module>
    board = Arduino(port)
            ^^^^^^^^^^^^^
  File "C:UsersmarceAppDataRoamingPythonPython311site-packagespyfirmata__init__.py", line 19, in __init__      
    super(Arduino, self).__init__(*args, **kwargs)
  File "C:UsersmarceAppDataRoamingPythonPython311site-packagespyfirmatapyfirmata.py", line 101, in __init__    
    self.setup_layout(layout)
  File "C:UsersmarceAppDataRoamingPythonPython311site-packagespyfirmatapyfirmata.py", line 157, in setup_layout
    self._set_default_handlers()
  File "C:UsersmarceAppDataRoamingPythonPython311site-packagespyfirmatapyfirmata.py", line 161, in _set_default_handlers
    self.add_cmd_handler(ANALOG_MESSAGE, self._handle_analog_message)
  File "C:UsersmarceAppDataRoamingPythonPython311site-packagespyfirmatapyfirmata.py", line 185, in add_cmd_handler
    len_args = len(inspect.getargspec(func)[0])
                   ^^^^^^^^^^^^^^^^^^
AttributeError: module 'inspect' has no attribute 'getargspec'. Did you mean: 'getargs'?
Asked By: clueless

||

Answers:

According to the first line of pyFirmata docs:

It runs on Python 2.7, 3.6 and 3.7

You are using Python 3.11. The inspect (core library module) has changed since Python 3.7.

Answered By: Le Minaw

As already pointed out in another answer, the pyFirmata modules is currently documented to run on Python 2.7, 3.6 and 3.7. This doesn’t mean it won’t work on other versions, but probably that it hasn’t been tested on other versions by the author and it isn’t officially supported. So it may or may not work on newer Python versions.

Your error message is caused by a missing function inspect.getargspec(). This function is part of the Python Standard Library, but has been deprecated since Python 3.0 (which came out in 2008). Unfortunately the author wasn’t aware of this or simply didn’t bother to fix it, so now the code doesn’t work anymore with the latest version of Python.

In the Python documentation, you can see that the function is still available in version 3.10, but not in version 3.11.

To solve this you have a number of options:

  • Downgrade to Python 3.10, which is currently still a good option (Python 3.10 is "alive" until 2026-10-04). I don’t know if all other functionality does work. I guess it will, but you would have to find out yourself.
  • Downgrade to Python 3.7, which is claimed to be supported. Given that Python 3.7 is also still alive (until 2023-06-27), that’s a reasonable option as well.
  • Create an issue for the pyFirmata module and hope the author will fix the problem. Note that an issue has already been created by someone in 2019 but apparently without effect. You could leave a comment there confirming that this has now broken for real.
  • Clone the library and fix it yourself (and create a Pull Request to get it in the official library).
  • Find another, similar, library that does work with Python 3.11.
  • Write the code yourself.

Downgrading to a Python version between 3.7 and 3.10 is certainly the simplest option, and leaving some feedback to the author will give you a chance it will be fixed in the future, in case your planning to use your script for a longer time.

Answered By: wovano

I can confirm pyFirmata works with Python 3.10.10 but not 3.11.x

Answered By: Luke E