How to make batch files run in anaconda prompt
WindowsBatch FileCmdAnacondaWindows Problem Overview
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 from cmd.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 get cmd.exe
to run python commands, but I'd like to avoid this if possible.
Windows Solutions
Solution 1 - Windows
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 - Windows
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 - Windows
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 - Windows
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 - Windows
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"
Solution 6 - Windows
Powershell Version:
$qtconsole="C:\Users\<YourUserName>\.anaconda\navigator\scripts\qtconsole.bat"
start-process $qtconsole -WindowStyle Hidden
Note: this script will only start one instance of the qtconsole at a time due to DLL limitations of Linux QT GUI library only supporting one instance of the same exe running at the same time. That's probably why they use "Anaconda Navigator" to launch the QtConsole programs to get around this restriction.
Solution 7 - Windows
As an alternative to the above solution if you are having windows os. You can use git bash
-
you need to add the path to
conda.sh
to you.bash_profile
or whatever it is named to be able to run conda commands. here is an example:
echo ". C:/Users/user/Anaconda3/etc/profile.d/conda.sh" >> ~/.bash_profile
- Run your script =>
. script.sh
It would be a good alternative too.
Check this and this for more details. Hope it will help someone :).
Solution 8 - Windows
Extending @N4v answer as this is the only approach that worked for me in calling the Python script. Python version is 3.7
set root=C:\Users\xxxx\Anaconda3 #Anaconda default folder on my computer
set env1=C:\Users\xxxx\Anaconda3\envs\py37 #My Python environment folder. The name I gave is py37. Can be specific to yours
call %root%\Scripts\activate.bat %env1% #Call command to activate py37 environment.
python "C:\Path to the folder with python file\Pythonfile.py" #Run the file of interest after running python specific to the called environment. Replace this with your files path and name.
pause
Solution 9 - Windows
Based on the answer of @ivan_pozdeev I found the following to be the cleanest solution for me:
@ECHO OFF
CALL "<anaconda_dir>\Scripts\activate.bat" [<conda_environment_if_not_base>]
%CONDA_PYTHON_EXE% "<full_path_to_your_python_script>" %1 %2 %3 %4 %5 %6 %7 %8 %9
conda deactivate
So for example:
@ECHO OFF
CALL "E:\ProgramData\Anaconda3\Scripts\activate.bat"
%CONDA_PYTHON_EXE% "C:\Users\<user>\Documents\Python3\my_project\src\my_script.py" %1 %2 %3 %4 %5 %6 %7 %8 %9
conda deactivate
By including conda deactivate
at the end of the batch file you leave the commandline in the state you started. And if you need a different conda environment you can specify this after activate.bat
.
Solution 10 - Windows
The easiest way to execute anaconda scripts through .bat
set venv=name_of_virtual_env
call %USERPROFILE%\Anaconda3\Scripts\activate %USERPROFILE%\Anaconda3
call activate %venv%
:: Change directory to the relative path that's needed for script
cd %~dp0
:: Run script at this location
call %USERPROFILE%/Anaconda3/envs/%venv%/python.exe "%~dp0\main.py"
PAUSE
%USERPROFILE% == C:\Users\name
%~dp == C:\Users\name\path\to\Project\RUN.bat
"%~dp0\main.py" == path to run targeted script