4

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 and py2exe

Any help would be appreciated. Thanks!

2
  • 4
    Please read this answer. Configure for shortcut property Target %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 file activate with file extension (.bat or .cmd) and py with full qualified file name instead of just file name.
    – Mofi
    Commented Oct 16, 2021 at 8:48
  • Your could ensure that the Start in: location of the shortcut reads as "%UserProfile%\Documents\PyProject", then define your Target: 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 shortcut Target: 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 of py.exe has been included within %PATH%.
    – Compo
    Commented Oct 16, 2021 at 19:26

1 Answer 1

4

Thanks to @Mofi for the link to comprehensive explanation of different approaches. The easiest solution that worked for me was the following:

(1) Create the following myscript.bat file and save it in the same directory as 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"

(2) Right-click on that myscript.bat file in the PyProject directory and "Create Shortcut". Drag that shortcut icon to the Desktop, and right-click to view "Properties." They should read:

Target: C:\Users\username\Documents\PyProject\myscript.bat

Start In: C:\Users\username\Documents\PyProject

Shortcut Key: None

Start In: Normal Window

The cmd window that is created after double-clicking on the icon remains open for the duration of the program.

1
  • 1
    From microsoft docs: /k Carries out the command specified by string and continues. (/c closes terminal on script end)
    – heap1
    Commented Jun 22, 2022 at 0:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.