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

7
  • 3
    All those paths were in the existing PATH variable before you changed it. Those are pretty standard. Commented Aug 1, 2018 at 14:47
  • 1
    How does the answer below solve your question? Commented Aug 1, 2018 at 15:26
  • @Squashman When I ran the first command the answer provided it didn't add any of the extra files. Commented Aug 1, 2018 at 15:32
  • 2
  • 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? Commented Aug 1, 2018 at 16:34

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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
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.
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
@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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.