IMD SolutionComputing LibraryShop main store

Troubleshooting

Why a script engine fails to import paho-mqtt for MQTT TLS

Edge computing and IIoT Published 8 min read By IMD Solution engineering team

Short answer

The engine stops with ModuleNotFoundError: No module named 'dns' because paho-mqtt imports dns.resolver, supplied by dnspython, and the engine's Python environment lacks it — no network or TLS call has happened yet. Deploy dnspython through the engine's own library path and prove it with an import test run by the engine. Then fix certificate verify failed separately: give the engine the self-signed certificate or private CA as a trust anchor, match the broker hostname to the certificate, and keep verification enabled.

On this page
  1. Why does the script engine stop with No module named 'dns'?
  2. Why did the same script work in the IDE interpreter?
  3. How do you install dnspython where the script engine can load it?
  4. Why does certificate verify failed appear only after the import passes?
  5. How do you make the script engine trust a self-signed MQTT certificate?
  6. Does the broker hostname have to match the certificate?
  7. How do you confirm the fix end to end?
  8. Questions asked

Your script engine is failing twice, at two separate stages, and the traceback tells you which one you are in. ModuleNotFoundError: No module named 'dns' happens while the MQTT client library loads: its client module imports dns.resolver, the dns package comes from the dnspython distribution, and the engine's Python environment does not have it. Nothing has reached the network yet — no socket, no port, no certificate. The output that looked like a working publish came from the IDE's Python interpreter, which is a different runtime. Fix the environment first. Then treat certificate verify failed as its own fault: give the engine the self-signed certificate or your private CA as a trust anchor, connect by a name the certificate lists, and keep verification enabled.

Why does the script engine stop with No module named 'dns'? #

Because the engine's Python environment is missing the dnspython package: the MQTT client library imports dns.resolver while it loads, and with no dns package present Python raises ModuleNotFoundError before your script reaches any MQTT code.

The traceback names two files in order, and that order is the whole diagnosis. Your script's frame comes last: line 3 runs from paho.mqtt import client as mqtt_client. Above it is the library copy bundled with the engine, paho/mqtt/client.py, and its frame shows the failure at line 72, on import dns.resolver. Your import triggered the library's import, the library's import failed, and the engine never reached any connection code. A missing Python module has nothing to do with your plant's name resolution — installing or reconfiguring a DNS server cannot change this error, because no lookup was ever attempted.

Confirm the stage in three steps:

  1. Read where the exception was raised. If it is the library's own import dns.resolver line, the fault is a missing dependency, not a network fault.
  2. Note what is absent from the traceback: no IP address, no port, no socket call. The failure happened before the client could build a connection.
  3. Treat only the engine as the judge. The result that counts is an import executed by the script engine itself, shown in the next section.

Why did the same script work in the IDE interpreter? #

It ran in a different runtime: an IDE interpreter and a deployed script engine can use different Python executables, module search paths, library directories and file permissions, so an import that succeeds in the IDE proves nothing about the engine.

The working output you saw earlier was generated by the workstation interpreter, so it proved your code, not your deployment. An interpreter on the engineering PC resolves modules along its own search path, which includes its site-packages; the script engine resolves modules along a different path, and the engine's bundled library tree is the one your traceback pointed into. A package installed on the workstation is invisible to the engine.

Decision: every commissioning test for this fault runs through the script engine, on the controller. An interpreter success is evidence about your code, never evidence about the engine.

How do you install dnspython where the script engine can load it? #

Deploy dnspython through the mechanism the engine itself supports — its application library directory, a project libraries folder, or its package tool — because an install into your workstation's default Python environment never reaches the engine's module search path.

  1. Find where the engine loads libraries from. Your traceback already names the tree that holds its bundled libraries; use the platform's documented way to add dependencies there — a project libraries folder, an application data directory, or the IDE's dependency manager.
  2. Deploy dnspython through that mechanism, at a release that supports the engine's Python version.
  3. Check that the engine's runtime account can read the deployed files, and that nothing of your own shadows the module: a file named dns.py or a folder named dns earlier on the search path wins.
  4. Reload the engine or redeploy the configuration if the mechanism requires it, then run this import test as a script through the engine:
import dns.resolver
from paho.mqtt import client as mqtt_client

print("dns.resolver loaded:", dns.resolver.__name__)
print("mqtt client loaded:", mqtt_client.__name__)

Decision: when both print lines name their modules without an exception, the environment is fixed and you may test the connection. Do not blank the import dns.resolver line out of the bundled library to force a pass — deploy the dependency instead; editing managed library files hides an incomplete deployment.

Why does certificate verify failed appear only after the import passes? #

Because it is the next stage, not a leftover of the first: with imports working, the engine opens the TCP connection to the broker, starts the TLS handshake, and the handshake fails because the presented self-signed certificate chains to no anchor the engine trusts.

That error is progress, not regression: TCP reached the broker listener, so the address, the port and the route are all proven. If you instead get a refused connection or a timeout at this point, that is a listener or routing question — see the walk-through of paho MQTT connection refused Errno 111 at the broker. TLS verification performs two checks, and a certificate you created yourself with OpenSSL passes neither by default: the chain must end at an anchor the engine holds, and the name must match. What the broker presents is decided on the broker side, by its listener; if you run Mosquitto, the server side is covered in the guide to configuring Mosquitto 2.0 listeners for remote clients. A client certificate is a separate matter: configure one only when the listener requires mutual TLS.

How do you make the script engine trust a self-signed MQTT certificate? #

Copy the trust material into the engine's own storage and select that file in the MQTT client's TLS settings: for a directly self-signed server certificate the server certificate file itself is the anchor, and for a private CA it is the CA certificate.

  • Inspect the certificate the listener actually presents, not the file left in your certificate-generation folder; the two diverge after any reissue.
  • Match the anchor to the chain shape. A directly self-signed server certificate is its own anchor — deploy that exact file. A server certificate signed by a private CA needs the CA certificate as the anchor, plus any intermediate the engine must also hold.
  • Copy the anchor into storage the engine's runtime account can read, and pass that path to the client library's TLS configuration as its trust file.
  • Check the validity window against the engine's clock, as in the worked example below.
  • Deploy the anchor so it survives a power cycle; the same symptom returns when a client loses its CA file after a reboot, as in the article on MQTT connect failures after power loss when the CA certificate is missing.

Worked example — the clock check. A self-signed certificate issued for one year carries notBefore 2025-01-06 and notAfter 2026-01-06. The engine's clock reads 2026-03-04. Days from the expiry date to the clock date: 25 remaining days of January (31 - 6), plus 28 days of February (2026 is not a leap year), plus 4 days of March: 25 + 28 + 4 = 57 days. The certificate has been expired for 57 days, and an expired certificate produces the same certificate verify failed text as a missing anchor. Decision: reissue the certificate with a lifetime you will actually track, redeploy the new anchor to the engine, and correct the engine's clock, because a clock that is wrong breaks validity checks in both directions.

Does the broker hostname have to match the certificate? #

Yes — verification includes a name check, and it is strict: the name you connect to must appear in the certificate's subject alternative names, and an IP address you type into the broker address field does not match a certificate that lists only DNS names.

Example: the engine connects to 192.168.1.50 and the certificate's subject alternative names list only DNS:broker.plant.local. The anchor is correct, the clock is correct, and verification still fails, because an IP address is not a DNS name. Decision: connect to broker.plant.local and make that name resolve from the engine — a hosts entry or the plant DNS server both work — or reissue the certificate with a subject alternative name covering the address you actually use. When you issue with OpenSSL, put every name you will connect by into the SAN extension; that is the field TLS verifiers match against, so a certificate issued without it leaves the name check to legacy fallbacks you should not rely on.

How do you confirm the fix end to end? #

Run the publish test through the script engine, not the interpreter: connect by a name the certificate lists, on the listener's TLS port — for example 8883, the usual MQTT-over-TLS port — with your credentials and client ID, publish to a known topic, and confirm the message at a subscriber.

Each stage has its own signature, and the fix that belongs to each is different:

Stage What you see What it proves What fixes it
Import ModuleNotFoundError: No module named 'dns' Nothing ran; the environment is incomplete Deploy dnspython to the engine's library path
TCP Refused connection or timeout Imports work; the listener or route does not Address, port and routing — never certificates
TLS certificate verify failed TCP works; trust or the name is wrong Trust anchor, name match, clock — verification on
MQTT Connection accepted, publish rejected Transport is fine; broker auth is not Credentials and topic authorization on the broker

Decision, one line per case: an import-stage traceback is an environment fault, fixed only in the engine's dependency path. A TLS-stage failure after TCP connects is a trust fault, fixed by anchor, name and clock. And on any broker that carries process or control data, certificate verification stays enabled — disabling it deletes the server identity check instead of repairing trust.

Questions asked

Can I delete the failing import from the bundled MQTT library instead of installing dnspython?

No. That file is managed library code supplied with the platform. Removing the import hides an incomplete dependency deployment, and the library no longer behaves as its authors shipped it. Deploy dnspython through the engine's supported library mechanism and prove it with the import test executed by the engine itself.

Does the engine need dnspython even when I connect to the broker by IP address?

Yes. The import runs while the library loads, before your script picks an address or opens a socket, so the module must be present whether or not this script ever performs a name lookup. The dependency belongs to the library's loading step, not to your network configuration.

How long should a self-signed MQTT certificate be valid?

Whatever lifetime you set at creation; the issuing tool accepts the number of days you give it. The practical limit is attention: a one-year certificate expires quietly and reproduces the same verification failure as a missing anchor. Choose a lifetime you will track and schedule the reissue before the expiry date.

Where does the engine look for the trust file I pass to the TLS settings?

At the path exactly as the engine resolves it, on the controller's own filesystem. A file that exists on your engineering workstation but was never copied into the engine's readable storage gives a load or verification failure, even though the path looks correct in the script.

Do I also need a client certificate for the TLS connection?

Only when the broker's listener is configured to require one. A client certificate and its private key are provisioned to the engine separately from the CA trust anchor, and they authenticate your client to the broker. They neither fix nor replace the server verification that certificate verify failed is about.

Keep reading

  • Troubleshooting

    Why the Flexy AWS IoT script fails at MQTT CONNECT with error 28

    Error 28 at MQTT CONNECT does not mean the WAN is down: the script prints its WAN message for any LSTERR value 28. An SSL/TLS configuration event in the event log puts the fault at the TLS hop, so check the CA, certificate and key files, then port 8883 and the Flexy clock, before touching the AWS device policy.

    Edge computing and IIoT

  • Troubleshooting

    MQTT connect fails after power loss when the CA certificate is missing

    After a power failure, a gateway whose MQTT connect fails while VPN, PLC traffic and open ports all still work may be missing its runtime files: in one reported case the CA.crt certificate and the BASIC script were gone from the usr folder, and restoring them fixed publishing when a firmware upgrade did not. Check the files and the certificate's expiry date before touching the network or firmware.

    Edge computing and IIoT

  • Troubleshooting

    MQTT TLS connects but no CONNACK: find the failing hop

    The connect callback fires only when the broker returns a CONNACK, so with TCP ESTABLISHED and no callback the failure is in the TLS handshake or between TLS and MQTT. Run openssl s_client against the listener, then mosquitto_sub -d with the same CA file and credentials; wrong credentials are not the cause, because the broker answers those with a CONNACK that fires the callback with a return code.

    Edge computing and IIoT

Why an Ignition historian import does not display data