Windows Shortcut to Run Python Script in Anaconda Command Prompt

12,499

Solution 1

For those who wish a "clean" cmd shell and based on Eryk Sun's answer. Create a *.bat file with:

echo off
cls
"%windir%\System32\cmd.exe" /k ""C:\ProgramData\Anaconda3\Scripts\activate.bat" "C:\ProgramData\Anaconda3" && python "C:\Users\...path_to_your_file\...\your_script.py" && exit"

This will provide a *.bat file that closes once the python script is done. Paths are the defaults as found when installing Anaconda without further input. The first path is identical to the one found in the "Anaconda Prompt" shortcut in the star menu and can be accessed via the shortcut's properties.

For those who do not wish to have a *.bat script it is possible to create a desktop (or wherever) shortcut *.lnk by right-clicking "New -> Shortcut" to the desired *.py file.

Then right click on the *.lnk file and change the target to:

%windir%\System32\cmd.exe /k ""C:\ProgramData\Anaconda3\Scripts\activate.bat" "C:\ProgramData\Anaconda3" && python "C:\Users\...path_to_your_file\...\your_script.py" && exit"

this should provide you with a direct shortcut to launch your python script. Please note the """, that enclose the subsequent commands.

Solution 2

Create a batch file works for me, for example, named jupyterlab.bat as:

echo off

CALL  %userprofile%\Anaconda3\Scripts\activate.bat %userprofile%\Anaconda3\envs\YourEnv
jupyter lab

echo on 

And then create Windows shortcuts for this batch file.

Solution 3

Based on Jesse's answer with additional details. The script for my installation was as follows:

CALL  C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3\envs\keras
cd C:\Users\boo\Dropbox\WSES
python pt1231C3F.py ABC 548 860

As you can see for me Anaconda was installed in

C:\ProgramData\Anaconda3

In order to find the location of your installation launch regular Conda command prompt and then type the following command:

where python

it will return your conda's location of the python. You can also run this command after activating an env and the path will update accordingly. enter image description here

Solution 4

Once the quotes were inserted correctly this worked perfectly for me. I had been looking all over for a good solution to this problem. I have Anaconda3 installed. Thanks to Eryk Sun et al.

My batch file as follows: -

echo off
cls
"%windir%\System32\cmd.exe" /k ""C:\ProgramData\Anaconda3\Scripts\activate.bat" "C:\ProgramData\Anaconda3" && python "T:\Arduino\NodeMCU (ESP8266) Projects\Audio LED Strip\visualization.py""

Solution 5

Creating a .bat file in Windows works for me. I can then schedule it using task scheduler, or just run it anytime from the command prompt.

Instead of hard-coding the username in the CALL, you could use the Windows variable %userprofile%. You can type echo %userprofile% to see what %userprofile% points to:

call %userprofile%\Anaconda3\Scripts\activate.bat C:\Users\user\Anaconda3 
cd %userprofile% 

rem Run the below Python scripts

python script_1.py
python script_2.py
pause
Share:
12,499
Nyxynyx
Author by

Nyxynyx

Hello :) I have no formal education in programming :( And I need your help! :D These days its web development: Node.js Meteor.js Python PHP Laravel Javascript / jQuery d3.js MySQL PostgreSQL MongoDB PostGIS

Updated on June 04, 2022

Comments

  • Nyxynyx
    Nyxynyx almost 2 years

    In order to make it simpler for a user to launch a Python script (from within a virtualenv environment running through an Anaconda Command Prompt), it is decided to create a Windows shortcut to achieve this in one double click.

    The current link to open the Anaconda Command Prompt with a virtualenv loaded is

    %windir%\system32\cmd.exe "/K" C:\Users\x\Anaconda2\Scripts\activate.bat C:\Users\x\Anaconda2\envs\myEnv
    

    How can we extend this shortcut to also run a Python script?

    • Eryk Sun
      Eryk Sun about 7 years
      Wrap the command-line to run -- the part after /k -- in quotes and within it use && to sequentially execute a command if the previous command succeeded, e.g. "%ComSpec%" /k ""C:\Users\x\Anaconda2\Scripts\activate.bat" "C:\Users\x\Anaconda2\envs\myEnv" && python "C:\Users\x\Anaconda2\Scripts\script.py"".
    • Timothy L.J. Stewart
      Timothy L.J. Stewart about 2 years
      @ErykSun where does this activate a specific conda environment?
  • Reimar
    Reimar over 5 years
    Works, though the path may be different. Mine was: C:\Users\UserName\AppData\Local\Continuum\anaconda3\Scripts\‌​activate.bat
  • BoZenKhaa
    BoZenKhaa over 3 years
    This does not actually work for me, the commands after "Anaconda3" are not run in the Anaconda prompt, instead, they run in the initial cmd.exe. Any idea what's going on?
  • BoZenKhaa
    BoZenKhaa over 3 years
    Ok, the answer is in the comment by Eryk Sun, the whole sequence of characters after \k has to be wrapped in quotes "": cmd.exe \k " "...\anaconda3" && python foo.py ", I missed that at first sight.
  • adruino-io
    adruino-io over 3 years
    Added *.lnk version of the same approach so that the use of a *.bat file can be circumvented.