首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从外部SD卡运行Py4A脚本?

从外部SD卡运行Py4A脚本?
EN

Stack Overflow用户
提问于 2013-06-12 04:10:18
回答 2查看 949关注 0票数 2

我需要从外部SD卡运行Python脚本,它在目标设备中挂载为/mnt/sdcard/_ExternalSD

我通过在标准SL4A脚本目录中准备“代理”脚本ExtSDRun.py,在我的目标设备是/mnt/sdcard/sl4a/scripts的情况下,成功地以一种快速而肮脏的方式完成了这项工作

代码语言:javascript
复制
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 *

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

EN

回答 2

Stack Overflow用户

发布于 2013-07-09 22:00:15

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

票数 0
EN

Stack Overflow用户

发布于 2013-06-27 04:45:21

简而言之:你不能。做你正在做的事情的一个更好的方法是把一个主函数放在script_to_run中。例如,如果script_to_run包含以下内容:

代码语言:javascript
复制
import sys
sys.stdout.write('Hi!\n') #Technically I should use print, but I'm trying
#to make the program longer.

你会这样做:

代码语言:javascript
复制
import sys

def main():
    sys.stdout.write('Hi!\n')

然后,在导入它时,使用:

代码语言:javascript
复制
import script_to_run
script_to_run.main() #This is what runs the script

另请参见https://stackoverflow.com/a/4463726/2097780。我不建议这样做,但如果你不能以另一种方式调用main,这可能是一种选择。

祝好运!

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17052863

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档