Skip to main content

Command Palette

Search for a command to run...

Working IBM MQ with PyMQI: A Comprehensive Guide for Python Developers

Published
7 min read
G

specializing in the development of scalable, efficient and hybrid platforms. My expertise centres around the advanced use of various Integration technologies, container orchestration, and automation to enhance operations and support continuous integration and deployment. Known for implementing key initiatives, modernize, migrate tech stack and implement state of the art frameworks in the organization. I am keen to apply my extensive skills and innovative methods to further the success of the organization.

Python library for IBM MQ PyMQI is a production-ready, open-source Python extension for IBM MQ (formerly known as WebSphere MQ and MQSeries).

For 20+ years, the library has been used by thousands of companies around the world with their queue managers running on Linux, Windows, UNIX and z/OS.

Requisites:

  • MQ Client or MQ Server

  • Ensure that there is a Primary installation of MQ

  • Python3

  • Python Development Package: python3-devel

Clarification on Requirements:

The MQ Redistributable Clients do not fulfill the requirements for pymqi. You need to use the MQ Client libraries that are formally installed by root/Administrator into an MQ Installation that can be is shown by setmqinst.

PyMQI is an open-source Python library for IBM MQ, used by numerous companies for over two decades. To set up PyMQI, ensure you have an MQ Client or Server installation marked as Primary, and install Python 3 with the development package, python3-devel, for compiling PyMQI. For MQ Client setups, symbolic links must exist in /usr/lib pointing to the MQ libraries. For MQ Server setups, set the environment variable MQ_CONNECT_TYPE=LOCAL to use local bindings. Install PyMQI using pip3 or build from source. Verify the installation with sample scripts that connect to an MQ queue manager, allowing message queuing operations.

Only for MQ Client :

The following Linux command confirms that the MQ Client package is installed.

rpm -q MQSeriesClient
MQSeriesClient-9.2.0-5.x86_64

If it is not installed, then you MUST install the MQ Client before proceeding.

  • Ensure to have a Primary installation of MQ Please keep in mind that even if you have only 1 installation of MQ, by default, it is NOT marked as Primary. Issue the MQ command:
/opt/mqm/bin/dspmqinst
InstName: Installation1
InstDesc: 
Identifier: 1
InstPath: /opt/mqm
Version: 9.3.0.0
Primary: No
State: Available

If the attribute "Primary" is "No" in the installation stanzas, then you will encounter runtime errors with PyMQI because PyMQI expects to access the MQ Client shared libraries directly from /usr/lib and by default, there are no symbolic links in /usr/lib that point to the actual installed files from MQ.

You need to designate one of the installations as Primary, and this will create symbolic links under /usr/lib for the MQ client shared library. To mark an installation as Primary:

Login as root and issue:

Setting Primary Installation:

  • This command designates a specific MQ installation as the primary one. It creates necessary symbolic links in /usr/lib for the MQ client shared library, which PyMQI requires to function correctly.
/opt/mqm/bin/setmqinst -i -n Installation1

'Installation1' (/opt/mqm) set as the primary installation. This will provide the necessary symbolic link that is needed by PyMQI.

ls -l /usr/lib/libmqic_r.so
lrwxrwxrwx 1 root root 25 Jan 20 05:26 /usr/lib/libmqic_r.so -> /opt/mqm/lib/libmqic_r.so

Notice that dspmqinst will show that the installation is Primary (value "Yes")

/opt/mqm/bin/dspmqinst

InstName: Installation1
InstDesc: 
Identifier: 1
InstPath: /opt/mqm
Version: 9.3.0.0
Primary: Yes

Python 3

  • Some systems have installed "Python 2" and the commands "python" and "pip" are version 2. In this host, Python 2 was not installed:

      +++ROOT+++ [suvereto1.fyre.ibm.com](http://suvereto1.fyre.ibm.com): /root
      # python
      -bash: python: command not found
    

PyMQI needs "Python 3" instead of "Python 2".

The commands for Python 3 are:

  • python3 --version and pip3 --version are used to verify the installed versions of Python 3 and pip, ensuring compatibility with PyMQI.
python3 --version
pip3 --version

You can issue the following command to find out the version.

 mqm@suvereto1.fyre.ibm.com: /home/mqm 
$ python3 -V 
Python 3.6.8

Python Development Package: python3-devel

  • Need to install the Python Development package "python3-devel", because the installation of PyMQI requires a recompilation.

    Note: If the development package is not installed, then the installation of PyMQI will fail with error: code

      /pymqi/pymqe.c:110:10: fatal error: Python.h: No such file or directory 
      #include "Python.h" ^~~~~~~~~~ 
      compilation terminated. 
      error: command 'gcc' failed with exit status 1
    
  • To install the Python Development package, login as root and issue:

    • The python3-devel package is necessary for compiling Python extensions. Without it, the installation of PyMQI will fail.
    +++ROOT+++ suvereto1.fyre.ibm.com: /root

    # yum install python3-devel

    Updating Subscription Management repositories. Red Hat Enterprise Linux 8 for x86_64 - AppStre 34 kB/s | 2.8 kB 00:00 Red Hat Enterprise Linux 8 for x86_64 - BaseOS 28 kB/s | 2.4 kB 00:00 Dependencies resolved.

    Package Arch Version Repository Size

    Installing: python36-devel x86_64 3.6.8-38.module+el8.5.0+12207+5c5719bc rhel-8-for-x86_64-appstream-rpms 17 k 
    Installing dependencies: platform-python-devel x86_64 3.6.8-45.el8 rhel-8-for-x86_64-appstream-rpms 250 k 
    python3-rpm-generators noarch 5-7.el8 rhel-8-for-x86_64-appstream-rpms 25 k … 
    Installed: platform-python-devel-3.6.8-45.el8.x86_64 
    python3-rpm-generators-5-7.el8.noarch 
    python36-devel-3.6.8-38.module+el8.5.0+12207+5c5719bc.x86_64 
    Complete

Only for MQ Server :

Set the environment variable for using MQ server shared libraries:

  • export MQ_CONNECT_TYPE=LOCAL sets an environment variable that allows PyMQI to use MQ Server shared libraries for local bindings, enabling direct communication with the MQ server.
export MQ_CONNECT_TYPE=LOCAL

Creating and Activating a Virtual Environment:

  • This sequence of commands creates an isolated Python environment, ensuring that dependencies for PyMQI do not interfere with other projects.
python3.11 -m venv virtualcop
cd virtualcop
source activate

Creating Directory and Navigating:

  • These commands create a new directory and navigate into it, preparing a workspace for downloading and installing PyMQI.
mkdir conhub
cd conhub

Downloading and Unzipping PyMQI:

  • The wget command downloads the PyMQI source code from GitHub, and unzip extracts the files, preparing them for installation.
wget https://github.com/dsuch/pymqi/archive/refs/heads/main.zip
unzip pymqi-main.zip

Building and Installing PyMQI:

  • These commands compile and install PyMQI from the source. The build server option is used to include server-specific components.
cd conhub/pymqi-main
python3 setup.py build server
python3 setup.py install

Create a sample Python file bind.py with the following content:

  • This command executes a Python script (bind.py) that likely contains code to test the PyMQI setup by connecting to an MQ queue manager.
import pymqi 
qmgr = pymqi.connect('QM1') 
print(qmgr.is_connected)

Running Sample Python File:

python3 bind.py

Using ONLY the MQ Client shared libraries

Login as root and issue:

+++ROOT+++ suvereto1.fyre.ibm.com: /root
pip3 install pymqi
WARNING: Running pip install with root privileges is generally not a good idea. Try pip3 install --user instead. 
Collecting pymqi 
Downloading https://files.pythonhosted.org/packages/20/dc/a1975fc640d1dabdabe7877f28eebec2bd4d5 13227322eb35c3753ee2d0b/pymqi-1.12.10.tar.gz (91kB) 
100% |ââââââââââââââââââââââââââââââââ| 92kB 2.5MB/s 
Installing collected packages: pymqi 
   Running setup.py install for pymqi ... done 
Successfully installed pymqi-1.12.10

How to confirm that PyMQI was successfully installed (MQ Client shared libraries)

We can use an extremely simple sample to do a very basic test. Ensure to modify the "pymqi.connect" line in the sample to reflect your queue manager.

Sample code #To put a message on a queue: putmsg.py

  • The putmsg.py script uses PyMQI to connect to an MQ queue manager and put a message on a specified queue. This demonstrates basic usage of the library for message queuing operations.
import pymqi
queue_manager = pymqi.connect('QM.1', 'SVRCONN.CHANNEL.1', 'localhost(1434)')
q = pymqi.Queue(queue_manager, 'TESTQ.1') q.put('Hello from Python!')

#To read the message back from the queue:

import pymqi
queue_manager = pymqi.connect('QM.1', 'SVRCONN.CHANNEL.1', 'localhost(1434)')
q = pymqi.Queue(queue_manager, 'TESTQ.1') msg = q.get() print('Here is the message:', msg)

Yeah!!! Iam able to connect now

Some Troubleshooting steps:

How to confirm that PyMQI was successfully installed (MQ Server shared libraries)

If you have installed PyMQI to also allow the use of the MQ Server shared libraries, then you can use the small sample "putmsg.py" shown above.

The only difference is that you need to set an MQ environment variable (MQ_CONNECT_TYPE) that will allow for the use of the MQ Server connection shared libraries (local bindings):

 $ export MQ_CONNECT_TYPE=LOCAL

 $ python3 putmsg.py 
   Starting putmsg 
   Putting 1 message into queue 
   Ending putmsg

Additional notes about using the MQ Server libraries

  • In case that you doubt that the MQ Server libraries are being used, then you could stop the MQ listener, confirm that the runmqlsr process is not running (which will prevent remote network connections) and run again the sample program.

    Use runmqsc to stop the MQ listener:

stop LISTENER(SYSTEM.LISTENER.TCP.1) 
   2 : stop LISTENER(SYSTEM.LISTENER.TCP.1) 
AMQ8706I: Request to stop IBM MQ Listener accepted.

Confirm that runmqlsr is no longer running:

  •     $ ps -ef | grep runmqlsr (none) 
        $ export MQ_CONNECT_TYPE=LOCAL 
        $ python3 putmsg.py 
          Starting putmsg 
          Putting 1 message into queue 
          Ending putmsg
    

The trace of the MQ client application will indicate that server library will be used:


...
10:45:32.968342 8208.1 : xcsGetEnvironmentString[MQ_CONNECT_TYPE] = 'LOCAL'
10:45:32.968346 8208.1 : ---} xcsGetEnvironmentString rc=OK FunctionTime=7
10:45:32.968349 8208.1 : MQ_CONNECT_TYPE says only attempt server connection
10:45:32.968354 8208.1 : ConnectType: 3, ServerOnly: TRUE, ClientOnly: FALSE, Fastpath:
FALSE, Shared: FALSE
10:45:32.968357 8208.1 : --} zswChooseLibraries rc=OK FunctionTime=23
10:45:32.968361 8208.1 : Attempting to connect using the server library
10:45:32.968365 8208.1 : Trying optimized connect using internal interface 0x7f52c9c928a0