How to make batch files run in anaconda prompt
Solution 1
I believe all the Anaconda prompt does is open CMD and run a batch file. Make the first command of your script:
call <anaconda_dir>/Scripts/activate.bat <anaconda_dir>
Solution 2
Extending Jeremy's answer:
You do need to use call
for the "activate.bat" script as well as any subsequent Anaconda/Python-related commands. Otherwise the prompt will immediately quit after running the commands, even if you use a pause
statement. Please see below example:
set root=C:\Users\john.doe\AppData\Local\Continuum\anaconda3
call %root%\Scripts\activate.bat %root%
call conda list pandas
pause
Solution 3
Thanks to this thread I solved my challenge to get a windows batch file to open the Ananconda Prompt and then run some python code.
Here is the batch file:
@echo on
call C:\ProgramData\Anaconda3\Scripts\activate.bat
C:\ProgramData\Anaconda3\python.exe "D:\Documents\PythonCode\TFLAPI\V1.py"
Solution 4
Add
call "<anaconda_dir>\Scripts\activate.bat"
to the start of your script (it doesn't actually require an argument, and it activates the base
environment by default).
Note that after this line, you can make use of the CONDA_
envvars!
Solution 5
For Windows, use the following script in your batch file to execute a Python script. Simply change your personal file paths like this:
cmd /c C:\ProgramData\Anaconda3\condabin\conda.bat run "C:\ProgramData\Anaconda3\python.exe" "C:\Users\User Name\Path to your Python File\Python File.py"
Admin
Updated on July 05, 2022Comments
-
Admin almost 2 years
After installing anaconda3 in windows, I can run python commands from the anaconda prompt, but not from the windows command prompt. I would like to make a desktop shortcut to activate my environment and run spyder from it. Previously, I would do this with a
.bat
file, but now that I cannot run python commands fromcmd.exe
this doesn't work.Is there an alternative way of running batch files for the anaconda prompt? I know that I could just modify my
PATH
to getcmd.exe
to run python commands, but I'd like to avoid this if possible. -
djvg over 6 yearsand maybe use
call
in your batch file, so it will not exit after the first command? -
Ryan Huebert almost 6 yearsi found my prompt would close even with
call
, I addedcmd /k
at the end of the file -
Bimo over 5 yearsinstall "active state python 3.5" instead of anaconda.. make sure its in your path then type: "PS C:\user\dfsdsdf> jupyter notebook" … you can get used to using notebooks instead of qtconsole for small calculations
-
Soren about 5 yearsThe solution is a bit unsatisfying, because the anaconda directory is different on each system. It would be better to have a shebang like in Linux that tells the script not to be called with cmd.exe but with anaconda prompt.
-
Jeremy McGibbon about 5 yearsYou do have to manually insert your anaconda directory where I've indicated <anaconda_dir>, unless there's an environment variable I'm missing. The anaconda prompt is actually just cmd.exe, and "it would be better if Windows were more like Linux" isn't an option, so a shebang doesn't quite make sense.
-
Ioannis K. Moutsatsos almost 5 yearsif you have created several conda environments, then you can activate a specific one by supplying it as a parameter to the activate.bat file. So the command would become call <anaconda_dir>/Scripts/activate.bat <env_name>
-
adinda aulia over 3 yearsTHANKK YOU SO MUCHHHH
-
Soumya Boral over 3 yearsThis is the complete solution with a working example.
-
Demetry Pascal over 2 yearsworks really good, but how to use opened prompt after script running? Is there some command to not exit ?
-
David Doria over 2 years@JedB when I double click a .bat file like this, I get a terminal window along with my GUI (using PySimpleGUI). Is there something to be done in the .bat that can hide the terminal window?
-
JedB over 2 years@DavidDoria I don't know of a way to do that.
-
Kream almost 2 yearsyes just add cmd \k at the very end instead of pause