Forked from maximlt/run_python_script_in_conda_env.bat
Last active
October 13, 2024 09:33
-
-
Save 1kastner/61140275e1be52cdac58358ce5b1f08c to your computer and use it in GitHub Desktop.
Run a Python script in a conda environment from a batch file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ECHO OFF | |
SETLOCAL EnableDelayedExpansion | |
REM Insert your conda env here | |
SET CONDA_ENV=MY_DESIRED_CONDA_ENV | |
CALL :activate_conda_env | |
REM Insert your python script here | |
CALL python script.py | |
REM | |
REM The boilerplate code to activate conda | |
REM | |
:activate_conda_env | |
REM Reset errorlevel to 0 | |
VERIFY > nul | |
CALL conda info 1>nul 2>nul | |
IF NOT ERRORLEVEL 1 ( | |
where conda > .conda_path | |
SET /p CONDA_PATH= < .conda_path | |
SET CONDA_PATH=!CONDA_PATH:Library\bin=Scripts! | |
SET CONDA_PATH=!CONDA_PATH:conda.bat=! | |
SET CONDA_PATH=!CONDA_PATH:conda.exe=! | |
ECHO !CONDA_PATH! | |
ECHO A conda installation located in !CONDA_PATH! is available in your PATH variable and is thus used. | |
SET CONDASCRIPTS=!CONDA_PATH! | |
GOTO CONDA_FOUND | |
) | |
ECHO Conda is not available in your PATH. Guessing the location of the installation... | |
ECHO Checking in user-specific installations below %USERPROFILE% for which users usually have writing access. | |
SET CONDASCRIPTS=%USERPROFILE%\Anaconda3\Scripts\ | |
ECHO Checking for conda installation at !CONDASCRIPTS! | |
IF EXIST %CONDASCRIPTS% ( | |
GOTO CONDA_FOUND | |
) | |
SET CONDASCRIPTS=%USERPROFILE%\Miniconda3\Scripts\ | |
ECHO Checking for conda installation at !CONDASCRIPTS! | |
IF EXIST %CONDASCRIPTS% ( | |
GOTO CONDA_FOUND | |
) | |
ECHO Checking at computer-wide shared folders for which the user might not have writing access and thus the creation | |
ECHO might fail. It is usually preferred if a user-specific installation (i.e. in a folder below %USERPROFILE%) | |
ECHO would have been used instead. | |
SET CONDASCRIPTS=C:\ProgramData\Anaconda3\Scripts\ | |
ECHO Checking for conda installation at !CONDASCRIPTS! | |
IF EXIST %CONDASCRIPTS% ( | |
GOTO CONDA_FOUND | |
) | |
SET CONDASCRIPTS=C:\ProgramData\Miniconda3\Scripts\ | |
ECHO Checking for conda installation at !CONDASCRIPTS! | |
IF EXIST %CONDASCRIPTS% ( | |
GOTO CONDA_FOUND | |
) | |
SET CONDASCRIPTS=C:\Anaconda3\Scripts\ | |
ECHO Checking for conda installation at !CONDASCRIPTS! | |
IF EXIST %CONDASCRIPTS% ( | |
GOTO CONDA_FOUND | |
) | |
SET CONDASCRIPTS=C:\Miniconda3\Scripts\ | |
ECHO Checking for conda installation at !CONDASCRIPTS! | |
IF EXIST %CONDASCRIPTS% ( | |
GOTO CONDA_FOUND | |
) | |
SET CONDASCRIPTS=C:\Anaconda\Scripts\ | |
ECHO Checking for conda installation at !CONDASCRIPTS! | |
IF EXIST %CONDASCRIPTS% ( | |
GOTO CONDA_FOUND | |
) | |
SET CONDASCRIPTS=C:\Miniconda\Scripts\ | |
ECHO Checking for conda installation at !CONDASCRIPTS! | |
IF EXIST %CONDASCRIPTS% ( | |
GOTO CONDA_FOUND | |
) | |
REM We have checked all default paths, nothing else to do than to fail. | |
ECHO No conda installation was found. Please install either Anaconda or Miniconda first before invoking this script. | |
PAUSE | |
EXIT 2 | |
REM Once a conda installation is found, proceed here | |
:CONDA_FOUND | |
ECHO The scripts folder at !CONDASCRIPTS! has been detected as a valid conda installation. | |
ECHO The conda commands from this directory are used in the following. | |
CALL !CONDASCRIPTS!activate !CONDA_ENV! && ( | |
ECHO The environment '!CONDA_ENV!' has been activated successfully. | |
) || ( | |
ECHO The environment '!CONDA_ENV!' could not be activated. Please check the output for hints. | |
PAUSE | |
EXIT 2 | |
) | |
GOTO :EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am happy that it is of help for you!