Python 子进程 check_output 返回与直接在终端中运行不同的结果
直接在终端中运行相同的命令会返回与通过 subprocess.check_output 运行不同的输出。有谁知道是什么原因造成的?
➜ /Users/okoo system_profiler SPHardwareDataType
Hardware:
Hardware Overview:
Model Name: MacBook Pro
Model Identifier: MacBookPro17,1
Chip: Apple M1
Total Number of Cores: 8 (4 performance and 4 efficiency)
Memory: 16 GB
System Firmware Version: 7429.81.3
OS Loader Version: 7429.81.3
Serial Number (system): FVFGJ221Q05P
Hardware UUID: 050F2C11-EA98-5EDB-A121-B66B0E810BAE
Provisioning UDID: 00008103-000240323E79001E
Activation Lock Status: Disabled
➜ /Users/okoo python3.9
Python 3.9.10 (main, Jan 15 2022, 11:48:00)
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> import os
>>> subprocess.check_output(["system_profiler", "SPHardwareDataType"], text=True, env=os.environ)
'Hardware:\n\n Hardware Overview:\n\n Model Name: MacBook Pro\n Model Identifier: MacBookPro17,1\n Processor Name: Unknown\n Processor Speed: 2.4 GHz\n Number of Processors: 1\n Total Number of Cores: 8\n L2 Cache: 8 MB\n Memory: 16 GB\n Serial Number (system): FVFGJ221Q05P\n Hardware UUID: 050F2C11-EA98-5EDB-A121-B66B0E810BAE\n Provisioning UDID: 050F2C11-EA98-5EDB-A121-B66B0E810BAE\n Activation Lock Status: Disabled\n\n'
在终端结果中运行一行 芯片:Apple M1
但如果通过子进程运行则不会出现此行,而是显示此行 处理器名称:未知
奇怪?
Running same command directly in terminal returns a different output than running via subprocess.check_output. Anyone know what is causing this?
➜ /Users/okoo system_profiler SPHardwareDataType
Hardware:
Hardware Overview:
Model Name: MacBook Pro
Model Identifier: MacBookPro17,1
Chip: Apple M1
Total Number of Cores: 8 (4 performance and 4 efficiency)
Memory: 16 GB
System Firmware Version: 7429.81.3
OS Loader Version: 7429.81.3
Serial Number (system): FVFGJ221Q05P
Hardware UUID: 050F2C11-EA98-5EDB-A121-B66B0E810BAE
Provisioning UDID: 00008103-000240323E79001E
Activation Lock Status: Disabled
➜ /Users/okoo python3.9
Python 3.9.10 (main, Jan 15 2022, 11:48:00)
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> import os
>>> subprocess.check_output(["system_profiler", "SPHardwareDataType"], text=True, env=os.environ)
'Hardware:\n\n Hardware Overview:\n\n Model Name: MacBook Pro\n Model Identifier: MacBookPro17,1\n Processor Name: Unknown\n Processor Speed: 2.4 GHz\n Number of Processors: 1\n Total Number of Cores: 8\n L2 Cache: 8 MB\n Memory: 16 GB\n Serial Number (system): FVFGJ221Q05P\n Hardware UUID: 050F2C11-EA98-5EDB-A121-B66B0E810BAE\n Provisioning UDID: 050F2C11-EA98-5EDB-A121-B66B0E810BAE\n Activation Lock Status: Disabled\n\n'
running in terminal result a lineChip: Apple M1
but this line is not present if running via subprocess, instead this line shows upProcessor Name: Unknown
strange?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了这个问题并且可以重现它。这是因为 python 和终端运行在不同的 cpu 架构上。这是在苹果硅 (M2) 上。
您可以通过在每个 shell 上运行“arch”来检查这一点。
就我而言,对于终端,它在arm64上运行,而在python(使用子进程调用“arch”)中,它在i386上运行。
如果在终端上运行以下命令,您应该看到与 python 中相同的结果:
我的猜测是,如果你在arm64上运行python,你将得到与终端中相同的结果。如果您只需要快速修复,我执行了以下操作来获取所需的输入:
希望这会有所帮助。
我正在运行 python 3.7.5,这就是发生在我身上的事情。
I ran into this issue and can reproduce it. It was because python and terminal were being run on different cpu architectures. This was on apple Silicon (M2).
You can check this by running "arch" on each shell.
In my case, for terminal it was running on arm64, and in python (calling 'arch' with subprocess) it was running on i386.
If you run the following on terminal, you should see the same result as in python:
My guess is, if you get python running on arm64 you will get the same results as in terminal. If you just need a quick fix, I did the following to get the input I needed:
Hope this helps.
I'm running python 3.7.5 and this was what was happening to me.