63
votes

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.

9
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.Jeremy McGibbon
Thanks, worked, that was easy!SuperNano
Great! I'll move it to an answer so you can mark your question as answered.Jeremy McGibbon
<anaconda_dir> is unknown syntax. And anaconda is not always installed to the same directory.Sören

9 Answers

69
votes

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>
42
votes

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
16
votes

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!

16
votes

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"
5
votes

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"
1
votes

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.

0
votes

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 :).

0
votes

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
0
votes

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.