Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is Python Installed?
#1
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
Reply
#2
On Mac and Linux you can use

which python3

or

command -v python3
Reply
#3
(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
Reply
#4
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.
Reply
#5
(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
Reply
#6
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.
Reply
#7
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Launcher (on Windows) only necessary when 2 or more Python versions installed? pstein 1 265 Feb-17-2024, 12:43 PM
Last Post: deanhystad
  Installed versions of Python SamHobbs 5 2,464 Sep-02-2021, 02:28 PM
Last Post: jefsummers
  I seem to have two versions of python 3.7.9 installed miner_tom 7 4,353 Nov-22-2020, 06:42 PM
Last Post: miner_tom
  Package installed through Anaconda "not found" by Python zonova 0 2,256 May-08-2020, 11:50 PM
Last Post: zonova
  How to change directory to any folder where python is not installed ? firashelou 4 2,573 Apr-03-2020, 02:43 PM
Last Post: firashelou
  In Visual Studio Python is not properly installed jalea148 4 3,037 Sep-26-2019, 12:31 PM
Last Post: snippsat
  Python can't find module installed in anaconda iFunKtion 12 54,201 Mar-06-2019, 05:15 PM
Last Post: jwsmallz
  can same time have installed Python 3.7.2 64-bit ??? lsepolis123 5 3,413 Jan-17-2019, 11:29 AM
Last Post: snippsat
  Python nuget package for visual studio not getting installed kuldeep26 0 3,655 Dec-27-2017, 08:14 AM
Last Post: kuldeep26
  Issue in launching spyder that was installed using pip install on Python 3.6.1 python300 6 10,994 Jun-02-2017, 10:44 PM
Last Post: python300

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020