Execute certain commands when opening Anaconda Prompt? - windows

When using Anaconda in Windows you get a shortcut to the "Anaconda Prompt" which is a cmd window that with the base-environment activated and probably all the conda commands loaded.
Is there a way to have some commands executed when starting it?
I'd like to have it change to a specific directory and activate a specific environment other than base.

The "Anaconda Prompt" is actually a link executing
%windir%\System32\cmd.exe "/K" C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3
The flag /k provided to cmd executes the subsequent commands, in this case activate.bat. To execute more commands we can just add them using the command separator &&, so we can use
%windir%\System32\cmd.exe "/K" C:\ProgramData\Anaconda3\Scripts\activate.bat
C:\ProgramData\Anaconda3 && cd some_directory && conda activate some_environment

Related

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 a batch file. Make the first command of your script:
call <anaconda_dir>/Scripts/activate.bat <anaconda_dir>
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
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"
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!
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"
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
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.
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 :).
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
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.
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

cannot run 'start' command from makefile in windows

I am using gnu make 3.8.1 (mingw32) in Windows 7 operating system.
I want to run 'start' command from makefile but it is giving some error. my syntax of command is-
start CMD /K http-server ./www -c-1
This command works when manually typed in cmd. It opens a new command prompt which runs the command 'http-server ./www -c-1', hosts the files present in www folder of current directory on port 8080 and shows the logs in the command prompt window it opened.
but in makefile, it doesn't seems to work. My makefile is-
hostx:
start CMD /K http-server ./www -c-1
There is more to the makefile but we only need to focus on this part.
when i type 'make hostx' in cmd it shows the following output.
I am posting my cmd output as complete image so this is more clear
any ideas how i could get it to work? also i don't know what does this error mean.
Only Windows 95/98/ME have a program called start.exe, in other versions of Windows it is an internal command inside cmd.exe.
To use the start command you need to invoke cmd.exe first:
CMD /C start CMD /K http-server ./www -c-1
If http-server is a console program or batch file then you don't need the CMD /K part because Windows will create a new console automatically. CMD /C start http-server ./www -c-1 should be enough.

Activating a Python virtual environment & changing directory in one shortcut (with cmd)

I'm trying to do the following:
Open a CMD prompt
Activate a virtual environment
Change the current directory to my project folder
In essence, I need to execute the following commands sequentially:
C:\Envs\djangorocks\Scripts\activate
cd "D:\GitHub\steelrumors"
I've found this link, but creating a shortcut as follows gives me nothing (just a plain CMD prompt in the currently active directory):
cmd \k "C:\Envs\djangorocks\Scripts\activate" & "cd "D:\GitHub\steelrumors""
After quite a while of searching I'm still doing it manually, any help is appreciated.
"creating a shortcut as follows gives me nothing (just a plain CMD prompt in the currently active directory):"
cmd \k "C:\Envs\djangorocks\Scripts\activate" & "cd "D:\GitHub\steelrumors""
Observations:
cmd \k should be cmd /k.
& should be && when using a shortcut.
You dont need all the " characters.
Try the following as the shortcut target:
cmd /k C:\Envs\djangorocks\Scripts\activate && cd D:\GitHub\steelrumors
Consider creating a batch file (e.g. c:\scripts\launchEnv.cmd) that does something like the following:
#echo off
C:\Envs\djangorocks\Scripts\activate
cd /d "D:\GitHub\steelrumors"
Then create a shortcut that invokes cmd /k c:\scripts\launchEnv.cmd .
Some notes:
the #echo off will prevent the commands from showing up in the cmd windows. If you do want to see the commands, then omit that line from your batch file
you'll need the /d param when changing directories to make sure you actually change and navigate there, independent of where the script is currently executing from.
As an extension to the great answer from #DavidPostill I've added an additional step to run a command from the newly created python env.
In my example below, I'm launching a new instance of the awesome data mining program, orange, from an anaconda env called orange. I've also cd'ed to the directory containing my orange data files. Note that I had to use the quotation marks "" to make it work.
C:\Windows\System32\cmd.exe /k "F: && cd \Dropbox\IT\Python\Orange && C:\Users\dreme\Anaconda3\Scripts\activate.bat orange && python -m Orange.canvas"

Run command on Command Prompt start

Is there a way to run any command (eg: cd D:\test) on Command Prompt start and to have this instance of Command Prompt as a different shortcut/instance.
You can create a shortcut that has a target of e.g.:
%comspec% /k ""C:\Full_Path_to\VsDevCmd.bat""
Where the .bat file is the command(s) that you want to run on startup. This will then leave you with an open command prompt after those commands have executed.
(In case it's not obvious, this is just shamelessly ripped from one of the Developer Command Prompt shortcuts that Visual Studio installs in the Start Menu)
%comspec% is a nice way of getting cmd to execute.
Cmd:
/k : Carries out the command specified by string and continues.

How to make a command prompt shortcut run a script on starting?

I can make a customized command prompt shortcut on my desktop to start in a particular directory and have some specified dimensions etc.
Now i have a script that sets some environment variables for the prompt and want to run it as soon as this shortcut is clicked.
How do i tell cmd or powershell to run this script in the current shell window and then show the shell ?
For cmd change the shortcut target to
%COMSPEC% /k C:\PATH\TO\your.cmd
and set the environment variables in your.cmd:
#echo off
set FOO=23
set BAR=42
Change the properties of the shortcut to include the file(scriptpath) and the noexit(to not close when done) parameter in the target-path. Example for target-path:
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -noexit -file "c:\users\graimer\desktop\testscript.ps1"

Resources