Embedding Python inside an experiment

Christopher Stawarz's Avatar

Christopher Stawarz

23 Jun, 2015 03:09 PM

Hi Mark,

As we have previously discussed, I've implemented a mechanism for running Python code inside an MWorks experiment (i.e. within the MWServer process).

MWorks now supports two Python-related actions: run_python_file, which takes the path to a Python source file and executes it, and run_python_string, which executes a string of Python code. In both cases, the code is executed within Python's __main__ module, so any variables, functions, classes, etc. defined by one Python action are available to subsequent ones. My thinking is that you'll want to set things up inside a Python source file, which gets executed once at the beginning of your protocol via run_python_file, and then call functions and methods as needed with run_python_string.

To enable interaction with the rest of your experiment, Python code has access to two MWorks-specific functions: getvar, which takes the name of an MWorks variable and returns its current value, and setvar, which takes the name of a variable and a value and stores the value in the variable. I've attached a very simple example experiment and associated Python script that demonstrates how this all works.

A few things worth noting:

  • Both Python actions are synchronous, meaning no subsequent actions will run until your Python code finishes executing. (You are, of course, free to create new threads from Python, and those threads will run concurrently with the main experiment thread, subject to the limitations imposed by Python's GIL.)

  • The new actions use the system-provided Python (version 2.7 on all currently-supported OS X versions). If you want to install third-party packages, you should do so with /usr/bin/easy_install or install directly in to /Library/Python/2.7/site-packages.

  • Python state is not reset when a new experiment is loaded. This is because the Python interpreter itself can't really be reset within a running process. If your Python code needs to run in a "clean slate" environment, then it'll have to reset things itself.

  • The file used by run_python_file is copied from client to server, just like an image or sound file. However, files imported by that file are not. If your script imports any modules or packages that aren't in the Python standard library, then you'll need to install them on the server machine.

The Python actions are available in the current nightly build. When you have a chance, please try them out and let me know what you think.


  1. 1 Posted by mhisted on 23 Jun, 2015 04:07 PM

    mhisted's Avatar

    Wow. I am really glad you have implemented this, Chris.
    We are in the process of upgrading to a new nightly. When that is done I
    will take a look and test this. Thanks!!!

  2. Christopher Stawarz closed this discussion on 22 Sep, 2015 02:02 PM.

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

Keyboard shortcuts


? 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

Recent Discussions

17 May, 2022 02:12 PM
16 May, 2022 03:12 PM
04 May, 2022 06:02 PM
03 May, 2022 01:30 PM
02 May, 2022 10:47 PM