0

I created a very simple batch file as a launcher for a python script, however it does not work fine depending on the "PATH" setup for the user (Python 2 x Python 3). Since changing the PATH can bring issues with other Python 2 based applications , could I make any update in the .bat to make a temporary change in the PATH while the Py script is running?
The idea is to use the batch to have the minimum interference from the user in the system setup (preferable a double click only). In case of yes, what could I do?

@echo off
title ###Beta Script Launcher###
python myscript.py 
pause
4
  • Possible duplicate of Can a script.bat make changes to Windows PATH Environment Variable Commented May 31, 2019 at 7:17
  • if you change the %PATH% variable with the set command, the changed value will only be valid in the current cmd process and its children that are executed after the change. Independently created processes will still have the original value. setx works the other way: it changes the value for any independently started processes in the future. Commented May 31, 2019 at 8:15
  • @Stephan - I made your suggestion using: set path "%path%;C:\Users\user\Programs\Python\Python37-32" prior calling for python in my .bat, but the output was the list of the PATH Env. variables currently listed and the message that the address I tried to add is not defined. Commented Jun 3, 2019 at 8:24
  • you got the wrong syntax. set and setx have different syntax (yes, very confusing). See set /? and setx /? Commented Jun 3, 2019 at 8:27

2 Answers 2

0

To make a temporary change to the path variable:

path c:\mydir;%path%

the documentation

c:\srv> path /?
Displays or sets a search path for executable files.

PATH [[drive:]path[;...][;%PATH%]
PATH ;

Type PATH ; to clear all search-path settings and direct cmd.exe to search
only in the current directory.
Type PATH without parameters to display the current path.
Including %PATH% in the new path setting causes the old path to be
appended to the new setting.

the normal way to do this for python, however, is to create a virtualenv (https://pypi.org/project/virtualenvwrapper-win/)

mkvirtualenv myenv
.. install everything in this environment ..

and then use it in your start script

@echo off
title ###Beta Script Launcher###
call workon myenv
python myscript.py 
pause

(disclaimer, I'm one of the maintainers of virtualenvwrapper-win)

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

2 Comments

I did not used the virtualenv tool, since most of users may not used to intall py packets, but the cmd path syntax helped a lot to define the best strategy in the batch file. Thanks.
If you want to distribute your code you should create a setup.py file. It's easy to do, and makes it really easy for your users...
-1

Yes you can edit the environment variables in batch script by using the following command

setx path "%PATH%;C:\New Folder" 

based on wherever your python that you want to use is installed. But this will change the path permanently

2 Comments

You may want to refer to this thread for more info : stackoverflow.com/questions/3835518/…
NO, NO and once more NO. This line corrupts the user PATH environment variable. It uses local PATH with all folder paths of system and user PATH with all environment variables expanded to set user PATH with C:\New Folder appended. This line is really, really bad and should be never used by anyone reading this answer. It is awful. The usage of local PATH to set user or system PATH is an absolute NO GO, NEVER EVER.

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.