Skip to main content
 首页 » 编程设计

python之从 WSL 中调用 Windows Python 的奇怪行为(Windows 上的 Bash)

2025年01月19日12落叶无声

安装 WSL 后,我在我的 PC 上安装了两个 python,一个是 Windows,另一个是 WSL。开发商声明here您“可以直接从 WSL 命令行调用 Windows 二进制文件”,但是,当我尝试调用 python.exe 文件时,出现以下奇怪行为:

myprompt$ python.exe 
1 
2  
print("test") 
print "test" 
    File "<stdin>", line 4 
        print "test" 
                   ^ 
SyntaxError: Missing parentheses in call to 'print'  
myprompt$ 

而如果交互式解释器正在运行,我会得到:

myprompt$ python.exe 
>>>1 
1 
>>>2  
2 
print("test") 
test 
print "test" 
    File "<stdin>", line 1 
        print "test" 
                   ^ 
SyntaxError: Missing parentheses in call to 'print'  
myprompt$ 

它就像解释器正在运行,但它没有给我提示,并在第一个错误时退出。有趣的是,如果我导航到同一目录并从 Windows 命令提示符运行 python.exe,一切正常。

P.S.:我通过在 Python 中运行 sys.executable 命令找到了 Windows Python 安装的位置。

请您参考如下方法:

使用 -i 选项调出交互式解释器。于是跑起来

python.exe -i

将从 WSL 以交互方式启动 python 的 windows 版本。