1

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?

OLIVER.KOO
  • 5,654
  • 3
  • 30
  • 62
  • Probably M1 processor is not supported by subprocess module yet or has bug, maybe open up a bug with python devs – eshirvana Mar 02 '22 at 01:26
  • The output you are looking at is more or less identical as far as I can tell. You probably need to understand the difference between the `repr()` of a string and the result of printing it. Or are you asking why it's reporting an unknown processor? – tripleee Mar 02 '22 at 06:18
  • Can't reproduce. Some wild ideas: Could you be running different copies of system_profiler? Since you're on Apple Silicon, Is Python running under Rosetta? Any differences in environment variables? – Ture Pålsson Mar 02 '22 at 11:24

1 Answers1

1

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:

arch -x86_64 /bin/zsh -c '/usr/sbin/system_profiler SPHardwareDataType'

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:

import os
import subprocess
subprocess.check_output(["arch","-arm64","/bin/zsh","-c",'/usr/sbin/system_profiler SPHardwareDataType'],text=True, env=os.environ)

Hope this helps.

I'm running python 3.7.5 and this was what was happening to me.

nigago
  • 11
  • 1