0

I am writing utility which does the following things:

  1. Check Python has installed it or not. if Not Installed, Script downloads the python and install it. In the same session, it executes the python script.

But I am facing one issue here, as soon as the script install python, it's throwing an error while executing the python script as python is not recognized in cmd. But if I run the same program again it works fine.

The problem here is - after installation of Python, the command prompt is not identifying the python command unless I restart the window.

is there any way?

Script is: Getting error at below highlighted section

:errorNoPython

echo.
echo Error^: Python not installed
echo.
echo.
echo Downloading Python 3.7.0...
IF EXIST "%CD%\python-3.7.0.exe" (
  echo Found Installer at "%CD%\python-3.7.0.exe"
) ELSE (
  powershell -Command "& {[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12, [Net.SecurityProtocolType]::Tls11, [Net.SecurityProtocolType]::Ssl3, [Net.SecurityProtocolType]::Tls; Invoke-WebRequest -Uri 'https://www.python.org/ftp/python/3.7.0/python-3.7.0.exe' -OutFile '%CD%\python-3.7.0.exe';}"
  echo Python download completed.
)

echo Installing Python...
powershell %CD%\python-3.7.0.exe /quiet InstallAllUsers=0 PrependPath=1 Include_test=0 TargetDir=c:\Python\Python370
setx path "%PATH%;C:\Python\Python370\"

timeout /t 30 /nobreak > nul
echo Python Installation completed.
echo Installing python dependencies.
**start cmd /k python -m pip install requests
start cmd /k python -m pip install pyjavaproperties**
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • Does this answer your question? [Is there a command to refresh environment variables from the command prompt in Windows?](https://stackoverflow.com/questions/171588/is-there-a-command-to-refresh-environment-variables-from-the-command-prompt-in-w) – Mike Scotty Oct 20 '20 at 06:57
  • Even after refreshing environment variables, i am getting below error - 'python' is not recognized as an internal or external command, operable program or batch file. But when I ran same command on command prompt one by one after refreshing environment via resetvers.vbs its working properly but via batch file its not working – Manish Test Oct 20 '20 at 07:45
  • No, Its not working at all. – Manish Test Oct 20 '20 at 08:30
  • One weird observation. If I run python command manually its working as expected but if i am calling python script via batch file its showin error 'python' is not recognized as an internal or external command, – Manish Test Oct 20 '20 at 09:23

1 Answers1

1

In order to reload the environment, you have to close and open cmd.exe

So to get around it, you can set the path and setx

Copy exactly as is, but this will set the path in the current environment:

setx path "%PATH%;C:\Python\Python370\"
set "path=%PATH%;C:\Python\Python370\"
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • Yes, but in my case i am installing python and planning to run some python script. so i cant restart the command prompt. – Manish Test Oct 20 '20 at 09:22
  • Did you read everything? just copy the `set` line as is into your code, below the `setx` line as demonstrated. – Gerhard Oct 20 '20 at 10:00