Digital AIUpdated 21 October 20268 min read

Python for Automation Engineers: PLC Data, OPC UA and Analysis

Python does not replace ladder logic or structured text, but it is excellent for pulling data out of PLCs, analysing it and automating reports. This guide covers the maintained libraries for S7, Modbus and OPC UA, plus the security and safety limits every engineer should know.

PythonIndustrial AutomationData AnalysisOPC UAModbusSiemens S7
Electrician in a blue hard hat checking wiring inside a tall industrial control cabinet

Key takeaways

  • Use Python beside the PLC for data collection, analysis and reporting; keep control and safety logic in the controller.
  • python-snap7 works on S7-1200 and S7-1500 only with PUT/GET enabled and cannot read optimised data blocks.
  • The project README warns that PUT/GET is unauthenticated and disabled by default; OPC UA or Modbus is a better production route.
  • The OPC UA library installs as asyncua, and pycomm3 is no longer actively developed.
  • Python was used by 57.9% of respondents in the 2025 Stack Overflow Developer Survey, so learning resources are abundant.

Python will not replace ladder logic or structured text on the PLC, but it is the most practical tool for what happens around the PLC: reading data from controllers, analysing logs, automating reports and prototyping condition monitoring. In the 2025 Stack Overflow Developer Survey, Python was used by 57.9% of all respondents, so help and libraries are plentiful. Use it beside the PLC, not inside the control loop.

Why would a PLC engineer learn Python?

Because the data that explains a problem usually sits outside the PLC. Production logs, alarm exports, historian files and CSV downloads need cleaning, joining and plotting, and Python does that in a few lines. It is also the language used by most data-science and machine-learning libraries, so it is the common ground when an automation team works with data engineers.

Good first uses:

  • Production and alarm analysis. Load an alarm export, count alarms per hour, find the top ten nuisance alarms.
  • Automated reports. Turn a daily log into an Excel or PDF summary.
  • Signal analysis. Filter noisy sensor data and look at vibration in the frequency domain with SciPy.
  • Test benches and data collection. Poll a PLC or sensor at a fixed rate and write to a file or database.
  • Prototyping. Test an idea on logged data before building it in the PLC or SCADA.

Which Python libraries talk to industrial devices?

Four libraries cover most cases. Library status changes, so check each project page before relying on it.

TaskLibraryWhat to know (checked October 2026)
Siemens S7 PLC accesspython-snap7 (import name s7 or snap7)Version 3.3.0. Pure Python, supports S7-300/400 and S7-1200/1500 with PUT/GET enabled
Modbus TCP and RTUpymodbusVersion 3.16.1. Client, server and simulator; sync and async
OPC UA client and serverasyncuaVersion 2.1.0. Project name opcua-asyncio on GitHub; install as asyncua
Allen-Bradley EtherNet/IPpycomm3Version 1.2.16, but the README says it is no longer actively developed
Data analysispandas, NumPy, SciPyTables, arrays and signal processing
ChartsMatplotlib, PlotlyStatic and interactive plots

Note two corrections to common advice. The OPC UA library is installed as asyncua, not "opcua-asyncio" (that is the repository name). And pycomm3 is no longer actively developed, so evaluate alternatives before using it in new work.

A Siemens SIMATIC S7-1200 programmable controller, one of the controller families python-snap7 can read when PUT/GET access is enabled
A Siemens SIMATIC S7-1200 programmable controller, one of the controller families python-snap7 can read when PUT/GET access is enabledPhoto: UlrichAAB, CC BY-SA 3.0, via Wikimedia Commons

How do you read a Siemens PLC from Python?

Use python-snap7 on a test or non-critical controller, and understand the security trade-off first. The project README gives this minimal pattern, shown here as plain lines:

  • from s7 import Client
  • client = Client()
  • client.connect("192.168.1.10", 0, 1)
  • data = client.db_read(1, 0, 4)
  • client.disconnect()

The arguments are the IP address, rack and slot, then data block number, start byte and length. The call returns raw bytes that you decode yourself, for example with Python's standard struct module for a REAL or DINT.

Three cautions from the project documentation:

  1. On S7-1200 and S7-1500 it only works when PUT/GET access is enabled in TIA Portal, and it cannot read optimised data blocks. You must switch off optimised block access for the data block you want.
  2. The README states that PUT/GET is unauthenticated and disabled by default, so leaving it disabled is the more secure configuration.
  3. If PUT/GET is off, or you need symbolic access, the project points to a separate library, s7commplus, which is younger and has firmware limits. For anything beyond a lab, OPC UA (below) is usually the better route.

Do not enable PUT/GET on production safety-related or critical controllers just to run a script. Agree it with whoever owns the plant's cybersecurity and change control.

How do you use Modbus and OPC UA from Python?

Both are straightforward and both are better choices than raw S7 access for production data collection. The pymodbus README shows a Modbus TCP client in four lines:

  • from pymodbus.client import ModbusTcpClient
  • client = ModbusTcpClient('MyDevice.lan')
  • client.connect()
  • result = client.read_coils(1,1)
  • client.close()

Method signatures changed between pymodbus major versions, so match the examples to the installed version.

For OPC UA, the asyncua README shows the client as an asynchronous context manager: import Client from asyncua, open "async with Client(url='opc.tcp://localhost:4840/freeopcua/server/') as client", then get a node with client.get_node and read it with await node.read_value(). Use certificates and signed, encrypted endpoints when connecting to a real server. Our OPC UA guide explains the security settings.

OPC UA with Python

What does a Python data workflow look like?

A reliable pattern separates collection, storage and analysis, so that each can fail independently.

  1. Collect. A small service polls the PLC (via OPC UA or Modbus) or subscribes to changes, and writes timestamped rows to a file or database.
  2. Clean. In pandas, set a datetime index, remove duplicates, handle gaps and convert units.
  3. Analyse. Resample to a useful interval, compute rolling statistics, flag outliers or find cycle times.
  4. Report. Produce a chart, a spreadsheet or a short email summary on a schedule.
  5. Decide. Humans or the PLC logic act on the result. Keep the control decision in the controller.

What are the limits of Python in industry?

Python is a general-purpose, interpreted language, so keep it away from jobs that need deterministic timing or certified safety.

  • Not real-time. Operating-system scheduling, garbage collection and network latency make Python unsuitable for control loops measured in milliseconds.
  • Not a safety function. Safety logic belongs in certified safety controllers, not scripts.
  • Maintenance risk. A forgotten script on a laptop becomes an unsupported production dependency. Put code in version control, document the connection details and have an owner.
  • Cybersecurity. Scripts that write to PLCs are a change-control and security matter. Prefer read-only access and least-privilege accounts.

IEC 61131-3 languages and Python are complementary: the PLC runs the machine, and Python helps you understand it.

How do I start learning?

Start with data analysis, not PLC writes. Install Python, learn pandas and Matplotlib on a downloaded CSV of alarm or sensor data, then add pymodbus against a simulator before touching any live controller. Practise the PLC side in free simulators first: the PLC simulator and ladder logic simulator run in the browser with no install.

What does a worked example look like?

Suppose a line generates an alarm export every shift and operators complain about nuisance alarms. A short script can load the CSV with pandas, convert the timestamp column to datetimes, group by alarm tag and count occurrences, then sort the result so the ten noisiest alarms appear first. Plot the counts per hour with Matplotlib and you can see whether alarms cluster around shift changes or a particular product changeover. The result is evidence to take to the engineering team, and no PLC was touched. This read-only pattern is the safest and most valuable place to begin.

How do you keep Python projects maintainable?

Treat scripts as small software products. Keep them in version control, pin library versions in a requirements file because APIs change between major releases, store connection settings in a configuration file rather than in the code, and write down which PLC addresses or OPC UA nodes each script reads. Log errors, add timeouts so a dead network does not hang the program, and name an owner who will keep it running. These habits matter more than clever code when a script becomes part of how a plant is monitored.

How EDWartens can help

EDWartens UK does not currently teach Python for industrial use in the Professional Module, and we do not claim otherwise. The Professional Module is a five-day live online class in Siemens TIA Portal (PLC, HMI, WinCC SCADA) with self-paced Basic Engineering, and the Python skills in this article build on that PLC foundation. The course is online and simulation-based, and its CPD certificate is not a regulated qualification. See the training overview or create a free account. Related: AI in manufacturing and predictive maintenance.

Start free

Learn industrial data free, then practise the job

Free job simulation

Data Analyst

Clean real-looking data, answer the business question and present it.

The first five days are free, no card needed.

Live UK jobs

Data and analytics jobs in the UK

Updated daily, with licensed visa sponsors marked.

Browse all engineering jobs

Frequently asked questions

Can Python program a PLC?

Not in the usual sense. PLCs run IEC 61131-3 languages such as ladder, structured text and function block diagram, and Python is not one of them on mainstream controllers. Python is used outside the PLC to read and write data, analyse logs, build reports and test ideas, while the controller keeps the real-time control.

How do I connect Python to a Siemens S7-1200 or S7-1500?

Either use python-snap7, which needs PUT/GET access enabled in TIA Portal and non-optimised data blocks, or use OPC UA with a library such as asyncua if your CPU provides an OPC UA server. OPC UA is generally the better production choice because it supports certificates and encryption.

Is it safe to enable PUT/GET on a PLC?

It reduces security. The python-snap7 documentation says PUT/GET is unauthenticated and is disabled by default, so leaving it off is the more secure configuration. If you need it for a lab or test controller, isolate the network and agree it with the plant's cybersecurity owner. Avoid it on critical systems.

Which Python libraries are used in industrial automation?

Common choices are python-snap7 for Siemens S7, pymodbus for Modbus TCP and RTU, asyncua for OPC UA, and pandas, NumPy, SciPy and Matplotlib for analysis. pycomm3 reads Allen-Bradley controllers over EtherNet/IP, but its README says it is no longer actively developed, so check alternatives for new projects.

Should I learn Python or structured text first?

Learn PLC fundamentals first, including ladder logic and structured text, because that is what controls machines and appears in most job adverts. Add Python afterwards for data and reporting work. Structured text also builds the programming habits that make Python easier to pick up.

Sources

  1. python-snap7 README (GitHub)
  2. python-snap7 on PyPI
  3. pymodbus README (GitHub)
  4. asyncua / opcua-asyncio README (GitHub)
  5. asyncua on PyPI
  6. pycomm3 README (GitHub): no longer actively developed
  7. Stack Overflow Developer Survey 2025: Technology

Share this article

About the author

EDWartens Team

EDWartens Instructional Team

The EDWartens instructional team brings together UK-based automation engineers, certified Siemens and Rockwell specialists, and industry practitioners with decades of combined experience in PLC programming, SCADA engineering, and industrial AI.

View all articles by EDWartens

Ready to become job-ready?

Start with a free course today, or train live in Siemens PLC, HMI and SCADA on the Professional Module.