<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[using python pymqi to connect MQ]]></title><description><![CDATA[using python pymqi to connect MQ]]></description><link>https://using-pythob-pymqi-to-connect-mq.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Tue, 23 Jun 2026 22:24:18 GMT</lastBuildDate><atom:link href="https://using-pythob-pymqi-to-connect-mq.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Working IBM MQ with PyMQI: A Comprehensive Guide for Python Developers]]></title><description><![CDATA[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 manager...]]></description><link>https://using-pythob-pymqi-to-connect-mq.hashnode.dev/working-ibm-mq-with-pymqi-a-comprehensive-guide-for-python-developers</link><guid isPermaLink="true">https://using-pythob-pymqi-to-connect-mq.hashnode.dev/working-ibm-mq-with-pymqi-a-comprehensive-guide-for-python-developers</guid><category><![CDATA[pymqi]]></category><category><![CDATA[mqclient]]></category><category><![CDATA[mqserver]]></category><category><![CDATA[MQ]]></category><category><![CDATA[ibmmq-installation-guide]]></category><category><![CDATA[IBM MQ]]></category><category><![CDATA[ibmmq ]]></category><dc:creator><![CDATA[Giri Naresh Allu]]></dc:creator><pubDate>Mon, 22 Dec 2025 02:50:52 GMT</pubDate><content:encoded><![CDATA[<p>Python library for IBM MQ PyMQI is a production-ready, open-source Python extension for IBM MQ (formerly known as WebSphere MQ and MQSeries).</p>
<p>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.</p>
<p><strong>Requisites:</strong></p>
<ul>
<li><p>MQ Client or MQ Server</p>
</li>
<li><p>Ensure that there is a Primary installation of MQ</p>
</li>
<li><p>Python3</p>
</li>
<li><p>Python Development Package: python3-devel</p>
</li>
</ul>
<p><strong>Clarification on Requirements:</strong></p>
<p>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.</p>
<blockquote>
<p>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.</p>
</blockquote>
<h2 id="heading-only-for-mq-client">Only for MQ Client :</h2>
<p>The following Linux command confirms that the MQ Client package is installed.</p>
<pre><code class="lang-bash">rpm -q MQSeriesClient
MQSeriesClient-9.2.0-5.x86_64
</code></pre>
<p>If it is not installed, then you MUST install the MQ Client before proceeding.</p>
<ul>
<li>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:</li>
</ul>
<pre><code class="lang-shell">/opt/mqm/bin/dspmqinst
</code></pre>
<pre><code class="lang-plaintext">InstName: Installation1
InstDesc: 
Identifier: 1
InstPath: /opt/mqm
Version: 9.3.0.0
Primary: No
State: Available
</code></pre>
<p>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.</p>
<p>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:</p>
<p>Login as root and issue:</p>
<p><strong>Setting Primary Installation:</strong></p>
<ul>
<li>This command designates a specific MQ installation as the primary one. It creates necessary symbolic links in <code>/usr/lib</code> for the MQ client shared library, which PyMQI requires to function correctly.</li>
</ul>
<pre><code class="lang-bash">/opt/mqm/bin/setmqinst -i -n Installation1
</code></pre>
<p>'Installation1' (/opt/mqm) set as the primary installation. This will provide the necessary symbolic link that is needed by PyMQI.</p>
<pre><code class="lang-bash">ls -l /usr/lib/libmqic_r.so
lrwxrwxrwx 1 root root 25 Jan 20 05:26 /usr/lib/libmqic_r.so -&gt; /opt/mqm/lib/libmqic_r.so
</code></pre>
<p>Notice that dspmqinst will show that the installation is Primary (value "Yes")</p>
<pre><code class="lang-bash">/opt/mqm/bin/dspmqinst

InstName: Installation1
InstDesc: 
Identifier: 1
InstPath: /opt/mqm
Version: 9.3.0.0
Primary: Yes
</code></pre>
<h2 id="heading-python-3">Python 3</h2>
<ul>
<li><p>Some systems have installed "Python 2" and the commands "python" and "pip" are version 2. In this host, Python 2 was not installed:</p>
<pre><code class="lang-bash">  +++ROOT+++ [suvereto1.fyre.ibm.com](http://suvereto1.fyre.ibm.com): /root
  <span class="hljs-comment"># python</span>
  -bash: python: <span class="hljs-built_in">command</span> not found
</code></pre>
</li>
</ul>
<p>PyMQI needs "Python 3" instead of "Python 2".</p>
<p>The commands for Python 3 are:</p>
<ul>
<li><code>python3 --version</code> and <code>pip3 --version</code> are used to verify the installed versions of Python 3 and pip, ensuring compatibility with PyMQI.</li>
</ul>
<pre><code class="lang-bash">python3 --version
pip3 --version
</code></pre>
<p>You can issue the following command to find out the version.</p>
<pre><code class="lang-bash"> mqm@suvereto1.fyre.ibm.com: /home/mqm 
$ python3 -V 
Python 3.6.8
</code></pre>
<h2 id="heading-python-development-package-python3-devel">Python Development Package: python3-devel</h2>
<ul>
<li><p>Need to install the Python Development package "python3-devel", because the installation of PyMQI requires a recompilation.</p>
<p>  Note: If the development package is not installed, then the installation of PyMQI will fail with error: code</p>
<pre><code class="lang-bash">  /pymqi/pymqe.c:110:10: fatal error: Python.h: No such file or directory 
  <span class="hljs-comment">#include "Python.h" ^~~~~~~~~~ </span>
  compilation terminated. 
  error: <span class="hljs-built_in">command</span> <span class="hljs-string">'gcc'</span> failed with <span class="hljs-built_in">exit</span> status 1
</code></pre>
</li>
<li><p>To install the Python Development package, login as root and issue:</p>
<ul>
<li>The <code>python3-devel</code> package is necessary for compiling Python extensions. Without it, the installation of PyMQI will fail.</li>
</ul>
</li>
</ul>
<pre><code class="lang-bash">    +++ROOT+++ suvereto1.fyre.ibm.com: /root

    <span class="hljs-comment"># yum install python3-devel</span>

    Updating Subscription Management repositories. Red Hat Enterprise Linux 8 <span class="hljs-keyword">for</span> x86_64 - AppStre 34 kB/s | 2.8 kB 00:00 Red Hat Enterprise Linux 8 <span class="hljs-keyword">for</span> 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
</code></pre>
<h2 id="heading-only-for-mq-server">Only for MQ Server :</h2>
<p>Set the environment variable for using MQ server shared libraries:</p>
<ul>
<li><code>export MQ_CONNECT_TYPE=LOCAL</code> sets an environment variable that allows PyMQI to use MQ Server shared libraries for local bindings, enabling direct communication with the MQ server.</li>
</ul>
<pre><code class="lang-bash"><span class="hljs-built_in">export</span> MQ_CONNECT_TYPE=LOCAL
</code></pre>
<p><strong>Creating and Activating a Virtual Environment:</strong></p>
<ul>
<li>This sequence of commands creates an isolated Python environment, ensuring that dependencies for PyMQI do not interfere with other projects.</li>
</ul>
<pre><code class="lang-bash">python3.11 -m venv virtualcop
<span class="hljs-built_in">cd</span> virtualcop
<span class="hljs-built_in">source</span> activate
</code></pre>
<p><strong>Creating Directory and Navigating:</strong></p>
<ul>
<li>These commands create a new directory and navigate into it, preparing a workspace for downloading and installing PyMQI.</li>
</ul>
<pre><code class="lang-bash">mkdir conhub
<span class="hljs-built_in">cd</span> conhub
</code></pre>
<p><strong>Downloading and Unzipping PyMQI:</strong></p>
<ul>
<li>The <code>wget</code> command downloads the PyMQI source code from GitHub, and <code>unzip</code> extracts the files, preparing them for installation.</li>
</ul>
<pre><code class="lang-bash">wget https://github.com/dsuch/pymqi/archive/refs/heads/main.zip
unzip pymqi-main.zip
</code></pre>
<p><strong>Building and Installing PyMQI:</strong></p>
<ul>
<li>These commands compile and install PyMQI from the source. The <code>build server</code> option is used to include server-specific components.</li>
</ul>
<pre><code class="lang-bash"><span class="hljs-built_in">cd</span> conhub/pymqi-main
python3 setup.py build server
python3 setup.py install
</code></pre>
<p>Create a sample Python file <a target="_blank" href="http://bind.py"><code>bind.py</code></a> with the following content:</p>
<ul>
<li>This command executes a Python script (<a target="_blank" href="http://bind.py"><code>bind.py</code></a>) that likely contains code to test the PyMQI setup by connecting to an MQ queue manager.</li>
</ul>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> pymqi 
qmgr = pymqi.connect(<span class="hljs-string">'QM1'</span>) 
print(qmgr.is_connected)
</code></pre>
<p><strong>Running Sample Python File:</strong></p>
<pre><code class="lang-python">python3 bind.py
</code></pre>
<h2 id="heading-using-only-the-mq-client-shared-libraries">Using ONLY the MQ Client shared libraries</h2>
<p>Login as root and issue:</p>
<pre><code class="lang-bash">+++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 <span class="hljs-keyword">for</span> pymqi ... <span class="hljs-keyword">done</span> 
Successfully installed pymqi-1.12.10
</code></pre>
<h3 id="heading-how-to-confirm-that-pymqi-was-successfully-installed-mq-client-shared-libraries">How to confirm that PyMQI was successfully installed (MQ Client shared libraries)</h3>
<p>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.</p>
<p>Sample code #To put a message on a queue: <a target="_blank" href="http://putmsg.py">putmsg.py</a></p>
<ul>
<li>The <a target="_blank" href="http://putmsg.py"><code>putmsg.py</code></a> 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.</li>
</ul>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> pymqi
queue_manager = pymqi.connect(<span class="hljs-string">'QM.1'</span>, <span class="hljs-string">'SVRCONN.CHANNEL.1'</span>, <span class="hljs-string">'localhost(1434)'</span>)
q = pymqi.Queue(queue_manager, <span class="hljs-string">'TESTQ.1'</span>) q.put(<span class="hljs-string">'Hello from Python!'</span>)
</code></pre>
<p>#To read the message back from the queue:</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> pymqi
queue_manager = pymqi.connect(<span class="hljs-string">'QM.1'</span>, <span class="hljs-string">'SVRCONN.CHANNEL.1'</span>, <span class="hljs-string">'localhost(1434)'</span>)
q = pymqi.Queue(queue_manager, <span class="hljs-string">'TESTQ.1'</span>) msg = q.get() print(<span class="hljs-string">'Here is the message:'</span>, msg)
</code></pre>
<h3 id="heading-yeah-iam-able-to-connect-now">Yeah!!! Iam able to connect now</h3>
<h2 id="heading-some-troubleshooting-steps">Some Troubleshooting steps:</h2>
<h3 id="heading-how-to-confirm-that-pymqi-was-successfully-installed-mq-server-shared-libraries">How to confirm that PyMQI was successfully installed (MQ Server shared libraries)</h3>
<p>If you have installed PyMQI to also allow the use of the MQ Server shared libraries, then you can use the small sample "<a target="_blank" href="http://putmsg.py">putmsg.py</a>" shown above.</p>
<p>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):</p>
<pre><code class="lang-bash"> $ <span class="hljs-built_in">export</span> MQ_CONNECT_TYPE=LOCAL

 $ python3 putmsg.py 
   Starting putmsg 
   Putting 1 message into queue 
   Ending putmsg
</code></pre>
<h3 id="heading-additional-notes-about-using-the-mq-server-libraries">Additional notes about using the MQ Server libraries</h3>
<ul>
<li><p>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.</p>
<p>  Use runmqsc to stop the MQ listener:</p>
</li>
</ul>
<pre><code class="lang-bash">stop LISTENER(SYSTEM.LISTENER.TCP.1) 
   2 : stop LISTENER(SYSTEM.LISTENER.TCP.1) 
AMQ8706I: Request to stop IBM MQ Listener accepted.
</code></pre>
<p>Confirm that runmqlsr is no longer running:</p>
<ul>
<li><pre><code class="lang-bash">    $ ps -ef | grep runmqlsr (none) 
    $ <span class="hljs-built_in">export</span> MQ_CONNECT_TYPE=LOCAL 
    $ python3 putmsg.py 
      Starting putmsg 
      Putting 1 message into queue 
      Ending putmsg
</code></pre>
</li>
</ul>
<p>The trace of the MQ client application will indicate that server library will be used:</p>
<pre><code class="lang-bash">
...
10:45:32.968342 8208.1 : xcsGetEnvironmentString[MQ_CONNECT_TYPE] = <span class="hljs-string">'LOCAL'</span>
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
</code></pre>
]]></content:encoded></item></channel></rss>