How to make batch files run in anaconda prompt
Asked Answered
M

11

81

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.

Monotone answered 19/9, 2017 at 16:33 Comment(4)
I believe all the Anaconda prompt does is open CMD and run <anaconda_dir>/Scripts/activate.bat <anaconda_dir> as the first command, so you could try putting that at the top of your script.Flitch
Thanks, worked, that was easy!Monotone
Great! I'll move it to an answer so you can mark your question as answered.Flitch
<anaconda_dir> is unknown syntax. And anaconda is not always installed to the same directory.Ceratodus
F
85

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>
Flitch answered 19/9, 2017 at 17:23 Comment(5)
and maybe use call in your batch file, so it will not exit after the first command?Churchwell
i found my prompt would close even with call, I added cmd /k at the end of the fileDecompensation
The 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.Ceratodus
You 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.Flitch
if 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>Hiroko
B
49

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
Bashibazouk answered 9/5, 2018 at 18:36 Comment(2)
works really good, but how to use opened prompt after script running? Is there some command to not exit ?Carbuncle
yes just add cmd \k at the very end instead of pauseLaboured
A
28

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"
Arteriotomy answered 12/8, 2019 at 11:26 Comment(4)
THANKK YOU SO MUCHHHHLouis
This is the complete solution with a working example.Pase
@Arteriotomy 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?Bathrobe
@DavidDoria I don't know of a way to do that.Arteriotomy
Z
17

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!

Zoilazoilla answered 13/9, 2018 at 21:35 Comment(0)
M
6

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"
Moderato answered 1/3, 2020 at 5:52 Comment(0)
R
3

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

Where

%USERPROFILE% == C:\Users\name

%~dp ==  C:\Users\name\path\to\Project\RUN.bat

"%~dp0\main.py" == path to run targeted script
Recumbent answered 8/10, 2021 at 21:34 Comment(0)
D
1

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.

Doable answered 15/10, 2018 at 14:40 Comment(1)
install "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 calculationsDoable
I
1

Perform the following steps:

call "C:\Users\yourname\anaconda3\condabin\activate.bat"
cd "Your\program\path"
call activate your_env
python main.py
call conda deactivate

Refer to this article:

https://medium.com/@roddyjaques/how-to-run-anaconda-programs-with-a-bat-file-5f6dd7675508

Immoral answered 14/2, 2023 at 6:2 Comment(0)
X
0

As an alternative to the above solution if you are having windows os. You can use git bash

  1. 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 
    
  2. Run your script => . script.sh

It would be a good alternative too.

Check this and this for more details. Hope it will help someone :).

Xylo answered 16/5, 2020 at 5:44 Comment(0)
E
0

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
Envision answered 19/4, 2021 at 23:20 Comment(0)
S
0

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.

Sudanic answered 30/4, 2021 at 10:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.