Python Bridge Issue

Yvonne Bowman's Avatar

Yvonne Bowman

05 Jul, 2018 05:15 PM

Hi Chris,

We're attempting to write a python bridge to write some variables to a file, but it won't write the file when called through MWorks. In our messing around with it we have the following in Works to call it:

run_python_file(stop_on_failure = YES
path = 'rrr.py'
)

and the following in the python file:

a = getvar('a')
setvar('a', a +10)

f = open('workfile.txt', 'a')
f.write('\nHello')
f.close
print 1212

The effect of this is that when MWorks is run, and reports the variable a, it adds 10 to the variable, meaning the python script was successfully run, but it doesn't write to the file. When running python through the terminal, however, it writes "Hello" to a file just fine. Is there anything else that should go into a python bridge of this sort?

Thanks,
Yvonne

  1. Support Staff 1 Posted by Christopher Sta... on 05 Jul, 2018 06:53 PM

    Christopher Stawarz's Avatar

    Hi Yvonne,

    f.close

    You haven't actually called "close" here. You need to add some parentheses:

    f.close()
    

    See if that fixes it.

    Cheers,
    Chris

  2. 2 Posted by Yvonne Bowman on 05 Jul, 2018 08:44 PM

    Yvonne Bowman's Avatar

    Hi Chris,

    This fix doesn't seem to change the behavior of the code at all.

    Best,
    Yvonne

  3. Support Staff 3 Posted by Christopher Sta... on 06 Jul, 2018 02:24 PM

    Christopher Stawarz's Avatar

    Ok. I thought maybe the write hadn't been flushed, but I guess that's not the problem.

    f = open('workfile.txt', 'a')

    Next guess: If you're writing to a filename with a relative (or empty) path, then it will be found/created relative to the working directory of the experiment. This is an experiment-specific subdirectory of $TMPDIR/MWorks/Experiment Cache on the machine running MWServer. Since this is probably not where you actually want the file to be created, you need to include an absolute path in the filename, e.g.

    f = open('/Users/yvonne/something/workfile.txt', 'a')
    

    As an aside, this is why the create_whitenoise_images.py script I gave you needs imagepath to be absolute.

    Sorry, I realize this is not at all obvious! But hopefully it's the correct fix.

    Cheers,
    Chris

  4. Christopher Stawarz closed this discussion on 24 Jul, 2018 01:12 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac