1

I got a install batch, that should create a python venv in a chosen folder, activate it and then install packages with pip.

Unfortunately it keeps throwing "(" cant be processed syntactically at this point after the echo #### Installing Packages. I'm not that firm with writing batch script and could not figure out what I got wrong here. Could someone please give me a hand here?

echo ##### Installation Python AppApp

setlocal

if "%~1"=="" (
    echo No params given
    set "folder_chosen=false"
) else (
    set "folder=%1"
    set "folder_chosen=true"
)

setlocal enabledelayedexpansion

if "%folder_chosen%"=="false" (
    call :CHOOSE_FOLDER
)

echo You chose !folder!

echo ##### Creating virtual environment
python -m venv !folder!\AppApp

echo ##### Activating virtual environment
call !folder!\AppApp\Scripts\activate

echo ##### Installing packages


if not "%HTTP_PROXY%"=="" (
    pip install -r requirements.txt --proxy=%HTTP_PROXY%
) else (
    echo Proxy not set
    set /p "use_proxy=No proxy for pip configured, do you want to use one? [y]/n"

    if %use_proxy%=="y" (

        set /p "proxy_user=Enter username for proxy! [%USERNAME%]"
        if "%proxy_user%"=="" set "proxy_user=%USERNAME%"
        set /p "proxy_host=Enter proxy host! [default.proxy.de]"
        if "%proxy_host%"=="" set "proxy_host=default.proxy.de"
        set /p "proxy_port=Enter proxy port! [8080]"
        if "%proxy_port"=="" set "proxy_port=8080"

        pip install -r requirements.txt --proxy %proxy_user%@%proxy_host%:%proxy_port%
    ) else (
        pip install -r requirements.txt
    )
)

IF %ERRORLEVEL% NEQ 0 GOTO PIPError

echo ##### Copying runnables
cp initPythonCommands.py !folder!\AppApp
cp startPythonCmd.bat !folder!\AppApp

echo ##### Succesfully installed Python AppApp

endlocal
pause

exit /b 0

:PIPError
ECHO ##### Could not fetch package from pip, you might want to check your proxy settings
@call deactivate
ECHO ##### Removing the created Venv
rm -r !folder!\AppApp
pause
exit /b 1

:CHOOSE_FOLDER
set "psCommand="(new-object -COM 'Shell.Application')^
.BrowseForFolder(0,'Please choose a folder.',0,0).self.path""
for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "folder=%%I"
exit /b
Igl3
  • 4,900
  • 5
  • 35
  • 69
  • I had something similar, it's matching the parenthesis in your "set psCommand..." statement, not the closing parenthesis that you would expect. Try moving your set psCommand and for /f statements to a "call" subroutine instead. – Jeff Breadner Jul 27 '17 at 05:55
  • I tried this before, but this gives me the same exception. If I use the "set psCommand" without "if" it works correctly. – Igl3 Jul 27 '17 at 06:30
  • You could move the set psCommand before the if statement, and only execute it inside? – Jeff Breadner Jul 27 '17 at 06:32
  • I got the folder chooser to work now by removing 2 " around = in set "folder_chosen"="false" and so on. Now I got the same exception but in the "if" part with the proxy. – Igl3 Jul 27 '17 at 06:40
  • 1
    not necessarily related to your errormessage, but you really should use [delayed expansion](https://stackoverflow.com/a/30284028/2152082) inside your `if` code blocks. – Stephan Jul 27 '17 at 06:55
  • I changed it, but is unrelated to the exception. – Igl3 Jul 27 '17 at 06:59
  • 2
    I would have sworn, it is: You use `if %use_proxy%=="y" (` and without delayed expansion `%use_proxy%` is empty, leading the line to be parsed as `if =="y" (`, which exactly gives the error you show (due to a syntax error)... – Stephan Jul 27 '17 at 07:05
  • There's also a syntax error: if `"%proxy_port"==""` is missing a trailing `%` sign – Jeff Breadner Jul 27 '17 at 07:08
  • You were right Stephan, I got to use delayed expansion in the else block where use_proxy get set. Add it as answer and I'll accept. – Igl3 Jul 27 '17 at 07:15

3 Answers3

0

Proposing something here without having tested it properly. I shouldn't be so bold, but I can't really propose this properly in the comments we have going above.

Try to change this:

if not %folder_chosen%=="true" (
    set "psCommand="(new-object -COM 'Shell.Application')^
    .BrowseForFolder(0,'Where to install the application',0,0).self.path""

    for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "folder=%%I"
)

to:

set psCommand="(new-object -COM 'Shell.Application')^
    .BrowseForFolder(0,'Where to install the application',0,0).self.path"
if not %folder_chosen%=="true" 
    for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "folder=%%I"
Jeff Breadner
  • 1,366
  • 9
  • 19
  • As I said in my comment above, the folder chooser works now but I got the same exception in the Proxy part. – Igl3 Jul 27 '17 at 06:49
0

%This seems to run.

I removed a lot of quote characters around the set "var=value" assignments, and added them around the set /p var=prompt.

I also removed the spaces around the var == value tests, from what I understand batch recognizes these spaces as part of the test.

I'm not sure what specific changes shook it loose to get it working, but it is working now on my Windows 10 system without pip / python installed.

@echo off

echo ##### Installation Python

setlocal

if "%1"=="" (
    set folder_chosen="false"
) else (
    set folder=%1
    set folder_chosen="true"
)

set psCommand="(new-object -COM 'Shell.Application').BrowseForFolder(0,'Where to install the application',0,0).self.path"
if not %folder_chosen%=="true" for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set folder=%%I


setlocal enabledelayedexpansion
echo You chose !folder!

echo ##### Creating virtual environment
python -m venv !folder!\AppApp

echo ##### Activating virtual environment
call !folder!\AppApp\Scripts\activate

echo ##### Installing packages


if not "%HTTP_PROXY%"=="" (
    pip install -r requirements.txt --proxy=%HTTP_PROXY%
) else (
    set /p use_proxy="No proxy for pip configured, do you want to use one? [y]/n"

    if "%use_proxy%"=="y" (

        set /p proxy_user="Enter username for proxy! [%USERNAME%]"
        if "%proxy_user%"=="" set proxy_user=%USERNAME%
        set /p proxy_host="Enter proxy host! [default.proxy.de]"
        if "%proxy_host%"=="" set proxy_host=default.proxy.de
        set /p proxy_port="Enter proxy port! [8080]"
        if "%proxy_port%"=="" set proxy_port=8080

        pip install a-r requirements.txt --proxy %proxy_user%@%proxy_host%:%proxy_port%
    ) else (
        pip install -r requirements.txt
    )
)
echo checkpoint 1
IF %ERRORLEVEL% NEQ 0 GOTO PIPError

echo ##### Copying runnables
cp initPythonCommands.py !folder!\AppApp
cp startPythonCmd.bat !folder!\AppApp

echo ##### Succesfully installed Python AppApp

endlocal
pause

exit /b 0

:PIPError
ECHO ##### Could not fetch package from pip, you might want to check your proxy settings
@call deactivate
ECHO ##### Removing the created Venv
rm -r !folder!\AppApp
pauseac
exit /b 1
Jeff Breadner
  • 1,366
  • 9
  • 19
0

You need to use delayed expansion inside of code blocks, if you use a variable changed in that block.

...
) else (
    setlocal enabledelayedexpansion
    set /p use_proxy="No proxy for pip configured, do you want to use one? [y]/n"

    if "%use_proxy%"=="y" (
    ...
)
...

Change if "%use_proxy%"=="y" to if "!use_proxy!"=="y"

(Note: there is no if (...) else (...) else (...) in cmd. Change your logic)

Igl3
  • 4,900
  • 5
  • 35
  • 69
Stephan
  • 53,940
  • 10
  • 58
  • 91