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

;SETUP

;1. Download and install Python 32-bit Windows version from https://www.python.org/downloads/
;;;;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://github.com/mhammond/pywin32/releases
;;;;Choose the version that matches your Python version, for example pywin32-224.win32-py3.7.exe for Python 3.7.x.
;;;;Run the setup program as administrator.

;I tested Python version 3.7.2 on Windows 10, 7 and 8.1. Works without problems.
;In the past I also tested version 3.5.2 and encountered various problems, but finally it worked on Windows 10 and 7. Version 3.4.3 worked without problems on Windows 10, 7 and 8.1. I didn't test 64-bit version, it probably would not work with QM.

;TEST

;Run macros in "test" folder.

Use the above SETUP instructions. The SETUP instructions in the downloaded macro are now obsolete.


Attached Files
.qml   Python.qml (Size: 10 KB / Downloads: 980)
#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:
Copy      Help
PythonExec ""

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

Else:

Function PythonPrintToQM
Code:
Copy      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:
Copy      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:
Copy      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__
#4
Thanks a lot for your "PrintToQM" workarounds. They worked as expected. Big Grin
#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)
   
#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.
#7
I tested the new version of python3.7 to run successfully.  Smile

Step 1 Download pyton_3.7:
https://www.python.org/ftp/python/3.7.0/...-3.7.0.exe

Step 2 Download pywin32_3.7:
Select pywin32-223.win32-py3.7.exe
https://github.com/mhammond/pywin32/releases
You can directly click on the link below to download
pywin32-223.win32-py3.7.exe

Step 3 Download QM extension
http://www.quickmacros.com/forum/attachment.php?aid=592

I want to know, in QM3, can it be called like this? Huh

I generated the exe file, but I put the exe file together with the already installed python3.7 and it will not run on another computer.

How modify the code and I can implement the above requirements?  thank you very much  Heart


Attached Files Image(s)
   
#8
Quote:I want to know, in QM3, can it be called like this?
I googled how Python can be used in C#. The most popular are these 2 methods: 1. Save code in a file and run the Python command line program that executes the file. 2. IronPython. But I'm sure in C# also can be used the COM method like here in QM. And probably the 4-th method - like in C. Also found "Python for .NET", not tested.
Quote:I generated the exe file, but I put the exe file together with the already installed python3.7 and it will not run on another computer.
I tested now Python 3.7.2. Works without problems.
Maybe not installed pywin32. Or installed Python 64 bit. Need 32 bit. Or did not add to PATH when installing.
I installed Python in Q:\Python37. When installing, I choose to install for all users, add to PATH, and removed the PATH length limitation. Everything else is standard.
#9
Thank you for your reply, maybe I didn’t describe it clearly.

1. I installed python3.7 and pywin32 on my computer. I found that pywin32 is installed in the \Lib\site-packages\ folder of python. I tested the python script test file in QM, it can run successfully, then I Generated exe file with QM

2. I have another computer. I don't want to install python3.7 and pywin32. I want to copy the python installation folder installed on the first computer directly to the same folder as exe. Is there any way to run the exe file?

I think that Python sets the environment variable when it is installed. This is the key to success.

How to modify the Python script code in QM, set environment variables, and run exe file directly on any computer? This is very convenient, portable and easy to use.

My English is not good, I used Google Translate  Big Grin

The following code can't run successfully  Huh


Function py_test
Code:
Copy      Help
out
PythonExec ""


#ret

def Script():
,i = int(raw_input('Net profit:'))
,arr = [1000000,600000,400000,200000,100000,0]
,rat = [0.01,0.015,0.03,0.05,0.075,0.1]
,r = 0
,for idx in range(0,6):
,,if i>arr[idx]:
,,,r+=(i-arr[idx])*rat[idx]
,,,print (i-arr[idx])*rat[idx]
,,,i=arr[idx]
,print r


# ---------------------------------------------------------------------------------
# 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__
#10
I saw Python has a version that can be redistributed with applications, but did not test it. It guess it would work only when executing the command line Python, not through COM like we do here.
#11
Hello, in QM3, support python scripts? Smile

I found the tool to run python under .net , the link below.


https://ironpython.net
#12
Maybe. I did not test it.
#13
I found a python version that don't need to install. Double-click Python35.cmd directly to enter python shell mode
How does QM call it directly? is it possible?

Python35.cmd  Code:
 
Code:
Copy      Help
 
@echo off
CD /d "%~dp0"
set "PATH=%CD%\Main\;%CD%\Main\Scripts\;%PATH%"
if "%~f1"=="" (
if exist "%~dp0Main\used.li" (
echo Init pip ...
python -m pip install -U pip
del /f /q "%~dp0Main\used.li"
)
python
) else (
python "%~f1"
pause>NUL
)


Attached Files
.zip   Python35.zip (Size: 9.35 MB / Downloads: 317)
#14
Use RunConsole2.
#15
It's easier to use (powershell + ironpython) without installing pywin32 and without installing python Tongue
The grammar is simpler and easier to understand

https://ironpython.net
#16
Unzip the attachment IPY2.7.9 to the desktop and run the following code
But I have a problem, the result will show extra characters, how to remove it?
Huh

I think there may be a better way to use ironpython in QM2, without using powershell Idea


Macro IPY
Code:
Copy      Help
str s.expandpath("$desktop$\IPY\IronPython.dll")
int i=50
_s=
F
;[reflection.assembly]::LoadFrom("{s}")
;$py = [ironpython.hosting.python]::CreateEngine()
;$py.Execute("print {i}+4")

PsCmd3 _s "" str's1
mes s1



Attached Files Image(s)
   

.zip   IPY2.7.9.zip (Size: 5.06 MB / Downloads: 346)
#17
IronPython should be used directly from C#, not through PowerShell. In QM can be used C#, but without intellisense. Therefore at first test C# code in Visual Studio. Then either copy C# code to QM, or run your C# app from QM.
#18
Thank you for your reply, I prefer to use powershell, because the PS syntax is simpler, don't need to install Visual Studio, the system comes with powershell ISE
#19
Executing python function successfully Smile


Macro IPY fun
Code:
Copy      Help
str s.expandpath("$desktop$\IPY\IronPython.dll")
_s=
F
;[reflection.assembly]::LoadFrom("{s}")
;$py = [ironpython.hosting.python]::CreateEngine()
;$pyv = $py.CreateScope()
;$pyc = $py.CreateScriptSourceFromString(    
;"
;def fun():
;;;;;print 'hello from example function'
;");
;$pyc.Execute($pyv)
;$py.Operations.Invoke($pyv.GetVariable("fun"));
PsCmd3 _s "" str's1
mes s1

Remove extra GAC ​​characters  Smile

 [reflection.assembly]::LoadFrom("{s}") | Out-Null


Forum Jump:


Users browsing this thread: 1 Guest(s)