简体   繁体   English

在 Anaconda 命令提示符中运行 Python 脚本的 Windows 快捷方式

[英]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.为了让用户更简单地启动 Python 脚本(从通过 Anaconda 命令提示符运行的 virtualenv 环境中),决定创建一个 Windows 快捷方式来实现这一点,只需双击即可。

The current link to open the Anaconda Command Prompt with a virtualenv loaded is打开加载了 virtualenv 的 Anaconda 命令提示符的当前链接是

%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?我们如何扩展这个快捷方式来运行 Python 脚本?

Based on Jesse's answer with additional details.基于 Jesse 的回答以及其他详细信息。 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如您所见,Anaconda 安装在

C:\ProgramData\Anaconda3

In order to find the location of your installation launch regular Conda command prompt and then type the following command:为了找到您的安装位置,启动常规 Conda 命令提示符,然后键入以下命令:

where python

it will return your conda's location of the python.它会返回你的 conda 的 python 位置。 You can also run this command after activating an env and the path will update accordingly.您也可以在激活 env 后运行此命令,路径将相应更新。 enter image description here在此处输入图片说明

For those who wish a "clean" cmd shell and based on Eryk Sun's answer.对于那些希望“干净”的 cmd shell 并基于 Eryk Sun 的回答的人。 Create a *.bat file with:创建一个 *.bat 文件:

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.这将提供一个 *.bat 文件,一旦 python 脚本完成,该文件就会关闭。 Paths are the defaults as found when installing Anaconda without further input.路径是在没有进一步输入的情况下安装 Anaconda 时找到的默认值。 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.第一个路径与星形菜单中“Anaconda Prompt”快捷方式中的路径相同,可以通过快捷方式的属性访问。

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.对于那些不想拥有 *.bat 脚本的人,可以通过右键单击所需的 *.py 文件的“新建 -> 快捷方式”来创建桌面(或任何地方)快捷方式 *.lnk。

Then right click on the *.lnk file and change the target to:然后右键单击 *.lnk 文件并将目标更改为:

%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.这应该为您提供一个直接的快捷方式来启动您的 python 脚本。 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.我安装了 Anaconda3。 Thanks to Eryk Sun et al.感谢 Eryk Sun 等人。

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:我的建议是创建一个批处理,例如,命名为 jupyterlab.bat 为:

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.然后为此批处理文件创建 Windows 快捷方式。

Creating a .bat file in Windows works for me.在 Windows 中创建 .bat 文件对我有用。 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%.您可以使用 Windows 变量 %userprofile%,而不是在 CALL 中对用户名进行硬编码。 You can type echo %userprofile% to see what %userprofile% points to:您可以输入echo %userprofile%来查看 %userprofile% 指向的内容:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Windows 10中从常规命令提示符处激活python anaconda并运行脚本 - How do I activate python anaconda and run script from regular command prompt in Windows 10 如何在命令提示符Windows 10中运行python脚本 - How to run python script in Command Prompt Windows 10 Python 模块请求在 anaconda 提示符下工作但不在 Windows 10 命令行中工作 - Python module requests working in anaconda prompt but not in windows 10 command line Python:无法使用Python脚本打开新的命令提示符会话并通过命令行(Windows)运行程序 - Python: Unable to open a new command prompt session and run a program through command line (windows) using Python script anaconda 提示符中的 Python 命令不起作用 - Python command in anaconda prompt not working 每当我在命令提示符下运行 python 时都会发出警告(我已经在我的 windows 8.1 操作系统上安装了 anaconda 最新版本) - Warning whenever i run python in my command prompt ( i have installed anaconda latest version on my windows 8.1 os) Python 在 Anaconda 提示符下工作,但不在命令提示符下 - Python is working in Anaconda prompt but not in command prompt 如何在 Windows 命令提示符下运行 python 文件? - How to run a python file in the windows command prompt? 使用python脚本在命令提示符下运行命令 - Run commands in command prompt using python script Python:从python脚本的Windows命令提示符中执行windows命令 - Python: Executing the windows command in windows command prompt from python script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM