[solved] Importing Python modules on Android?
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
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:
Then I use:
This, I hope, should produce a list called
coordthat contains my coordinates.The file
practice_0.pyis 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!
Hi Florian,
There are two questions here: The first is whether and how you can use the
csvmodule on Android; the second is how you can execute a.pyscript from the file pool.First, the
csvmodule 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
practice_0.pyin the Python workspace that is used by OpenSesame. (exec()will normally use its own workspace.)Cheers!
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
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
csvmoduleIt does run without any issues on my computer but not on the tablet. Here's the crash report from my testing yesterday:
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:
Again, thank you!
EDIT: Okay, I am an idiot. Your line of code works like a charm - on the tablet.
(I deleted the post I wrote here before...)