0

I'm trying to set up a script that will install python automatically, and I'm stuck on setting up the user path. I have only a vague clue about what I'm doing here so please excuse me if I'm using any terms incorrectly.

I'm trying to set the environment variables using setx path "%PATH%;%LOCALAPPDATA%\Programs\Python\Python310"\ but I've run into several issues. I finally have this command not failing because of 'multiple default arguments' or something but now when trying to set PATH, I get duplicate entries.

If originally %PATH% gave me '\path1;\path2', and I run setx path "%PATH%;\path3", %PATH% outputs '\path1;\path2;\path1;\path2;\path3', when I expected to have '\path1;\path2;\path3'

As per what I've been reading from other answers, I think %PATH% gives you the combined SYSTEM and USER paths, but setx path modifies the USER path only. So everytime I run setx path I'm adding the system variables again.

I just want to add my python.exe location to the user path variable in a .bat script without this duplicating issue. Does anyone have a working solution?

Diego Cuadros
  • 196
  • 1
  • 12
  • 1
    See for example [Why are other folder paths also added to system PATH with SetX and not only the specified folder path?](https://stackoverflow.com/a/25919222/3074564) or [How to search and replace a string in environment variable PATH?](https://stackoverflow.com/a/24650324/3074564) or [Adding the current directory to Windows path permanently](https://stackoverflow.com/a/47080452/3074564). – Mofi Oct 07 '22 at 05:23
  • 1
    But if you need to add the *Python* path only on your PC manually to __user__ environment variable, do not use a Windows Command Prompt and the command __SETX__ at all. Click on the Windows __Start__ button and type on the keyboard __environment__ and Windows offers in language of Windows the items __Edit environment variables for your account__ and __Edit the system environment variables__. Click on the first item for __user__ environment variables and the window __Environment Variables__ opens with the upper pane listing the __user__ environment variables. Double click on __Path__ to edit. – Mofi Oct 07 '22 at 05:27
  • 1
    The path to add to the __user__ environment variable `Path` is `%LOCALAPPDATA%\Programs\Python\Python310` exactly as written here. Press twice the button __OK__ and best restart Windows to get the `Path` modification active for all running processes started with your user account. Please take a look on [What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?](https://stackoverflow.com/a/41461002/3074564) – Mofi Oct 07 '22 at 05:31

1 Answers1

0
@ECHO OFF
SETLOCAL
SET "python=%%USERPROFILE%%\AppData\Local\Microsoft\WindowsApps"
ECHO %path%>u:\pp.txt


FOR /f "tokens=1,2,*" %%u IN ('reg query HKCU\Environment') DO IF "%%u"=="Path" (
FIND /v "%python%" "u:\pp.txt" >NUL
IF NOT ERRORLEVEL 1 (
 ECHO CHANGE path
 ECHO SETX PATH "%%w;%python%"
 )
)

DEL u:\pp.txt

GOTO :EOF

I used a path that I have installed as python. Note that the % need to be doubled.

Write the current path to a tempfile (u:\pp.txt is simply on a RAMDRIVE for me)

Read the environment data from the registry, tokenise and select for the first item in %%u being Path. Its value will be in %%w.

See whether the "python path" is already in the path; if not, errorlevel will be 1 so execute the setx.

  • I merely echoed the setx as I'm not going to change the registry. If the command echoed appears correct, remove the echo to actually execute the setx.

It may be an idea to also set the path in the current environment, as setx changes the variable's value for future instances, not for the current one.

===== Revision ==== in the light of comments:

@ECHO Off
SETLOCAL
SET "python=%%USERPROFILE%%\AppData\Local\Microsoft\WindowsApps"
ECHO %path%>u:\pp.txt


FOR /f "tokens=1,2,*" %%u IN ('reg query HKCU\Environment') DO IF /i "%%u"=="Path" (
 FIND /v "%python%" "u:\pp.txt" >NUL
 IF NOT ERRORLEVEL 1 (
  SETLOCAL ENABLEDELAYEDEXPANSION
  SET "xpath=%%w;%python%"
  SET "xpath=!xpath:~159!"
  IF DEFINED xpath (ECHO PATH too long) ELSE (
   ECHO CHANGE path
   ECHO SETX %%u "%%w;%python%"
  )
  ENDLOCAL
 )
)

DEL u:\pp*.txt

GOTO :EOF

Fixes:

  1. Comparison in for ...%%u made case-insensitive.

  2. Length of resultant user-path variable checked. I used a value of 159 for testing, 1022 for real-world.

  3. variable name being setx'd will be identical to that retrieved from the registry. (For me, it's Path (W11 22H2) - My editor helpfully changes any batch keyword followed by a space to upper-case)

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • __Note 1:__ The truncation is done after 1024 characters by __SETX__. So it is possible to use `SET "xpath=!xpath:~1024!"` as I verified explicitly. __Note 2:__ The batch file does not create the __user__ environment variable `Path` on not existing at all as in this case the __IF__ condition is never true. __Note 3:__ Enabled delayed expansion cause wrong behavior and `Path` value corruption on __user__ `Path` already existing and containing by chance a path with an exclamation mark. __Note 4:__ The script does not check if existing `Path` value ends already with a semicolon. – Mofi Oct 08 '22 at 11:36