Conda environments and .BAT files

PythonAnacondaConda

Python Problem Overview


I am setting up calls to python (Anaconda distribution) via BAT files and the windows task scheduler.

I've now used environments for the first time and was trying to set a .bat file up as below:

activate [my_env]
python my_script.py
deactivate

Unfortunately it appears that the second command does not get executed.

Python Solutions


Solution 1 - Python

Use the 'call' command when activating/deactivating the environment.

call activate [my_env]
python my_script.py
call conda deactivate

See https://github.com/conda/conda/issues/794

Solution 2 - Python

Are you sure you need a batch file? I think this should work.

cmd "/c activate [my_env] && python my_script.py && deactivate"

When I made a simple file containing

print("Hello")

Which I called myprint.py and ran

cmd "/c activate anaconda33 && python myprint.py && deactivate"

This worked for me. You could also put this in a one line batch file.

Solution 3 - Python

All activate does is put the environment in the front of the PATH. You can just call the absolute path to the python in the environment you want, like C:\Anaconda\python my-script.py.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHansView Question on Stackoverflow
Solution 1 - PythonChris BurgoyneView Answer on Stackoverflow
Solution 2 - PythonBKayView Answer on Stackoverflow
Solution 3 - PythonasmeurerView Answer on Stackoverflow