python-3.xmodulenotfounderrorpymodbus

pymodbus package import error - modulenotfound


Raspberry Pi 4 running Raspian (most current as of 02/09/2023), Python 3.9.2. Installed Pymodbus with pip3 install -U pymodbus[twisted] (detail: it said twisted was not available).

When I run my code in Python I get the error ModuleNotFoundError: No module named 'pymodbus.server.asynchronous' on the line from pymodbus.server.asynchronous import StartTcpServer, StopServer.

Any idea as why it is not working? Same process used to work in 2021 with Python 3.7.

Installed Pymodbus from this command pip3 install -U pymodbus[twisted]

EDIT: I read the official page for Pymodbus https://pypi.org/project/pymodbus/ which says that the current version of it is compatible with Python 3.8+ , and the old versions of it were compatible with up to Python 3.7 (which was the version I was using). Could this be breaking the code and giving me the error i'm getting?

EDIT 2: Full traceback

    Traceback (most recent call last):
  File "/home/pi/mohm-lhf/Program2.py", line 11, in <module>
    from communication import MODBUS
  File "/usr/lib/python3/dist-packages/shiboken2/files.dir/shibokensupport/__feature__.py", line 142, in _import
    return original_import(name, *args, **kwargs)
  File "/home/pi/mohm-lhf/communication.py", line 8, in <module>
    import modbus_server as server
  File "/usr/lib/python3/dist-packages/shiboken2/files.dir/shibokensupport/__feature__.py", line 142, in _import
    return original_import(name, *args, **kwargs)
  File "/home/pi/mohm-lhf/modbus_server.py", line 13, in <module>
    from pymodbus.server.asynchronous import StartTcpServer, StopServer
  File "/usr/lib/python3/dist-packages/shiboken2/files.dir/shibokensupport/__feature__.py", line 142, in _import
    return original_import(name, *args, **kwargs)
ModuleNotFoundError: No module named 'pymodbus.server.asynchronous'

Solution

  • Twisted is removed from 3.0.0dev5, and I can see that you used this import line in your code:

    from pymodbus.server.asynchronous import StartTcpServer, StopServer
    

    This is an old implementation working only for 2.5.3<, there is a new way of implementing an async server - check here the example.