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

;SETUP

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

;To install CPython:
;;;1. Download and install Python 32-bit Windows version from
;;;;;;;;;https://www.python.org/downloads/
;;;;;;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
;;;;;;;;;https://sourceforge.net/projects/pywin32/files/pywin32/
;;;;;;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: https://support.microsoft.com/en-us/kb/2999226.
;;;;;;;;;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.

;TEST

;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: 309)
Reply
#2
Is there anyway to capture output of a "print" statement in python to a QM string or to QM output window?
Macro test PythonExec1
Code:
Select All      Help
PythonExec ""

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

Else:

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

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

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


PythonAddCode ""

#ret
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
Code:
Select All      Help
out
PythonPrintToQM
PythonExec ""


#ret
print("test")
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
Code:
Select All      Help
out
PythonExec ""


#ret

def Script():
,print("test")
,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__
Reply
#4
Thanks a lot for your "PrintToQM" workarounds. They worked as expected. Big Grin
Reply
#5
I downloaded from the website http://www.activestate.com/activepython/downloads the latest 2.7.13 operating tests, there has been an error

My system version is: win7 64-bit


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

Try the second option - https://www.python.org/ . I did not test its newest version 3.6.2. The older version 3.5.2 works on my Windows 7 64-bit.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)