{"id":7632,"library":"python-snap7","title":"python-snap7","description":"python-snap7 is a pure Python S7 communication library for Siemens PLCs, implementing the full protocol stack (TPKT, COTP, S7) in Python. As of version 3.0.0, it no longer requires the underlying Snap7 C library. It provides an interface for connecting to and interacting with Siemens S7-300, S7-400, S7-1200, and S7-1500 PLCs. The library is actively maintained with a moderate release cadence, with major versions introducing significant architectural changes.","status":"active","version":"3.0.0","language":"en","source_language":"en","source_url":"https://github.com/gijzelaerr/python-snap7","tags":["plc","siemens","s7","industrial","automation","communication"],"install":[{"cmd":"pip install python-snap7","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"note":"As of v3.0.0, the C library wrapper `snap7.snap7` is removed. Use `snap7.client.Client`.","wrong":"import snap7.snap7","symbol":"Client","correct":"from snap7.client import Client"}],"quickstart":{"code":"import snap7\nfrom snap7.snap7types import S7AreaDB\nimport os\n\n# Configure PLC connection details\nplc_ip = os.environ.get('PLC_IP', '192.168.0.100') # Placeholder IP\nplc_rack = int(os.environ.get('PLC_RACK', '0'))\nplc_slot = int(os.environ.get('PLC_SLOT', '1'))\n\nclient = snap7.client.Client()\n\ntry:\n    client.connect(plc_ip, plc_rack, plc_slot)\n    if client.get_connected():\n        print(f\"Successfully connected to PLC at {plc_ip}\")\n\n        # Example: Read 10 bytes from Data Block (DB) number 10, starting at byte 0\n        db_number = 10\n        start_byte = 0\n        size = 10\n        data_buffer = client.db_read(db_number, start_byte, size)\n        print(f\"Read from DB{db_number}, Start:{start_byte}, Size:{size}: {data_buffer}\")\n\n        # Example: Write 10 bytes to Data Block (DB) number 10, starting at byte 0\n        # Make sure the PLC is configured to allow writes.\n        # new_data = bytearray([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A])\n        # client.db_write(db_number, start_byte, new_data)\n        # print(f\"Wrote to DB{db_number}, Start:{start_byte}, Size:{size}: {new_data}\")\n\n    else:\n        print(\"Failed to connect to PLC.\")\n\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\nfinally:\n    if client.get_connected():\n        client.disconnect()\n        print(\"Disconnected from PLC.\")","lang":"python","description":"This quickstart demonstrates how to connect to a Siemens PLC using `snap7.client.Client`, read 10 bytes from Data Block (DB) 10, and then disconnect. Ensure the PLC's IP address, rack, and slot are correctly configured. Remember to enable PUT/GET communication on your PLC for the library to function correctly."},"warnings":[{"fix":"Update your code to use `from snap7.client import Client` instead of `import snap7.snap7` or `from snap7 import snap7`. Remove any manual C library installations (`libsnap7.so`, `snap7.dll`). Consult the v3.0.0 documentation for updated API specifics.","message":"Version 3.0.0 is a complete rewrite in pure Python, removing the dependency on the Snap7 C library. This changes internal architecture and some import paths.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Review the official documentation and changelog for `python-snap7` v2.x to v3.x migration. Pay close attention to method signatures and data type handling, especially if upgrading from pre-2.0 versions.","message":"Version 2.0.0 introduced significant code restructuring with potential, though not fully documented, API changes for method arguments and internal types.","severity":"breaking","affected_versions":">=2.0.0, <3.0.0"},{"fix":"For versions prior to 3.0.0, ensure the Snap7 C library is correctly installed for your operating system and its location is known to your environment. For v3.0.0+, this is no longer necessary.","message":"Older versions of `python-snap7` (pre-3.0.0) required the Snap7 C shared library (`libsnap7.so` on Linux, `snap7.dll` on Windows) to be installed and accessible in the system's PATH or library search paths.","severity":"gotcha","affected_versions":"<3.0.0"},{"fix":"Check your PLC's TIA Portal project settings for the CPU and ensure PUT/GET communication is enabled. Also verify firewall rules on both the PLC network and the client machine.","message":"Siemens PLCs often require specific settings (e.g., 'Permit access with PUT/GET communication from remote partner (PLC, HMI, SCADA)') to allow external communication. Without this, `python-snap7` connections will fail.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Update your import statements to use the new pure Python client: `from snap7.client import Client`.","cause":"Attempting to import the old C-binding wrapper module (`snap7.snap7`) which was removed in version 3.0.0.","error":"ModuleNotFoundError: No module named 'snap7.snap7'"},{"fix":"1. Verify `plc_ip`, `plc_rack`, `plc_slot` are correct. 2. Ping the PLC's IP address. 3. Ensure PUT/GET communication is enabled in the PLC's TIA Portal project. 4. Check network firewalls on both the client and PLC side.","cause":"This usually indicates a connection timeout or refusal. Common reasons include incorrect IP/rack/slot, PLC not reachable, PLC not configured for PUT/GET communication, or firewall blocking the connection.","error":"snap7.snap7exceptions.Snap7Exception: error while connecting: 10060"},{"fix":"Convert your string data to a `bytearray` or `bytes` object before passing it to the write functions. Example: `data_to_write = bytearray(b'Hello')` or `data_to_write = 'Hello'.encode('ascii')`.","cause":"Passing a string directly to functions expecting a bytearray or bytes object (e.g., `db_write`, `mb_write`).","error":"TypeError: 'str' object cannot be interpreted as a buffer"}]}