Jython subProcess.Popen usage: how to propagate environment

Asked by Brad Hagen on 2018-01-24

Hello Sikuli community!

I have a need to capture audio using pyaudio during a test that runs via a sikuli script. I am running on windows 10.

The issue I am stuck on is when this line in my sikuli script runs:
subprocess.Popen(["start cmd C:\\python27\\python.exe", "audio_recorder_popen.py"])
I get this error:
    could not import the PyAudio C module '_portaudio'.
    [error] script [ Desktop ] stopped with error in line 8
    [error] ImportError ( No module named _portaudio )
    [error] --- Traceback --- error source first line: module ( function ) statement 116: pyaudio ( <module> ) import _portaudio as pa
    1: audio_recorder_popen ( <module> ) import pyaudio
    11: test_telephony_sikuliuser21 ( <module> ) import audio_recorder_popen
    [error] --- Traceback --- end --------------

I have tried variations of the Popen but seem to hit the same issue.
If I open a command prompt and run the same python script it works as expected:
    C:\Users\anony\Documents\DesktopAutomatedTesting\Desktop.sikuli>python.exe audio_recorder_popen.py

    recording...
    finished recording

I have copied the entire contents of C:\Python27\Lib\site-packages to C:\Users\anony\AppData\Roaming\Sikulix\Lib\site-packages which didn’t resolve the issue.
I can’t figure out where the PyAudio C module '_portaudio' resides on my system and how to work around this.

I’m hoping there is a way to run the script entirely independent of Sikuli but I’m not sure how to do that. I was thinking subprocess.Popen would be the solution.

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
Brad Hagen
Solved:
2018-01-24
Last query:
2018-01-24
Last reply:
2018-01-24
RaiMan (raimund-hocke) said : #1

--- I have copied the entire contents of C:\Python27\Lib\site-packages to C:\Users\anony\AppData\Roaming\Sikulix\Lib\site-packages

never do this with any Python stuff.
\Sikulix\Lib\site-packages is only for SikuliX related stuff, that is compatible/useable with Jython

I guess your problem is, that the subprocess.Popen as you use it, does not copy the outside environment. So it might be, that python.exe does not find its stuff.

Brad Hagen (bhagen) said : #2

Thanks RaiMan that sounds like a good direction to look! I'm guessing there is a way to print the environment while running from the command line then set it during the execution of the Popen in Sikuli. I'll do some research. Thanks for the quick response!

TestMechanic (ndinev) said : #3

some suggestions

-- Any chance to have 2 python versions? try to see if this works >C:\python27\python.exe audio_recorder_popen.py
-- Try to call form sikuli os.system('python.exe audio_recorder_popen.py')

Brad Hagen (bhagen) said : #4

Thanks TestMechanic I appreciate the suggestion. While trying that out I realized that even with the subprocess.Popen commented out I was getting the error. I turns out I had an import from earlier attempts to use the recorder as a class. I removed the import and modified my sikuli script to

    xyz = subprocess.Popen(["C:\\python27\\python.exe", "C:\\Users\\anony\\Documents\\DesktopAutomatedTesting\\Desktop.sikuli\\audio_recorder_popen.py"] ,stdout=subprocess.PIPE)
    print "xyz", xyz.communicate()

now I get the file as expected. Yes!

Thanks RaiMan and TestMechanic - Problem solved