Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activate python 3 environemnt in Anaconda with batch-file

Tags:

cmd

anaconda

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

After installation, I built a python 3 support environment with instruction in this page. My next task is to activate my python environment automatically with built-in batch file.

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

After observing the batch file I realized it seems to be capable of taking an input argument that is supposed to be the environment I would like to activate. So I copied the shortcut and modified it as

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

Then I double clicked on the new shortcut, it opened a new command window but...the designated environment did not activate!

@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 guess there may be something to do with this line

setlocal enableextensions enabledelayedexpansion

I tried to remove that line but kept trapped in the ERRORLEVEL 1 expression with message.

error: unable to enable command extensions

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

like image 991
Y. Chang Avatar asked Mar 05 '14 15:03

Y. Chang


People also ask

What is activate bat in Anaconda?

Runng activate. bat opens the Anaconda enviroment in Command Prompt. The next line is a standard command line that opens the folder in which my code is saved. The next line runs the code saved in that folder, and because of the first line this will use the Anaconda enviroment to run the python code.


1 Answers

I don't think you need a batch file. Assuming that Anaconda and CMD are on your path (which they should be), you can try this as an alternative (it is what I do):

cmd "/c activate py3k && ipython --pylab"
like image 118
BKay Avatar answered Sep 24 '22 00:09

BKay