Python Client

Python teams adopt IBM MQ when integration scripts must put audit events on enterprise queues, ML pipelines must consume mainframe-sourced work items, or DevOps automation must drain test queues after deployments. pymqi exposes the Message Queue Interface to Python 3 with objects like QueueManager and Queue wrapping MQCONN and MQOPEN. Underneath it is still the C client and SVRCONN—your pip install fails on a laptop without IBM MQ client libraries, and your script gets 2035 if operations never granted MCAUSER on the target queue. This tutorial explains pymqi installation constraints, connect patterns with host and channel, put and get with bytes and strings, using CCDT via environment variables, transactional puts with pymqi CMIT and BACK, pairing with Pandas and Celery architectures, container deployment, and troubleshooting import errors versus MQ reason codes.

Installation Reality

pip install pymqi triggers build against installed cmqc.h and libmqm. On Windows install IBM MQ client first and set MQ_FILE_PATH. On Linux use supported distributions or IBM container base. CI pipelines install client tarball before pip step. Virtualenv does not remove native dependency—document in README for data scientists cloning the repo. Pin pymqi version in requirements.txt with tested MQ client version comment.

pymqi objects versus MQI
pymqiMQI equivalentRole
QueueManagerMQCONNConnection
QueueMQOPENObject handle
putMQPUTSend message
getMQGETReceive message
CMITMQCMITCommit UOW

Connect and Put Example

python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import pymqi conn_info = "QM_PROD" channel = "ORDERS.API" host = "mq-prod.corp" port = "1414" qmgr = pymqi.QueueManager(None) qmgr.connect(conn_info, channel, f"{host}({port})") queue = pymqi.Queue(qmgr) queue.open("ORDERS.IN", pymqi.CMQC.MQOO_OUTPUT) queue.put(b"payload bytes") queue.close() qmgr.disconnect()

connect first argument is queue manager name. Channel and conn_info string follow pymqi signature for your version—verify against current pymqi docs because APIs evolved. Use bytes for binary payloads; decode explicitly when mainframe sends EBCDIC. Catch pymqi.MQMIError and read comp, reason, and mqrc attributes for logging.

Context Manager Pattern

python
1
2
3
4
5
with pymqi.QueueManager(None) as qmgr: qmgr.connect("QM_PROD", "ORDERS.API", "mq-prod.corp(1414)") with pymqi.Queue(qmgr, "ORDERS.IN") as q: q.open(pymqi.CMQC.MQOO_OUTPUT) q.put(b"event data")

Context managers help scripts that crash on exceptions still disconnect—reduces orphaned SVRCONN instances on servers when analysts run ad hoc notebooks repeatedly.

Get Loop Consumer

Open with MQOO_INPUT_AS_Q_DEF and MQGMO_WAIT with suitable wait interval. Loop get until empty or shutdown flag. Process message; commit or backout syncpoint if in transactional mode. Poison messages hitting backout need alignment with BOQNAME on server. Use signal handlers in long-running workers for graceful exit calling disconnect.

CCDT and Environment

os.environ["MQCHLLIB"] = "/var/mqm/ccdt" and MQCHLTAB before connect allows channel-only connect strings per pymqi CCDT support patterns. Alternatively pass full connection tuple in connect. Ansible templates push env vars to batch hosts running Python ETL.

Data Science and Automation Use Cases

  • ETL trigger—put file ready message to mainframe batch queue.
  • Monitoring—get depth probe messages from admin queues.
  • Test data—seed queues in CI with Python fixtures.
  • Avoid long blocking get in Jupyter without timeout—kernels hang.

Troubleshooting

  1. ImportError pymqi—client not installed or wrong arch.
  2. MQMIError reason 2035—DISPLAY AUTHREC server side.
  3. Reason 2085—queue name wrong case.
  4. Encoding errors—check CCSID and bytes versus str.
  5. Stale connection—disconnect in finally blocks.

Explainer: Python Gloves for C Hands

pymqi lets Python wear gloves to handle MQ—the hands doing the work are still the C client underneath.

Explain Like I'm Five: Python Client

Python asks pymqi to mail a message to MQ, and pymqi knows how to speak the post office language.

Practice Exercises

Exercise 1

Write a script that puts one persistent message and logs MQMIError reason on failure.

Exercise 2

Add MQCHLLIB to a script and explain when ops prefers that over hardcoded host.

Exercise 3

Design idempotent consumer pseudocode for at-least-once delivery.

Frequently Asked Questions

Frequently Asked Questions

Test Your Knowledge

Test Your Knowledge

1. Python MQ library is commonly:

  • pymqi
  • requests
  • flask
  • pandas only

2. pymqi needs:

  • IBM MQ client installed
  • Only pip
  • No TCP
  • JES

3. with QueueManager helps:

  • Clean disconnect
  • Skip authority
  • Disable TLS
  • Format DASD

4. 2035 in Python means:

  • Check server AUTHREC
  • pip upgrade
  • Delete queue
  • Wrong Python version
Published
Read time20 min
AuthorMainframeMaster
Verified: IBM MQ 9.3 documentation