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

22.1k Views Asked by At

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:\Users\Public\pythonpublic\arduino.py", line 5, in <module>
    board = Arduino(port)
            ^^^^^^^^^^^^^
  File "C:\Users\marce\AppData\Roaming\Python\Python311\site-packages\pyfirmata\__init__.py", line 19, in __init__      
    super(Arduino, self).__init__(*args, **kwargs)
  File "C:\Users\marce\AppData\Roaming\Python\Python311\site-packages\pyfirmata\pyfirmata.py", line 101, in __init__    
    self.setup_layout(layout)
  File "C:\Users\marce\AppData\Roaming\Python\Python311\site-packages\pyfirmata\pyfirmata.py", line 157, in setup_layout
    self._set_default_handlers()
  File "C:\Users\marce\AppData\Roaming\Python\Python311\site-packages\pyfirmata\pyfirmata.py", line 161, in _set_default_handlers
    self.add_cmd_handler(ANALOG_MESSAGE, self._handle_analog_message)
  File "C:\Users\marce\AppData\Roaming\Python\Python311\site-packages\pyfirmata\pyfirmata.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'?
3

There are 3 best solutions below

0
On BEST ANSWER

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.

0
On

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.

The 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. Note that Python 3.7 is end of life though.

  • 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 (I have already done that as well). In 2022, the issue is reported again by another user.

  • Clone the library and fix it yourself (and create a Pull Request to get it in the official library). UPDATE: This has been done already — PR #122 fixes this issue and was merged to master in September 2022. Unfortunately, no new pip release has been made, so you'd have to checkout the source instead of installing using pip to use this.

  • Find another, similar, library that does work with Python 3.11. For example, pyfirmata2 is an updated version of pyFirmata which is being actively supported at time of writing (November 2023) and does not have this issue.

  • Write the code yourself.

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

1
On

Replace

len_args = len(inspect.getargspec(func)[0])

with

len_args = len(inspect.getfullargspec(func)[0]