psychopy.org | Reference | Downloads | Github

Working directory change with 2020.1 and later?

This is less a problem and more a request for information. Version 2020.1 added this fancy new run window, which is great, but it seems to have strange behavior w/r/t what it sets as the active directory when running a python script. For my purposes in particular (i.e., PyHab), I have scripts that look for a few files in the directory in which the script is saved. However, this is not what PsychoPy recognizes as the working directory (at least on Mac). os.getcwd() instead returns “/Applications/PsychoPy3.app/Contents/Resources”

So 1) why this change, and is it necessary for running a single script? Put bluntly, can we go back to the pre-2020.1 behavior or is there a clear advantage to this new approach?
2) If not, I can get the working directory back where it needs to go with os.path.dirname(os.path.realpath(__file__)), but is this the best way?

My main concerns have to do with replicability across labs - if you want to share a custom script across labs, and it relies on external files, then this is going to require either updating the path manually in every new lab, or including some code to update it automatically. It also seems bad for backward compatibility.

3 Likes

I have submitted a fix: https://github.com/psychopy/psychopy/pull/2819

1 Like

I’m running into this problem too, with the latest PsychoPy3 version running on Windows. Do you know if there’s a way for me to incorporate your fix in a Windows environment before PsychoPy rolls it into their releases? I don’t appear to have the script you modified. Thanks!

There is a temporary fix you can apply to just the script that is having the problem. At the top of your script, put:

import os
os.chdir(os.path.dirname(os.path.realpath(__file__)))

If you want to incorporate the change to PsychoPy itself before the next version rolls out, you can download this file:
https://raw.githubusercontent.com/jfkominsky/psychopy/master/psychopy/app/runner/scriptProcess.py
and then on Windows go to Program Files\PsychoPy3\Lib\site-packages\psychopy\app\runner\ and replace the file of the same name.

Thanks for the follow-up! Yeah, the Windows install does not appear to have the “runner” subdirectory or any file named scriptProcess.py. I was thinking there might be an analogous file with a different name for Windows, but I didn’t want to just go replacing the wrong file! I’ll just use the temp fix meanwhile. Thanks again!

Update: As of 2020.1.3 this is fixed.