【发布时间】:2021-12-04 03:14:27
【问题描述】:
我正在尝试为 Windows 10 系统创建一个可单击的桌面快捷方式,该快捷方式仅执行以下我目前需要在 Windows Powershell 中重复键入的命令:
PS C:\Users\user> cd C:\Users\username\Documents\PyProject
PS C:\Users\username\Documents\PyProject> .venv\scripts\activate
(.venv) C:\Users\username\Documents\PyProject> py -3 .\myscript.py
我尝试了一些推荐的解决方案 here、here 和 here(包括其他一些未链接的解决方案),但都因立即关闭应该保持打开的控制台/命令窗口而失败并从程序中打印出文本行。
我的一些尝试包括:
- 创建一个
.bat文件,并在桌面上使用快捷方式保存在...\PyProject\中。
@echo off
cmd /k "cd /d C:\Users\username\Documents\PyProject\.venv\Scripts & .\activate & cd /d C:\Users\username\Documents\PyProject & py -3 myscript.py"
- 使用
pyinstaller和py2exe
任何帮助将不胜感激。谢谢!
【问题讨论】:
-
请阅读this answer。配置快捷方式属性 Target
%SystemRoot%\System32\cmd.exe /D /S /K "call venv\scripts\activate & py -3 myscript.py"和快捷方式属性 Start in%UserProfile%\Documents\PyProject。最好使用文件扩展名(.bat或.cmd)指定批处理文件activate,并使用完整的限定文件名而不是文件名指定py。 -
您可以确保快捷方式的
Start in:位置读取为"%UserProfile%\Documents\PyProject",然后将您的Target:定义为%SystemRoot%\System32\cmd.exe /D /K "Call .venv\Scripts\activate.bat & py.exe -3 myscript.py",正如已经建议的那样。或者,为了匹配您当前的想法,(尽管不是我的建议),只需将快捷方式Target:定义为%SystemRoot%\System32\cmd.exe /D /K "CD /D "%UserProfile%\Documents\PyProject" && Call .venv\Scripts\activate.bat & py.exe -3 myscript.py"。 两者都假定py.exe的位置已包含在%PATH%中。
标签: python python-3.x windows powershell batch-file