16

I need to run the 2nd command on windows cmd only if the 1st one fails, in another scneario, I want to open python setup after checking if it is installed or not.

I used this command

python --version || path/to/python_install.exe

as I know the || means run if the last command failed. but it only runs the first one.

Ahmed Wagdi
  • 3,913
  • 10
  • 50
  • 116
  • So you want to run the python installer if `python --version` "fails". But does it fail? What happens when you run `python --version`? What's the exit status? – glenn jackman Jun 22 '19 at 12:30
  • I've removed all python versions from my pc and now it gives me `'python' is not recognized as an internal or external command, operable program or batch file.` – Ahmed Wagdi Jun 22 '19 at 12:33
  • I didn't notice the [tag:windows] and [tag:cmd] tags. I see what you mean. Apparently windows thinks there's something special about the `python` command. I don't have python installed, and when I enter the `python` command without options, the Microsoft Store opens. Try entering `where python` in the cmd window. – glenn jackman Jun 22 '19 at 12:43
  • 2
    `missingcommand || echo command not found` does work as expected, so your question needs to be more specifically "How do I test if Python is installed on Windows (windows-version)`? – glenn jackman Jun 22 '19 at 12:47
  • 2
    I found this relevant question: [Typing “python” on Windows 10 (version 1903) command prompt opens Microsoft store](https://superuser.com/questions/1437590/typing-python-on-windows-10-version-1903-command-prompt-opens-microsoft-stor) – glenn jackman Jun 22 '19 at 12:49
  • @glennjackman I can't get it, what is `missingcommand || echo command not found` and how can it help me to run the installer if it is not installed !! – Ahmed Wagdi Jun 22 '19 at 12:51
  • I'm saying the problem is not the `A || B` construct, that works fine. The problem is that Microsoft has installed a `python.exe` program without you knowing, and that's interfering with your plan. The `A` part is *not failing*, so the `B` part is not invoked. Read the linked question for the solution. – glenn jackman Jun 22 '19 at 16:03

3 Answers3

11
  1. Open Command Prompt > Type Python Or py > Hit Enter If Python Is Installed it will show the version Details Otherwise It will Open Microsoft Store To Download From Microsoft Store

  2. Just go in cmd and type where python if it installed it will open a prompt .

Sometimes it may not work if environment variable is not set up, so you can also check by where python in cmd. If where python returns something hot to that path and see for python.exe

Sagar Suba
  • 119
  • 1
  • 4
10

All of the comments guided me to the right way to do it.

I used this great working code:

:: Check for Python Installation
python --version 3>NUL
if errorlevel 1 goto errorNoPython

:: Reaching here means Python is installed.
:: Execute stuff...

:: Once done, exit the batch file -- skips executing the errorNoPython section
goto:eof

:errorNoPython
echo.
echo Error^: Python not installed
"C:\Program Files\used\systems\innoventiq\accumanager\required\excutables\python-3.7.3-amd64.exe"
Mofi
  • 46,139
  • 17
  • 80
  • 143
Ahmed Wagdi
  • 3,913
  • 10
  • 50
  • 116
  • 2
    You should write `goto :EOF` and not `goto:eof` which works only because of automatic error correction by `cmd.exe`. The command is __GOTO__ and the first argument (argument 1) is `:EOF` which should be separated by a space character from the command (argument 0). That can be seen by opening a command prompt window and run `goto /?` which outputs the help for this command explaining `goto :EOF`. There is no need to escape with `^` a colon. So last but one line works also with `echo Error: Python not installed`. – Mofi Jun 27 '19 at 06:07
  • I recommend reading also DosTips forum topic: [ECHO. FAILS to give text or blank line - Instead use ECHO/](https://www.dostips.com/forum/viewtopic.php?f=3&t=774) and replace `echo.` by `echo/` or `echo(` to output an empty line and suggest further reading [Where does GOTO :EOF return to?](https://stackoverflow.com/a/37518000/3074564) – Mofi Jun 27 '19 at 06:09
  • `python --version 3>NUL` should be modified to `python --version 2>NUL` to really suppress the error message output by `cmd.exe` to handle STDERR with handle number __2__ and not *3* by redirecting it to device __NUL__. See Microsoft article about [Using command redirection operators](https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490982(v=technet.10)). Well, your code assumes that path of folder containing *Python* executable is in `PATH`. That's not good in my point of view, but if you and your customers are happy with it, it's okay. – Mofi Jun 27 '19 at 06:14
  • Where to run this? – Julien Mar 01 '23 at 10:23
  • if anyone needs this for powershell `&{ python3 --version 2>&1 >$NULL; $? }` will return True for python3 being installed, and False otherwise – Hashbrown Jul 02 '23 at 12:53
0

try these commands: python3 for the python version over 3.x or python for the rest of the version of python

CHANDER
  • 21
  • 4
  • With Windows 10 v 19044, if you type the name of an application that is not installed, then Windows will take you to the Microsoft store. What application is started when you type python will depend on how the environment variables are setup on the system and not simply the name of the executable. The order of the path variables or whether there is a python application in the path will dictate which version of python is executed when you type in python. The accepted answer is a more complete answer to the posed question. – jaesle Aug 01 '22 at 17:27