如何在 Python 中使用当前的 Python 解释器运行外部 Python 脚本? [英] How in Python to run external Python script with the current Python interpreter?

查看:49
本文介绍了如何在 Python 中使用当前的 Python 解释器运行外部 Python 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在修改别人的 Python 代码,它目前使用系统默认的 Python 同步执行外部 Python 脚本(fast-downward)(并获取返回码):

I'm modifying someone else's Python code, and it currently synchronously executes an external Python script (fast-downward) using the system's default Python (and gets the return code):

code = os.system("%s/src/translate/translate.py %s %s" % (down_home, domain, pddl) # ...

我不想将 /usr/local/bin/python2.7 设为我的默认 Python 解释器(CentOS 附带一个较旧的 Python).如何使用当前的 Python 解释器调用外部 Python 脚本?

I don't want to make /usr/local/bin/python2.7 my default Python interpreter (CentOS ships with an older Python). How can I invoke an external Python script using the current Python interpreter?

我不想分叉.我会尝试这个建议,但我需要返回码.

I don't want to fork. I'll try the suggestion, but I need the return code.

推荐答案

您可能只想将 virtualenv 与新的解释器一起使用.您可以安装新的 Python,但不要将其设为主要系统 Python.或者,您可能已经安装了 Python 2.7.

You might just want to use virtualenv with the new interpreter. You can install the new Python, but don't make it the primary system Python. Or maybe you already have Python 2.7 installed.

https://github.com/0xdata/h2o/wiki/Installing-python-2.7-on-centos-6.3.-Follow-this-sequence-exactly-for-centos-machine-only

然后

pip install virtualenv
virtualenv venv --distribute -p /usr/local/bin/python2.7
source venv/bin/activate

现在您的路径将被调整到包含新 Python 的本地 venv 目录.可以安装依赖等,它们只会安装到venv中.这是将您的环境彼此隔离的好方法.

Now your path will be adjusted to the local venv directory, containing the new Python. You can install dependencies, etc., and they will only be installed into venv. This is a great way to isolate your environments from each other.

这篇关于如何在 Python 中使用当前的 Python 解释器运行外部 Python 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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