"("此时无法在句法上进行处理“

时间:2017-07-27 05:52:39

标签: python batch-file syntax-error

我有一个安装批处理,它应该在所选文件夹中创建一个python venv,激活它然后用pip安装包。

不幸的是,它一直在"(" cant be processed syntactically at this point之后抛出echo #### Installing Packages。我写的批处理脚本并不是那么坚定,也无法弄清楚我在这里遇到了什么问题。有人可以帮我一把吗?

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

3 个答案:

答案 0 :(得分:0)

在没有正确测试的情况下在这里提出建议。我不应该这么大胆,但我不能在上面的评论中正确地提出这一点。

尝试改变这一点:

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"
)

为:

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"

答案 1 :(得分:0)

%这似乎已经开始了。

我删除了set "var=value"作业周围的很多引号字符,并将其添加到set /p var=prompt周围。

我还删除了var == value测试周围的空格,据我所知,批处理将这些空间识别为测试的一部分。

我不确定哪些具体的更改让它无法正常工作,但它现在在我的Windows 10系统上运行而没有安装pip / python。

@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

答案 2 :(得分:0)

如果您使用该块中更改的变量,则需要在代码块内部使用delayed expansion

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

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

if "%use_proxy%"=="y"更改为if "!use_proxy!"=="y"

(注意:if (...) else (...) else (...)中没有cmd。更改您的逻辑)