I am trying to create a clickable desktop shortcut for a Windows 10 system that simply executes the following commands that I currently need to repeatedly type in a 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
I've tried a few of the recommended solutions here, here, and here (including a few others not linked) but all fail by immediately closing the console/command window that is supposed to remain open and print out lines of text from the program.
Some of my attempts have included:
- creating a
.bat
file that I saved in...\PyProject\
with a shortcut on the desktop.
@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"
- using
pyinstaller
andpy2exe
Any help would be appreciated. Thanks!
%SystemRoot%\System32\cmd.exe /D /S /K "call venv\scripts\activate & py -3 myscript.py"
and for shortcut property Start in%UserProfile%\Documents\PyProject
. It would be best to specify the batch fileactivate
with file extension (.bat
or.cmd
) andpy
with full qualified file name instead of just file name.Start in:
location of the shortcut reads as"%UserProfile%\Documents\PyProject"
, then define yourTarget:
as%SystemRoot%\System32\cmd.exe /D /K "Call .venv\Scripts\activate.bat & py.exe -3 myscript.py"
, as already advised. Alternatively, to match your current idea, (although not my recommendation), just define a shortcutTarget:
as%SystemRoot%\System32\cmd.exe /D /K "CD /D "%UserProfile%\Documents\PyProject" && Call .venv\Scripts\activate.bat & py.exe -3 myscript.py"
. Both assume that the location ofpy.exe
has been included within%PATH%
.