Pergunta

I am trying to create a windows batch file to automatically set the environment variable to use python 2.4 or python 3.3.

Both python 2.4 and 3.3 are installed on my system. Here is my code:

::To toggle between Python24 and Python 33
@echo on
if (%PYTHONHOME:~-2%) == "24" (setx PYTHONHOME "C:\Python33" && setx PATH %PATH:Python24=Python33% ) else (setx PYTHONHOME "C:\Python24" && setx PATH %PATH:Python33=Python24% )
pause

To start with I have PYTHONHOME set to C:\Python24

But the above script gives the following error:

SUCCESS: Specified value was saved.
ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).
Type "SETX /?" for usage.

My PYTHONHOME still points to python 24 and nothing is changed. The setx command does not change the environment variable. What is causing this error?

Foi útil?

Solução

The Windows command line error:

ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).
Type "SETX /?" for usage.

Summary:

You are using a setx command and assigning it multiple tokens when only one is allowed.

How to reproduce this error on Windows:

Open a windows cmd terminal and enter these commands. This throws the error:

C:\Users\Charity>setx FANCYPANTS string with spaces

ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).
Type "SETX /?" for usage.

Do the same command, but quote your string like this:

C:\Users\Charity>setx FANCYPANTS "string with spaces quoted"
SUCCESS: Specified value was saved.
C:\Users\Charity>

The variable was set, restart the cmd terminal here to load changes.

C:\Users\Charity>echo %FANCYPANTS%
string with spaces quoted

The environment variable is saved. Now delete it.

C:\Users\Charity>setx FANCYPANTS ""
SUCCESS: Specified value was saved.

restart the cmd terminal here to load changes. Print contents again.

C:\Users\Charity>echo %FANCYPANTS%
%FANCYPANTS%

the variable FANCYPANTS was deleted and no longer exists.

Outras dicas

SETX requires values with spaces to be quoted, and quotes within the value must be escaped as \".

Best also to use delayed expansion to protect against special characters during the batch parsing phase.

The following will not only toggle the values for new CMD sessions, it will also toggle the value for the remainder of the batch script run. An implicit ENDLOCAL at the end of the script will revert to the old values within the current session once the script ends. If needed, the script can be modified to preserve the new values past the ENDLOCAL barrier.

@echo on
setlocal enableDelayedExpansion
if "!PYTHONHOME:~-2!" == "24" (
  set "PYTHONHOME=C:\Python33"
  set "PATH=!PATH:Python24=Python33!"
) else (
  set "PYTHONHOME=C:\Python24"
  set "PATH=!PATH:Python33=Python24!"
)
setx PYTHONHOME "!home!"
setx PATH "!path:"=\"!"
pause

The SETX command is very reliant on the syntax of the command. The following example shows the basic syntax to use to set the path environment variable:

SETX PATH "%PATH%;Path to new thing added" /M

This will also add the new path to the system registry, but still won't add it for the current session. Re-launch the terminal for it to take affect.

I really like this way

here's the batch script:

@setlocal enableextensions enabledelayedexpansion
@echo off
set str1=%PYCURRENTPATHS%

if not "x%str1:python2=%" == "x%str1%"  (
    set PYCURRENTPATHS=%PY3PATHS%
) else (
    set PYCURRENTPATHS=%PY2PATHS%
) 
setx PYCURRENTPATHS %PYCURRENTPATHS%
set PATH=%PATH%
endlocal

we'll need 3 variables : (use "set" to set current terminal, use "setx" to set persistent variable)

set PY2PATHS=D:\ProgramData\Anaconda3\env\python2;D:\ProgramData\Anaconda3\env\python2\Scripts
set PY3PATHS=D:\ProgramData\Anaconda3;D:\ProgramData\Anaconda3\Scripts
setx PY2PATHS %PY2PATHS%
setx PY3PATHS %PY3PATHS%
setx PYCURRENTPATHS %PY2PATHS%

And add "%PYCURRENTPATHS%" to your path via GUI: Paths

This example uses anaconda and python2 setup from this example: conda create -n python2 python=2.7 anaconda

So for ultra painless Windows python, I can't recommend the following solution enough. Please give it a try I think you'll like it.

1) use Anaconda (start with python 3 for this example)... for the longest time I resisted and had better luck with manual installs/management of python, but due to network issues, I was forced to use Anaconda (didn't want to allow pip url on network :( )

2) install python2 from anaconda prompt: conda create -n python2 python=2.7 anaconda

3) create the script above to make your python installs available to command line (e.g. add to path)

You can add more virtual python environments, set them in the command line or enhance this script as well (eg PY2Tensor, Py3Scikit, etc.) or simply manage them via conda :)

Anaconda benefits from pre-compiled packages. No package mismatches, unstable releases, legacy issues or broken dependencies.

I still prefer Linux for development, but if you have to use windows and python, it's gotten better.

Note: set PATH=%PATH% causes re-eval of "PATH" + "%PYCURRENTPATHS%" in current session, a new session will reflect the change without this line

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top