从不同的实例执行另一个Python实例
我目前有一个脚本正在由一个带有自己的Python解释器的程序执行。 此版本不包含我需要的库,因此我使用 os.exec 针对本地安装的 2.7 实例执行第二个脚本。
然而,第二个脚本在手动执行时可以完美运行并且没有定义 PYTHONPATH。如果我在执行包装器时记录 PYTHONPATH 的输出,并且在加载第二个脚本时,它们都将 PYTHONPATH 初始化为原始解释器,而这恰好也是一个 2.6 实例。 这显然让我相信我传递了一些我没有预料到的环境变量,此外,在另外 2 个系统上,此设置按预期 100% 工作,而在这一个系统上,这种行为正在发生。 有没有一种更干净的方法可以创建一个包装器来调用第二个脚本?如果不是,我怎样才能避免这种不必要的环境变量传递。
I currently have a script being executed by a program that comes with its own Python interpreter included.
This version does not include libraries I require so I use an os.exec to execute a second script against a locally installed instance of 2.7.
However the second script, when executed manually, works perfectly and doesn't have PYTHONPATH defined. If I log the output of PYTHONPATH when the wrapper is executed and when the second script is loaded they both have PYTHONPATH initialized to the original interpreters, which happens to also be a 2.6 instance.
This obviously has led me to believe I have some environmental variables being passed that I didn't anticipate, furthermore on 2 other systems this setup works 100% as expected whilst on this one system this behavior is happening.
Is there a cleaner way I could create a wrapper to call the second script? If not how could I avoid this unnecessary passing of env variables.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
os.exec*
命令有许多变体:*args
,v
变体使用args
。p
变体使用 PATH 环境变量来查找file
e
变体替换环境。非e
变体使用的环境当前进程。
您可以使用
e
变体之一(例如os.execle
)来控制传递给新进程的环境变量。There are many variants of the
os.exec*
command:l
variants use*args
,v
variants useargs
.p
variants use the PATH environment variable to findfile
e
varients replace the environment. Non-e
variants use the env ofcurrent process.
You could use one of the
e
variants, such asos.execle
, to control the environment variables passed to the new process.您可能想看一下 virtualenv,它允许您基本上创建一个沙箱,您可以在其中创建一个沙箱。安装所有必需的库。
You might want to take a look at virtualenv, that allows one to create basically a sandbox where you can install all the required libraries.