8

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?

Nyxynyx
  • 61,411
  • 155
  • 482
  • 830
  • 5
    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""`. – Eryk Sun Mar 04 '17 at 23:44
  • @ErykSun where does this activate a specific conda environment? – Timothy L.J. Stewart Feb 17 '22 at 13:00

5 Answers5

3

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.

Jesse
  • 3,243
  • 1
  • 22
  • 29
  • Works, though the path may be different. Mine was: C:\Users\UserName\AppData\Local\Continuum\anaconda3\Scripts\activate.bat – MarLeo Jan 03 '19 at 14:37
3

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.

adruino-io
  • 31
  • 3
  • 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 Oct 07 '20 at 18:33
  • 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. – BoZenKhaa Oct 07 '20 at 18:42
  • Added *.lnk version of the same approach so that the use of a *.bat file can be circumvented. – adruino-io Dec 01 '20 at 16:13
2

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

2

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""
1

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
Topchi
  • 323
  • 2
  • 6