Python Forum

Full Version: Is Python Installed?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Good morning,

I am new to the group and have a question I am hoping everyone can help with. I am working on a application and want to find out if there is a way at the initial run of the app, to check and see if Python is installed on a users system before it loads, regardless of if it is Windows, Mac or Linux?

Any help would be appreciated Smile
On Mac and Linux you can use

which python3

or

command -v python3
(Jan-25-2020, 04:03 PM)Axel_Erfurt Wrote: [ -> ]On Mac and Linux you can use

which python3

or

command -v python3

Thank you Axel, that is a big help. I appreciate your response Smile
Of course, which should return 0 if the program exists and non-zero if it can't find it, as is the convention on UNIX.
(Jan-25-2020, 05:13 PM)ndc85430 Wrote: [ -> ]Of course, which should return 0 if the program exists and non-zero if it can't find it, as is the convention on UNIX.

In Linux which returns the path of the executable


Output:
axel@SamsungSA-11:/tmp$ which python3 /usr/bin/python3
By "return", I meant the exit code of the program, which in Bash, is stored in $?:

Output:
$ which gcc /usr/bin/gcc $ echo $? 0 $ which foobar which: no foobar in (/usr/lib64/go1.11.4/go/bin:/usr/local/bin:/usr/bin:/bin:/usr/games:/usr/lib64/java/bin:/usr/lib64/java/jre/bin:/usr/lib64/kde4/libexec:/usr/lib64/qt/bin:/usr/share/texmf/bin:/usr/local/heroku/bin) $ echo $? 1
Obviously if you're doing this in a Python program (e.g. using subprocess), there will be a way to obtain the return value so you can test it.
Hm, if python program runs, obviously the python is installed. And if it is not installed, there is no way that a "running" python program would check if python is installed :-) it will just fail to execute it ($ python your_program.py)

now, you can check if some third-party package required is installed (or to be more precise - handle the error if it is not installed).

If your goal is to ship your program without need for end user to install python, there are plenty of options - compile it to binary executable, ship python interpreter bundled in the distribution, use containerization (Docker), etc.