Activate python 3 environemnt in Anaconda with batch file

Recently installed Anaconda (1.9) for my python project on win7

After installation, I built a python 3 support environment with the instructions in this page . The next task is to automatically activate my python environment with an embedded batch file.

I used the command in the [Anaconda Command Prompt] command line shortcut that I found in my start menu. It runs a batch file named [ anaconda.bat ]

After observing the batch file, I realized that it is capable of accepting an input argument, which should be the environment that I would like to activate. So I copied the shortcut and changed it as

C:\Windows\System32\cmd.exe /k "C:\Anaconda\Scripts\anaconda.bat py3k"

Then I double-clicked the new shortcut, it opened a new command window, but ... the specified environment was not activated!

@echo off

rem +===========================================================================
rem | Initialisation
rem +===========================================================================
verify bogus-argument 2>nul
setlocal enableextensions enabledelayedexpansion
if ERRORLEVEL 1 (
    echo error: unable to enable command extensions
    goto :eof
)

for %%i in ("%~dp0..\envs") do (
    set ANACONDA_ENVS=%%~fi
)

if not "%1" == "" (
    if not exist "%ANACONDA_ENVS%\%1\python.exe" (
        echo No environment named "%1" exists in %ANACONDA_ENVS%
        goto :eof
    )
    set ANACONDA_ENV_NAME=%1
    set ANACONDA=%ANACONDA_ENVS%\%1
    title Anaconda (%ANACONDA_ENV_NAME%^)
) else (
    set ANACONDA_ENV_NAME=
    for %%i in ("%~dp0..") do (
        set ANACONDA=%%~fi
    )
    title Anaconda
)

set ANACONDA_SCRIPTS=%ANACONDA%\Scripts

for %%i in ("python.exe") do (
    for %%j in ("%ANACONDA%\python.exe") do (
        if not "%%~f$PATH:i" == "%%~f$PATH:j" (
            set ANACONDA_OLD_PATH="%PATH%"
            set PATH=%ANACONDA%;%ANACONDA_SCRIPTS%;%PATH%;
            echo Added %ANACONDA% and %ANACONDA_SCRIPTS% to PATH.
        )
    )
)

if not "%ANACONDA_ENV_NAME%" == "" (
    echo Activating environment %ANACONDA_ENV_NAME%...
    set PROMPT=[%ANACONDA_ENV_NAME%] $P$G
)

I have very little experience with bat language, but I think there might be something to do with this line

setlocal enableextensions enabledelayedexpansion

I tried to delete this line, but remained trapped in the ERRORLEVEL 1 expression with the message.

error: unable to enable command extensions

Can anyone suggest what should I do to make this bat file work correctly?

+4
source share
1 answer

I do not think you need a batch file. Assuming Anaconda and CMD are in your way (what they should be), you can try this as an alternative (this is what I am doing):

cmd "/c activate py3k && ipython --pylab"
+4
source

Source: https://habr.com/ru/post/1530273/


All Articles