How to make batch files run in anaconda prompt

84,395

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"
Share:
84,395
Admin
Author by

Admin

Updated on July 05, 2022

Comments

  • Admin
    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 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.

  • djvg
    djvg over 6 years
    and maybe use call in your batch file, so it will not exit after the first command?
  • Ryan Huebert
    Ryan Huebert almost 6 years
    i found my prompt would close even with call, I added cmd /k at the end of the file
  • Bimo
    Bimo over 5 years
    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 calculations
  • Soren
    Soren about 5 years
    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.
  • Jeremy McGibbon
    Jeremy McGibbon about 5 years
    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.
  • Ioannis K. Moutsatsos
    Ioannis K. Moutsatsos almost 5 years
    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>
  • adinda aulia
    adinda aulia over 3 years
    THANKK YOU SO MUCHHHH
  • Soumya Boral
    Soumya Boral over 3 years
    This is the complete solution with a working example.
  • Demetry Pascal
    Demetry Pascal over 2 years
    works really good, but how to use opened prompt after script running? Is there some command to not exit ?
  • David Doria
    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
    JedB over 2 years
    @DavidDoria I don't know of a way to do that.
  • Kream
    Kream almost 2 years
    yes just add cmd \k at the very end instead of pause