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 11:14AM

    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 11:14AM

    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

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

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games