【发布时间】:2020-03-09 20:01:31
【问题描述】:
环境:
Windows: 10
Python: 2.7.13 and 3.8.1
default Python launcher: py -3
Default python: 2.7.13
> python -V
> Python 2.7.13
> py -3 -V
> Python 3.8.1
启动器.py:
import subprocess
subprocess.call(['python', '-V'])
- 测试 1:
py -3 launcher.py输出:python 3.8.1(如何!)
- 测试 2:
py -2 launcher.py输出:Python 2.7.13
输出应该只是Python 2.7.13,即使启动器在py 3下运行!
请注意,添加shell=True 会起作用,但我的想法是不要使用它,如果我跑了
subprocess.call(['python', 'script_under_py_2.py']) # Will run python 3 with script python 2!
谢谢
亚当
【问题讨论】:
-
有趣!当我在 macOS 上执行此操作时,我得到了正确的结果:python2 和 python3 都显示相同的
python -V。 -
更改 launcher.py 以使用
sys.executable而不是'python'。前者是当前运行的 Python 解释器的路径。 -
@martineau 这与 OP 想要的相反。由于我无法想象的原因,他似乎想要一个可以在 python 2 或 python 3 下运行但最终会启动 python 2 子进程的启动器。所以,如果用
py -3启动,他实际上是想保证目标是notsys.executable
标签: python python-3.x subprocess