简体   繁体   中英

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?

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

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.

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

My suggest is create a batch, for example, named jupyterlab.bat as:

echo off

CALL  C:\Users\YourName\Anaconda3\Scripts\activate.bat C:\Users\YourName\Anaconda3\envs\YourEnv
jupyter lab

echo on 

And then create Windows shortcuts for this batch file.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM