我需要从外部SD卡运行Python脚本,它在目标设备中挂载为/mnt/sdcard/_ExternalSD。
我通过在标准SL4A脚本目录中准备“代理”脚本ExtSDRun.py,在我的目标设备是/mnt/sdcard/sl4a/scripts的情况下,成功地以一种快速而肮脏的方式完成了这项工作
import android
droid = android.Android()
import os
import sys
# Add directory with the scripts to the Python path
sys.path.append("/mnt/sdcard/_ExternalSD/scripts")
# Start the script located in the external SD card
# in the script_to_run.py file
import script_to_run
# You can also do:
# from script_to_run import *有没有更好更优雅的方法来实现这个目标呢?
发布于 2013-07-09 22:00:15
我非常确定您可以从外部SD卡运行脚本。试试this。这是一个简单的Python函数,它可以启动SL4A安装了解释器的任何脚本,只要给出脚本的任意路径。我没有外部卡来测试它,但我认为它没有失败的理由。
发布于 2013-06-27 04:45:21
简而言之:你不能。做你正在做的事情的一个更好的方法是把一个主函数放在script_to_run中。例如,如果script_to_run包含以下内容:
import sys
sys.stdout.write('Hi!\n') #Technically I should use print, but I'm trying
#to make the program longer.你会这样做:
import sys
def main():
sys.stdout.write('Hi!\n')然后,在导入它时,使用:
import script_to_run
script_to_run.main() #This is what runs the script另请参见https://stackoverflow.com/a/4463726/2097780。我不建议这样做,但如果你不能以另一种方式调用main,这可能是一种选择。
祝好运!
https://stackoverflow.com/questions/17052863
复制相似问题