ChatGPT解决这个技术问题 Extra ChatGPT

How to make batch files run in anaconda prompt

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.

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

f
flywire

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>

and maybe use call in your batch file, so it will not exit after the first command?
i found my prompt would close even with call, I added cmd /k at the end of the file
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.
You do have to manually insert your anaconda directory where I've indicated , 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.
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 /Scripts/activate.bat
N
N4v

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

works really good, but how to use opened prompt after script running? Is there some command to not exit ?
yes just add cmd \k at the very end instead of pause
J
JedB

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"

THANKK YOU SO MUCHHHH
This is the complete solution with a working example.
@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?
@DavidDoria I don't know of a way to do that.
i
ivan_pozdeev

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!


J
Jose R

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"

B
Bimo

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.


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
D
DINA TAKLIT

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


D
Dharman

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

g
geodata

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.


A
Aaron Roethe

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