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?

floflo
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.)

Thanks!

  • Florian

Comments

  • floflo
    edited 4:16AM

    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('practice_0.py')  )
    

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

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


    Traceback (also in debug window):
    File "/Applications/OpenSesame.app/Contents/Resources/lib/python2.7/libopensesame/inline_script.py", line 150, in prepare
    File "/Applications/OpenSesame.app/Contents/Resources/lib/python2.7/libopensesame/python_workspace.py", line 111, in _exec
    File "", line 26, in
    File "", line 1
    /var/folders/7x/q_9tsb610gzcb3_b52fr85gw0000gn/T/tmpj5NFSD.opensesame_pool/practice_0.py
    ^
    SyntaxError: invalid syntax


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

    Thanks!

    • Florian
  • edited 4:16AM

    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 ...

    exec('print("hi!")')
    

    ... 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:

    exp.python_workspace.run_file(pool['practice_0.py'])
    

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

    Cheers!
    Sebastiaan

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

  • 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.run(): experiment started at Tue Dec  8 07:50:08 2015
    Traceback (most recent call last):
      File "opensesame/main.py", line 122, in main
      File "opensesame/libopensesame/experiment.py", line 390, in run
      File "opensesame/libopensesame/item_store.py", line 95, in execute
      File "opensesame/libopensesame/item_store.py", line 110, in run
      File "opensesame/libopensesame/sequence.py", line 49, in run
      File "opensesame/libopensesame/item_store.py", line 110, in run
      File "opensesame/libopensesame/inline_script.py", line 107, in run
    osexception: 
    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
    
    Traceback:
      File "opensesame/libopensesame/inline_script.py", line 102, in run
      File "opensesame/libopensesame/python_workspace.py", 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.