Reputation: 999
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.
Upvotes: 83
Views: 134237
Reputation: 29
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
Upvotes: 3
Reputation: 623
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
Upvotes: 1
Reputation: 140
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
.
Upvotes: 0
Reputation: 445
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
Upvotes: 0
Reputation: 552
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"
Upvotes: 6
Reputation: 8388
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 :).
Upvotes: 0
Reputation: 493
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"
Upvotes: 28
Reputation: 3775
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>
Upvotes: 86
Reputation: 35986
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!
Upvotes: 17
Reputation: 6587
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.
Upvotes: 1
Reputation: 825
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
Upvotes: 49