-1

I have a Windows batch file that calls the python interpreter:

python -m ...

That works. But now I have a second batch file that calls the first batch file. When I run this batch file, I get an error:

'python' is not recognized as an internal or external command, operable program or batch file.

I don't understand why the second batch file doesn't work.

If it helps to have a concrete example, here's one:

In helloworld.py

print("Hello, world!")

In batch1.cmd

@echo off
echo About to call python...

python -m helloworld

pause
exit

In batch2.cmd

@echo off
set "path=%~dp0batch1.cmd"

start "" %path%

Output:

About to call python

'python' is not recognized as an internal or external command, operable program or batch file.

Press any key to continue . . .

Daryl McCullough
  • 303
  • 5
  • 20
  • It's definitely a path thing. If I replace "python" by `C:\Users\smccullo\AppData\Local\Programs\Python\Python36-32\python.exe` it works, but that shouldn't be necessary... – Daryl McCullough Apr 25 '18 at 16:18
  • 1
    Of course the original path variable contains also the path to python. Simply use a different variable name for **yourpath** –  Apr 25 '18 at 16:21
  • 5
    `%path%` is a system variable to tell `cmd` where to find it's executables. Don't overwrite or change it (unless you exactly know, what it does and what's the effect of your changes). Choose another variable name. – Stephan Apr 25 '18 at 16:26
  • 1
    `Call` the batch file or just use `%~dp0batch1.cmd`, don't `start ""` it. Your problem is more than likely related to you emptying the all important `%PATH%` , do not set a variable as `%path%`! – Compo Apr 25 '18 at 16:26
  • That was pretty dumb on my part. I didn't realize that Windows environment variables such as path were case-insensitive. – Daryl McCullough Apr 27 '18 at 14:49

1 Answers1

2

You are completely breaking the system variable %path% by setting it in the batch. Now your system cannot find python anymore.

Simply change second batch to:

@echo off
set "mypath=%~dp0batch1.cmd"
start "" %mypath%

To explain the %path% variable better. It holds the path to any possible file locations to search for files to execute or open without the need to have the full path specified each time by a user. By running a command in cmd like python, it first checks the current directory where the batch started in, if python.exe is not there, it will search each semicolon seperated path in the %path% variable. When you set a new %path% variable it only knows of the newly set path and cannot find python to execute and you get the most common cmdline error on windows.

On another note, if you want to start batch1 in the same window, perhaps consider calling batch instead of starting it

 call "%mypath%"
Gerhard
  • 22,678
  • 7
  • 27
  • 43