3

I am trying to create a batch file to automatically add my python folder to the Environment Path. Below is my environment variables before the file was run.enter image description here

And this is the file I ran (Note the bat file is in the same directory as the python folder):

@echo OFF
setx path "%path%;%cd%\Python36"

This file added python to path (see the red underline) but also added a bunch of other folders to the path (blue circle). I am confused as to why this occurred. Any help would be greatly appreciated! enter image description here

Compo
  • 36,585
  • 5
  • 27
  • 39
J.C
  • 105
  • 1
  • 1
  • 9
  • 3
    All those paths were in the existing `PATH` variable before you changed it. Those are pretty standard. – Squashman Aug 01 '18 at 14:47
  • 1
    How does the answer below solve your question? – Squashman Aug 01 '18 at 15:26
  • @Squashman When I ran the first command the answer provided it didn't add any of the extra files. – J.C Aug 01 '18 at 15:32
  • The code below does not change the System PATH variable like you stated you wanted to do in your question. – Squashman Aug 01 '18 at 15:34
  • When I deleted all of my path variables, and then ran the answer, it seemingly only added the red underlined line in the photo above. That was what I was trying to ask in my question; adding the red underlined part without the blue circle parts appearing. – J.C Aug 01 '18 at 15:39
  • 2
    @J.C Read the answers on [Why are other folder paths also added to system PATH with SetX and not only the specified folder path?](https://stackoverflow.com/questions/25915767/), [Adding the current directory to Windows path permanently](https://stackoverflow.com/a/47080452/3074564) and [What is the reason for '...' is not recognized as an internal or external command, operable program or batch file?](https://stackoverflow.com/a/41461002/3074564) – Mofi Aug 01 '18 at 15:40
  • 3
    @J.C Why do you want to add `Python36` folder path to __user__ `PATH`? Why not writing the path for example into a file located in `"%APPDATA%\YourAppName\PythonPath.cfg"` and reading it from this file? Or what about adding to __user__ environment variables list with `setx MyAppPythonPath "%CD%\Python36"` the Python path and your program or script uses environment variable `MyAppPythonPath` and do not depend on __local__ `PATH` and `PATHEXT`? Or what about adding Python path to registry key `HKCU\Software\MyApp` with value `PythonPath` and querying this registry value from Windows registry? – Mofi Aug 01 '18 at 16:34

1 Answers1

3

There is a couple different ways.

First - Search for python, then set the $PATH.

FOR /f %%p in ('where python') do SET PYTHONPATH=%%p
ECHO %PYTHONPATH%

Second - If you know where Python is installed, you can use setx to permanently set PATH or PYTHONPATH.

setx path "%path%;C:\Users\Admin\AppData\Local\Programs\Python\PythonVersion;"

I found an extremely intensive PowerShell script for installing Python. Just needs the links updated.

Noah M.
  • 310
  • 1
  • 8
  • 2
    The PowerShell script is as awful as most scripts which update __user__ or __system__ environment variable `PATH` because of replacing `PATH` in registry with __local__ `PATH` with all environment variables expanded, __system__ and __user__ `PATH` concatenated and not checking if folder path to add exists already in either __user__ or __system__ `PATH`. I just can hope not many users use this PowerShell script and those using it could have some troubles with their installed applications. It is as absolute NO GO - NEVER EVER updating __user__ or __system__ `PATH` using __local__ `PATH`. – Mofi Aug 01 '18 at 15:37
  • Thank you for the information. So, it's not the tool, it's the user? Do you have a better example we can learn from, genuinely curious? @Mofi – Noah M. Aug 01 '18 at 15:42
  • 2
    In the `First` example in the answer above there are several issues. It should stipulate either tokens or delimiters, it should be more specific in the locations it intends `WHERE` to search, it should stipulate the extension, to prevent any extension from `%PATHEXT%` being matched, and it should incorporate doublequotes. The `Second` example above, regardless of its lack of doublequoting, is completely nonsensical without further information/expandsion. I will allow you time, @NoahM., to fix those issues, before I choose to downvote your answer, due to it being simply a link to another sites. – Compo Aug 01 '18 at 15:54
  • Both were taken from Python's official documentation. Interesting. So, it's best just to recommend others to use the Python installer if they don't know how to set `PATH` or `PYTHONPATH`. I'll edit the clarity of the second. Thank you. @Compo – Noah M. Aug 01 '18 at 15:58
  • @NoahM., I'm aware that the OP has marked your answer as accepted but not only is your most recent edit insufficient to rectify the issues I've highlighted above, the code once you have done so, still wouldn't answer the question. The `SET` command is local only to the `cmd.exe` session. That means once you've ran either of those examples, even after fixing the issues, the OP's dialog box will not have been altered/updated at all! – Compo Aug 01 '18 at 16:11