Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Execute Python code in QM
Macro Python scripting help
Select All      Help
;These functions can be used to execute Python code in QM.


;Download and install Python 32-bit.
;There are multiple Python implementations. I tested the "traditional" implementation (CPython) from .

;To install CPython:
;;;1. Download and install Python 32-bit Windows version from
;;;;;;I tested version 3.5.2 on Windows 10 and 7, and version 3.4.3 on Windows 8.1. I recommend version 3.4.3, read below why. I didn't test 64-bit version, it probably would not work with QM.
;;;;;;IMPORTANT: When installing, check "Add to PATH". After installing, restart QM to update its copy of PATH variable.
;;;2. Download and install PyWin32 from
;;;;;;Choose the version that matches your Python version, for example pywin32-220.win32-py3.5.exe for Python 3.5.x or pywin32-220.win32-py3.4.exe for Python 3.4.x.
;;;;;;IMPORTANT: Run the setup program as administrator.
;;;Possible problems with Python 3.5.x:
;;;;;;On Windows 7: error "api-ms-win-crt-runtime-l1-1-0 dll missing".
;;;;;;;;;I solved this not easily. Tried several ways, and now don't know what was actually needed, but the problem finally disappeared after installing KB2999226:
;;;;;;;;;I tried to install all Windows updates, Visual C++ 2015 Redistributable package, KB2999226. Then uninstall/install the Python programs and restart QM.
;;;;;;;;;Or you can try an older Python version, eg 3.4.3.
;;;;;;On Windows 8.1: Python 3.5.2 and 3.6.0a2 setup failed. Version 3.4.3 + pywin32-220.win32-py3.4.exe worked without problems.


;Run macros in "test" folder.

Also I tested ActiveState Python 2.7.10, it was easier to install. But its newer version 2.7.13 does not work.

Attached Files
.qml   Python.qml (Size: 10 KB / Downloads: 303)
Is there anyway to capture output of a "print" statement in python to a QM string or to QM output window?
Macro test PythonExec1
Select All      Help
PythonExec ""

print("Hello World!")
If the QM macro is a console exe, Python print() will write to its console.


Function PythonPrintToQM
Select All      Help
;Redirects Python print() function output to QM output.

;print() will be redirected in all Python code added by PythonExec or PythonAddCode that are called after calling this function in current thread.

;PythonExec ""
;print("") # empty line
;print("one\ntwo") # multiline
;print(2) # number
;print("list", 1, 2, 3)
;print("list2", 1, 2, 3, sep=", ")

PythonAddCode ""

import sys
import ctypes
import win32gui

def printQM(*a, sep = ' '):
,s = ""
,for v in a:
,,if(s != ""): s += sep
,,s += str(v)
,if(printQM.hwndQM == 0): printQM.hwndQM=win32gui.FindWindow("QM_Editor", None)
,ctypes.windll.user32.SendMessageW(printQM.hwndQM, 12, -1, s)
printQM.hwndQM = 0

print = printQM

Macro test PythonPrintToQM
Select All      Help
PythonExec ""

print("") # empty line
print("one\ntwo") # multiline
print(2) # number
print("list", 1, 2, 3)
print("list2", 1, 2, 3, sep=", ")

Or alternative:

Macro Python print output to QM - alternative
Select All      Help
PythonExec ""


def Script():
,print("") # empty line
,print("one\ntwo") # multiline
,print(2) # number

# ---------------------------------------------------------------------------------
# Put your Python script in the Script() function. Don't need to change other code.

import sys
import ctypes
import os
import win32gui

class PrintRedirector(object):
,def __init__(self):
,,self.hwndQM=win32gui.FindWindow("QM_Editor", None)
,def write(self, message):
,,if(message=='\n'): return
,,ctypes.windll.user32.SendMessageW(self.hwndQM, 12, -1, message)

sys.stdout = PrintRedirector()
try: Script()
finally: sys.stdout = sys.__stdout__
Thanks a lot for your "PrintToQM" workarounds. They worked as expected. Big Grin
I downloaded from the website the latest 2.7.13 operating tests, there has been an error

My system version is: win7 64-bit

Attached Files Image(s)
I tested ActiveState Python 2.7.13 now, and it does not work.

Try the second option - . I did not test its newest version 3.6.2. The older version 3.5.2 works on my Windows 7 64-bit.

Forum Jump:

Users browsing this thread: 1 Guest(s)