Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conda environments and .BAT files

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.

like image 756
Hans Avatar asked Jul 10 '14 13:07

Hans


People also ask

What is a .bat file?

A batch file is a script file that stores commands to be executed in a serial order. It helps automate routine tasks without requiring user input or intervention. Some common applications of batch files include loading programs, running multiple processes or performing repetitive actions in a sequence in the system.

What is a conda environment file?

A conda environment is a directory that contains a specific collection of conda packages that you have installed. For example, you may have one environment with NumPy 1.7 and its dependencies, and another environment with NumPy 1.6 for legacy testing.

What does activate bat do?

Schedule the activate. bat itself and it will automatically run your script after the virtual environment activated.


2 Answers

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

like image 68
Chris Burgoyne Avatar answered Sep 22 '22 22:09

Chris Burgoyne


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.

like image 21
BKay Avatar answered Sep 22 '22 22:09

BKay