Skip to content

Instantly share code, notes, and snippets.

@maximlt
Last active September 14, 2024 12:08
Show Gist options
  • Save maximlt/531419545b039fa33f8845e5bc92edd6 to your computer and use it in GitHub Desktop.
Save maximlt/531419545b039fa33f8845e5bc92edd6 to your computer and use it in GitHub Desktop.
Run a Python script in a conda environment from a batch file
@echo OFF
rem How to run a Python script in a given conda environment from a batch file.
rem It doesn't require:
rem - conda to be in the PATH
rem - cmd.exe to be initialized with conda init
rem Define here the path to your conda installation
set CONDAPATH=C:\ProgramData\Miniconda3
rem Define here the name of the environment
set ENVNAME=someenv
rem The following command activates the base environment.
rem call C:\ProgramData\Miniconda3\Scripts\activate.bat C:\ProgramData\Miniconda3
if %ENVNAME%==base (set ENVPATH=%CONDAPATH%) else (set ENVPATH=%CONDAPATH%\envs\%ENVNAME%)
rem Activate the conda environment
rem Using call is required here, see: https://stackoverflow.com/questions/24678144/conda-environments-and-bat-files
call %CONDAPATH%\Scripts\activate.bat %ENVPATH%
rem Run a python script in that environment
python script.py
rem Deactivate the environment
call conda deactivate
rem If conda is directly available from the command line then the following code works.
rem call activate someenv
rem python script.py
rem conda deactivate
rem One could also use the conda run command
rem conda run -n someenv python script.py
@JonasH22
Copy link

JonasH22 commented May 4, 2021

Thanks for sharing!

@nirklaiman
Copy link

Thank you!

@1kastner
Copy link

Thanks! Somehow, from time to time I encounter the following issue:

CondaHTTPError: HTTP 000 CONNECTION FAILED for url https://conda.anaconda.org/conda-forge/win-64/repodata.json
Elapsed: -
An HTTP error occurred when trying to retrieve this URL.
HTTP errors are often intermittent, and a simple retry will get you on your way.
'https://conda.anaconda.org/conda-forge/win-64'
The creation process encountered an error, please check the output

Does anybody have an idea why?

@77377
Copy link

77377 commented Nov 9, 2021

@maximlt Love it!

works like a charm, is literally self explanatory and saves my day.
Thank you!

@1kastner
Copy link

Over some time I created this version which checks more directories and it goes both for Anaconda and Miniconda paths. Maybe it is helpfull for somebody else, too.

@vinissouza
Copy link

Thank you for sharing, you save my day!

@iremi
Copy link

iremi commented Mar 21, 2022

Thank you very much for the way to run a python script with the environment in 1 command. Didn't know about that and it saved me.
conda run -n someenv python script.py

@eltrujo
Copy link

eltrujo commented Mar 22, 2022

Thanks

@leshikus
Copy link

Thanks, that's helped

@sharpe5
Copy link

sharpe5 commented Oct 21, 2022

Works like a charm.

@azamkamranian
Copy link

works perfectly.

@AndFCar
Copy link

AndFCar commented Nov 28, 2022

Thank you for sharing!

@markwaters306
Copy link

This works perfectly! thanks

@Cohee1207
Copy link

Legendary

@AmitHenig
Copy link

Thank you!

@robinson0919
Copy link

Thank you, it works perfectly.

@robeisenrich
Copy link

Saved me so much trouble shooting! Thank you!

@mayobanexsantana
Copy link

Thanks for sharing!!! this save me lot of issues.

@chris-vecchio
Copy link

Thanks. The "call" part is what I was missing. Appreciate it!

@mdte123
Copy link

mdte123 commented Jul 19, 2024

Thanks for this, for some strange reason I had to change this line:
call %CONDAPATH%\Scripts\activate.bat %ENVPATH%

to:
call %CONDAPATH%\Scripts\activate.bat %ENVNAME%

@AniccsSora
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment