Registry / http-networking / python-snap7

python-snap7

JSON →
library3.0.0pypypi✓ verified 87d ago

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.

pip install python-snap7
INSTALL
IMPORT
SIG · PYTHON-SNAP7
P
python-snap7
http-networkingpythonv3.0.0
Install
1.6s avg
Import
155ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v3.0.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.155s · 18.5MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 1.6s · import 0.154s · 19MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Client
from snap7.client import Client
import snap7.snap7
As of v3.0.0, the C library wrapper `snap7.snap7` is removed. Use `snap7.client.Client`.

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.

import snap7 from snap7.snap7types import S7AreaDB import os # Configure PLC connection details plc_ip = os.environ.get('PLC_IP', '192.168.0.100') # Placeholder IP plc_rack = int(os.environ.get('PLC_RACK', '0')) plc_slot = int(os.environ.get('PLC_SLOT', '1')) client = snap7.client.Client() try: client.connect(plc_ip, plc_rack, plc_slot) if client.get_connected(): print(f"Successfully connected to PLC at {plc_ip}") # Example: Read 10 bytes from Data Block (DB) number 10, starting at byte 0 db_number = 10 start_byte = 0 size = 10 data_buffer = client.db_read(db_number, start_byte, size) print(f"Read from DB{db_number}, Start:{start_byte}, Size:{size}: {data_buffer}") # Example: Write 10 bytes to Data Block (DB) number 10, starting at byte 0 # Make sure the PLC is configured to allow writes. # new_data = bytearray([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A]) # client.db_write(db_number, start_byte, new_data) # print(f"Wrote to DB{db_number}, Start:{start_byte}, Size:{size}: {new_data}") else: print("Failed to connect to PLC.") except Exception as e: print(f"An error occurred: {e}") finally: if client.get_connected(): client.disconnect() print("Disconnected from PLC.")
Debug
Known issues
breakingVersion 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.
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.
affects: >=3.0.0
breakingVersion 2.0.0 introduced significant code restructuring with potential, though not fully documented, API changes for method arguments and internal types.
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.
affects: >=2.0.0, <3.0.0
gotchaOlder 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.
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.
affects: <3.0.0
gotchaSiemens 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.
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.
affects: all
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'snap7.snap7'
Attempting to import the old C-binding wrapper module (`snap7.snap7`) which was removed in version 3.0.0.
fix
Update your import statements to use the new pure Python client: `from snap7.client import Client`.
snap7.snap7exceptions.Snap7Exception: error while connecting: 10060
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.
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.
TypeError: 'str' object cannot be interpreted as a buffer
Passing a string directly to functions expecting a bytearray or bytes object (e.g., `db_write`, `mb_write`).
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')`.
Upgrade
Version history
3.0.0latest on PyPI · released Mar 18, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
python-snap7 — pip install python-snap7 · libregistry