简体   繁体   中英

How can I import a Python script with several modules included into Sikuli

I'm trying to call a Python (2.7) script from Sikuli (r930) and use a variable from the script. Below is the Python code:

import sys
import re
import os
import time
from pywinauto import application
from SendKeys import SendKeys
from cStringIO import StringIO


app=application.Application()
app.connect_(path=r'C:\Program Files\myApp\myApp.exe')

backup = sys.stdout

sys.stdout = StringIO()     

app.dlg.print_control_identifiers()

out = sys.stdout.getvalue() 

sys.stdout.close()  # close the stream 
sys.stdout = backup # restore original stdout

regex = re.compile(r'(\d{8}\s*\-\s*\d{8})')
found = re.search(regex, out)

print found.group(0) #pass this variable to Sikuli

I'm capturing stdout because this is what Pywinauto's print_control_identifiers method returns (not a string). Also, I need a hashed serial number from the GUI app that I can't get with Sikuli, hence the need to use Pywinauto. However, when I attempt to call execfile() from Sikuli, I get the error:

ImportError: no module named Pywinauto. 

I read the docs, and I know that Sikuli (Jython) can include Python modules and scripts. Besides, the external .py file that I'm calling runs successfully when ran independently. Can someone tell me if I'm missing a step?

Code I'm using to call the .py file shown above from Sikuli:

aScript = 'c:\\getHash_serial.py'
execfile(aScript)

The immediate problem you're having is that Jython can't find your module. Probably, you installed it under Python, and they don't share a module path. You can fix this by setting the JYTHONPATH environment variable.

However, Pywinauto links with native code, and this is something that Jython does not support.

You may be able to get around this limitation by calling regular Python from Jython via the subprocess module .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM