Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

[solved] Importing Python modules on Android?

edited December 2015 in OpenSesame

Hello again,

I am working on an experiment in which I read in coordinates from a CSV file (and then use those to draw lines which are essentially my stimuli). I wrote the code and everything works nicely in OpenSesame on my Mac. Now I want to deploy it to an Android tablet but the experiment crashes at the following line:

import csv

I am using functions from the csv module to read the .csv files (duh). I could probably work around this and write my own function to read in the csv files and extract the necessary data but I was wondering whether there's a way to make modules available on an Android tablet?

(I found this bit about available modules for the Android runtime but that doesn't really answer my question.)


  • Florian


  • floflo
    edited 7:19PM

    I tried to work around this issue by saving the content of the .csv file as a .py file. The content of the .py file is something like this:

    coord = [['-514', '-352'], ['-335', '0'], ['-181', '-2'], ['-168', '102'], ['-67', '104'], ['0', '234'], ['2', '-284'], ['160', '0'], ['328', '-2'], ['444', '174'], ['486', '2']]

    Then I use:

    exec( exp.get_file('')  )

    This, I hope, should produce a list called coord that contains my coordinates.

    The file is in the experiment's file pool. This gives me the following error, though:

    Traceback (also in debug window):
    File "/Applications/", line 150, in prepare
    File "/Applications/", line 111, in _exec
    File "", line 26, in
    File "", line 1
    SyntaxError: invalid syntax

    Something along these lines should work, right? What am I missing?


    • Florian
  • edited 7:19PM

    Hi Florian,

    There are two questions here: The first is whether and how you can use the csv module on Android; the second is how you can execute a .py script from the file pool.

    First, the csv module is part of the Python standard library, so you should just be able to use it on Android. No problem there.

    Second, with exec() you execute a string of text that contains Python code. For example ...


    ... will print 'hi!'. What you're trying to do is execute a filename. The contents of the file are a Python script, of course, but the filename itself is not. Do you see? So, if anything, you'll want to do this:


    This will run the contents of in the Python workspace that is used by OpenSesame. (exec() will normally use its own workspace.)


    There's much bigger issues in the world, I know. But I first have to take care of the world I know.

  • floflo
    edited December 2015

    Hi Sebastiaan,

    thank you for your time. You are right, I am mixing up two issues in the same thread, sorry about the mess.

    Importing the csv module

    It does run without any issues on my computer but not on the tablet. Here's the crash report from my testing yesterday:

    Launching /sdcard/Experiments/mirror tracing coordinates.opensesame.tar.gz
    openexp.sampler._legacy.init_sound(): sampling freq = 48000, buffer size = 1024 experiment started at Tue Dec  8 07:50:08 2015
    Traceback (most recent call last):
      File "opensesame/", line 122, in main
      File "opensesame/libopensesame/", line 390, in run
      File "opensesame/libopensesame/", line 95, in execute
      File "opensesame/libopensesame/", line 110, in run
      File "opensesame/libopensesame/", line 49, in run
      File "opensesame/libopensesame/", line 110, in run
      File "opensesame/libopensesame/", line 107, in run
    Error while executing inline script
    item-stack: experiment[run].setup_script[run]
    exception type: ImportError
    exception message: No module named csv
    item: setup_script
    time: Tue Dec  8 07:50:08 2015
    phase: run
      File "opensesame/libopensesame/", line 102, in run
      File "opensesame/libopensesame/", line 160, in _exec
      File "<string>", line 6, in <module>
    ImportError: No module named csv

    By the way, I am also using the line "from random import shuffle" in my script and that doesn't give me any trouble (neither on my computer nor on the tablet). Not sure whether that's relevant...

    Executing strings of text as code

    Ahh, I understand the difference. Thank you. :) I still wasn't able to get it to work, though. OpenSesame tells me:

    AttributeError: 'python_workspace' object has no attribute 'run_file'

    Again, thank you!

    • Florian
  • floflo
    edited December 2015

    EDIT: Okay, I am an idiot. Your line of code works like a charm - on the tablet. :D

    (I deleted the post I wrote here before...)

Sign In or Register to comment.