Home > Blockchain >  Check if Python3 is installed without misleading macOS installation window
Check if Python3 is installed without misleading macOS installation window

Time:02-18

I've tried to google and have failed to find any answers.

I'm trying to check if Python 3 is installed and its version.

I'm using this command:

python3 --version

Unfortunately, in case Python 3 is NOT installed, this command produces this window instead of just printing some error message to the console:

enter image description here

The python3 command requires the command line developer tools. Would you like to install the tools now?

This unwanted window makes our users think that our app needs them to install this stuff and they do install all of it, which includes XCode, which they don't need actually.

Is there a way to suppress this window?

Addition #1. For now, I'm thinking about just using which python3 command. If it says /usr/bin/python3 - Python 3 is NOT installed. Is it good solution? If Python 3 is installing by means of standard installer, then this command outputs this path (at least on my machine): /usr/local/bin/python3. I'm curios if some other type of installation (e.g. homebrew) can just replace this /usr/bin/python3. It seems that should never happen, but I'm not sure.

Addition #2. OK, I've checked that XCode replaces this /usr/bin/python3 dummy when it installs Python 3. So the solution in Addition #1 will not work.

Addition #3. It seems that this /usr/bin/python3 dummy is not replaced by XCode. This dummy is just searchs for the actual Python binary installed by XCode, e.g. in this path: /Applications/Xcode.app/Contents/Developer/usr/bin/python3. If it finds it - it launches it. In case of failure, it shows this window.

So, it seems that the working solution would be to check if which python3 command says /usr/bin/python3. If no - we just can safely try to launch python3 --version. If yes - we need to check if /Applications/Xcode.app/Contents/Developer/usr/bin/python3 exists. If yes - again we can safely launch python3 --version. If no - Python 3 is NOT installed.

CodePudding user response:

It seems, that the working solution is to check if which python3 command says /usr/bin/python3.

If no - we just can safely try to launch python3 --version.

If yes - we need to check if /Applications/Xcode.app/Contents/Developer/usr/bin/python3 exists.

If yes - again we can safely launch python3 --version.

If no - Python 3 is NOT installed.

  • Related