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 line
Chip: Apple M1
but this line is not present if running via subprocess, instead this line shows up
Processor Name: Unknown
strange?
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.