Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Shortcut to Run Python Script in Anaconda Command Prompt

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?

like image 486
Nyxynyx Avatar asked Mar 04 '17 22:03

Nyxynyx


3 Answers

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

like image 125
Igor Stavnitser Avatar answered Oct 08 '22 02:10

Igor Stavnitser


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""
like image 24
Phil Lowe UK Avatar answered Oct 08 '22 02:10

Phil Lowe UK


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.

like image 45
adruino-io Avatar answered Oct 08 '22 00:10

adruino-io