在运行时添加python脚本 [英] Add a python script at runtime

查看:110
本文介绍了在运行时添加python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开始python并在一个项目上工作,而我想做的一件事是在运行时动态下载并运行脚本。

I am beginning python and working on a project, and one thing I would like to be able to do is download and run a script dynamically at runtime.

通常的想法是能够连接到服务器,按需下载python脚本,并运行刚刚下载的脚本,而无需重新启动程序或将特定脚本写入程序的硬代码。

The general idea is to be able to connect to a server, download a python script on demand, and run that script that was just downloaded without having to restart the program or hard code that specific script in to the program.

我正在使用的程序将Python用于脚本编写和执行,并将C ++用于其他代码(例如渲染和数学)。

The program I'm working with uses Python for scripting and execution, and C++ for the other code such as rendering and math.

我希望能够使用python运行从服务器下载的脚本(用于用户生成的内容)。

I would like to be able to use python to run scripts that have been downloaded from a server (for things such as user generated content).

我想知道是否有可能,或者是否应该针对这种情况使用另一种脚本语言(可能是LUA)?

I was wondering if that was possible, or if I should use another scripting language (possibly LUA) for that situation?

谢谢,
Th3flyboy

Thanks, Th3flyboy

推荐答案

您绝对可以做到这一点。使用 urllib 下载或生成脚本,然后将其导入。每次将脚本下载到PYTHONPATH时,它每次都像魅力一样。

You can definitely do this. Use urllib to download or generate the script, then import it. It'll work like a charm every time, as long as you download the script to your PYTHONPATH.

例如:

from urllib import urlretrieve
urlretrieve('http://www.mysite.com/myscript.py', '/home/me/script.py')
import script

您还可以自己从模板生成脚本,从在线获取数据(也许以xml或数据库的形式),并使用Python的文本处理功能进行必要的调整。

You can also generate the script yourself from a template, pulling data from online (perhaps in xml or from a database) and using Python's text processing capabilities to make the necessary adjustments.

这篇关于在运行时添加python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆