从外部SD卡运行Py4A脚本



我需要从外部SD卡运行Python脚本,该SD卡在目标设备中被挂载为/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 *

是否有更好更优雅的方法来实现这个目标?

我很确定您可以从外部SD卡运行脚本。试试这个。这是一个简单的Python函数,只要给定脚本的任意路径,它就可以启动SL4A安装了解释器的脚本。我没有外部卡来测试它,但没有理由它会失败。

简短的回答:你不能。做您正在做的事情的更好的方法是将一个主函数放在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,这可能是一个选项。

祝你好运!

最新更新