Launching 'external' software from Psychopy

782 views
Skip to first unread message

Adrie

unread,
Nov 10, 2013, 9:47:41 AM11/10/13
to psychop...@googlegroups.com
Hi everybody!

I'm working on an experiment which combines several different tasks and questionnaires into one long string. So far I have been putting this all together in the builder view, but now ran into a question which I haven't been able to google my way out of. So hopefully you can help me out!

What I am looking for is a block to put into the builder which tells psychopy to open a small program which is in the same folder as the test itself. Now to elaborate a bit more: one of the tasks I would like users to do is a relatively simple 'typing task' in which users have to duplicate a text. Now for this I have a very nifty little program which does this great, logs all errors, key strokes etc etc, but the big question is, can I have psychopy open this software and have psychopy keep running in the background (as at some point the typing test will close, and the user should continue with the psychopy tasks). 

The software just starts using a .exe file and when the set time is over will close automatically so the only thing really needed is some sort of a start or launch command I guess.

Now as said I've already been googling for a bit and have come across things like 
import os;
os.system('path')

Or
import subprocess 
subprocess.call(['path']) 

However, I have not been able to get this to work from the builder interface. It might well be that I'm just not putting them in the right boxes or something...Btw, I have been testing this by using a path to notepad.exe

So my questions:
1: Can Psychopy be used to launch another program 'over' it
2: If so, how should one go about this, and would it be possible from the builder view?

Hopefully someone has any experience with this!

Thanks in advance for reading/replying and thinking with me!

Regards,
Adrie

Jeremy Gray

unread,
Nov 10, 2013, 10:26:06 AM11/10/13
to psychop...@googlegroups.com
Hi Adrie,

It sounds like you are on the right track, with subprocess etc. However, I am guessing that you will need to close the PsychoPy window, call the typing program, and re-open the PsychoPy window again. Something like this, from a code component:

win.close()
core.shellCall(['path'])  # like subprocess.call(['path']), no need to import subprocess
win = visual.Window(fullscr=True, …)  # here duplicate all initial settings

This might be a bit rough visually, bit might approximate what you want. A possible problem: the subject may not end up back in PsychoPy when the typing program finishes, for example.

--Jeremy

--Jeremy


--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/psychopy-users/475aa9e7-4efe-47a4-9008-f73cf353a692%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Jeremy Gray

unread,
Nov 10, 2013, 10:32:53 AM11/10/13
to psychop...@googlegroups.com
hmm, I think my suggestion would get you into the typing program, but then once back in PsychoPy all previously defined visual stim would not know that the new "win" is supposed to be the same as the one that was closed, and so they would not draw. 

So the best you might be able to do is use a non-full-screen PsychoPy window initially, and switch back and forth (which might require manual intervention to get window focus, etc).

--Jeremy

Jared Roberts

unread,
Nov 10, 2013, 11:01:56 AM11/10/13
to psychop...@googlegroups.com
Hi Adrie,

I think that Jeremy is on track.  There definitely are ways to automate window focus using python, but it depends on the OS that you're using.  If you're using windows, check this out :http://kvance.livejournal.com/985732.html,  If you're using a mac, you may want to check into apple scripting.  You can run apple scripts from python using the system command.  Such programs would no longer be platform independent, but that's a small price to pay.

-Jared


--
Jared Roberts
Graduate Student
The Yassa Learning and Memory Laboratory
Johns Hopkins University
"A lost cause can be as spiritually satisfying as a victory." - Robert Heinlein

Adrie

unread,
Nov 10, 2013, 11:28:31 AM11/10/13
to psychop...@googlegroups.com
Thanks for the help guys! Much appreciated!

So, let me see if I can make something out of this (my coding knowledge and skills are very limited, appologies for that)

1: Run the program in windowed mode (psychopy settings) 
2: Call the program, using the core.shellCall(['path'])  part that Jeremy mentions (i'm assuming the first win.close part is taken out again, relating to Jeremy's second post).
3: Make sure the focus is on the right window. According to the link Jared gave that should look something like this:

import win32com.client 
shell = win32com.client.Dispatch("WScript.Shell") 
shell.AppActivate('Some Application Title')

where the 'some application title' I assume has to link to the window name of the typing tool?

4: After the typing task is done (fixed time) I need to put the focus back on the Psychopy screen using the same code?

Now the next question would be, how to put this into the builder view. I assume I have to put some of this code into a custom code block. But what part should go where? Should the entire part go into Begin routine and maybe with the final focus-switch in the end-routine part?

Thanks again for the help guys!

global...@gmail.com

unread,
Nov 23, 2013, 2:52:41 PM11/23/13
to psychop...@googlegroups.com
I've been using the following commands in the Builder to launch an external Java application in which the participants are supposed to write a longer text about themselves. Some commands may seem redundant (such as minimizing the window and setting fullscreen to False afterwards), but in my case it doesn't work if I delete any of those lines:

win.winHandle.minimize() #minimize the PsychoPy window
win
.fullscr = False #disable fullscreen
win
.flip() #redraw the (minimized) window
try:
    subprocess
.call(['java','-jar','TextEntry.jar',str(entryLength),vpnr]) #launch external program
except WindowsError: #catch an error if the program can't be launched
   
print "External application couldn't be launched!"
win
.winHandle.maximize() #when external program closes, maximize PsychoPy window again
win
.fullscr = True
win
.winHandle.activate() #re-activate window
win
.flip() #redraw the newly activated window

Hope that helps :)

Regards, 
Jan
Message has been deleted

Kevin Anderson

unread,
May 21, 2014, 6:11:10 PM5/21/14
to psychop...@googlegroups.com
Hi All, 

I'm hoping I can piggy-back this thread :) I am attempting to call a python script from within a PsychoPy experiment. I've tried the suggestions listed above, but always receive the following error:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/py2app-0.9-py2.7.egg/py2app/apptemplate/lib/site.py", line 22, in <module>
zipimport.ZipImportError: can't decompress data; zlib not available

This is the offending line of code:

os.system('/Library/Frameworks/Python.framework/Versions/2.7/bin/python test.py')

Importantly, this error message is thrown no matter which python script I try to call (and if I use subprocess). Also, I can implement the python script using os.system() just fine outside of PsychoPy -- so my questions are, how does PsychoPy use the py2app package? And why would it interfere with the implementation of shell commands?

Thanks for reading!
Kevin 

Jared Roberts

unread,
May 22, 2014, 12:15:22 AM5/22/14
to psychop...@googlegroups.com
Hi Kevin,

Is there a particular reason that you're trying to run the second script using a different interpreter?  My guess is that the shell that psychopy's interpreter opens has environment variables set up to point to psychopy's site-packages directory (although that's just a guess).  Things get a little weird when dealing with multiple versions of the python interpreter.  If you just want to run a simple script, you should define a function in that script, import the script, and run the function.  Here's and example:

http://stackoverflow.com/questions/1186789/what-is-the-best-way-to-call-a-python-script-from-another-python-script

-Jared


--
Jared Roberts
Graduate Student
Neuroscience of Memory, Aging and Dementia Lab
Department of Neurobiology and Behavior
University of California, Irvine

Kevin Anderson

unread,
May 22, 2014, 12:56:42 PM5/22/14
to psychop...@googlegroups.com
Hi Jared, thanks for the quick response!

The script that I'm launching is an interactive maze programmed with Panda3d. I'm hoping to nest it within PsychoPy to keep timing and experiment parameters exact and easy to manipulate (this will be an fMRI study, eventually...). 

Thanks for the link. Importing a function from a script likely won't solve the whole problem though. I need to launch 'ppython', which is panda3d specific interpreter. It works fine on my native version of python2.7, and only gets jumbled when using the PsychoPy's interpreter (which says it's using python2.7). 

Do you know of a way to modify or override PsychoPy shell's default path variables?

Kevin

Jared Roberts

unread,
May 22, 2014, 1:41:44 PM5/22/14
to psychop...@googlegroups.com
Hi Kevin,

I'm not positive that environment variables are actually the problem...  Rather than navigating those treacherous waters, I might suggest installing PsychoPy to your native interpreter rather than using the standalone.  The standalone is really there for convenience (it comes with all dependencies), and it is not necessary to use it in order to use PsychoPy.  Just make sure to install all of the dependencies as well.  There's a list here: http://www.psychopy.org/installation.html#dependencies.

-Jared


--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.

Jonathan Peirce

unread,
May 22, 2014, 2:08:08 PM5/22/14
to psychop...@googlegroups.com
py2app does some magic when it builds the app bundle and I don't really understand it either!

There's currently a work in progress to move to a new way of delivering psychopy with all it's dependencies using an anaconda/miniconda install. This is very exciting but not quite ready yet. So for now, as Jared correctly says, it's a matter of manually installing dependencies to your standard python install until you stop getting errors about "ImportError: No module named XXXX"

best wishes,
Jon

Kevin Anderson

unread,
May 22, 2014, 5:25:14 PM5/22/14
to psychop...@googlegroups.com
Thank you guys for the help! Is it the case that PsychoPy will only run on 32-bit python?

Best,
Kevin
Reply all
Reply to author
Forward
0 new messages