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
Alex
  • 878
  • 1
  • 10
  • 25
SantoS
  • 5
  • 4
  • Possible duplicate of [Can a script.bat make changes to Windows PATH Environment Variable](https://stackoverflow.com/questions/3835518/can-a-script-bat-make-changes-to-windows-path-environment-variable) – montonero May 31 '19 at 07: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. – Stephan May 31 '19 at 08: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. – SantoS Jun 03 '19 at 08:24
  • you got the wrong syntax. `set` and `setx` have different syntax (yes, very confusing). See `set /?` and `setx /?` – Stephan Jun 03 '19 at 08:27

2 Answers2

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)

thebjorn
  • 26,297
  • 11
  • 96
  • 138
  • 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. – SantoS Jun 04 '19 at 07:59
  • 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... – thebjorn Jun 04 '19 at 08:58
-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

CodePhobia
  • 1,315
  • 10
  • 19
  • You may want to refer to this thread for more info : https://stackoverflow.com/questions/3835518/can-a-script-bat-make-changes-to-windows-path-environment-variable – CodePhobia May 31 '19 at 06:30
  • 3
    __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. – Mofi May 31 '19 at 11:41