Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch Script to Execute script with python 3 if available else python 2

Trying to create a batch script for windows that runs a program with python3 if available else python2.

I know the script can be executed with $py -2 script.py and py3 with $py -3 script.py.

and if I run py -0, it returns all the python versions.

How do I build this script?

I do not want to check if the python directory is available or not, I'd prefer to check in a way that is python location agnostic.

like image 679
azazelspeaks Avatar asked Mar 28 '26 16:03

azazelspeaks


1 Answers

Not a full solution, but a method to detect which version of Python is installed:

You can check if Python 3 is installed by running py -3 --version and then checking the %ERRORLEVEL% variable in the batch script. If it is 0, then py -3 --version was successful, i.e. Python 3 is installed on the system. If it is nonzero, then Python 3 is not installed.

like image 182
Mikhail Burshteyn Avatar answered Apr 02 '26 05:04

Mikhail Burshteyn