3

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!

  • 4
    Please read [this answer](https://stackoverflow.com/a/61135726/3074564). 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 Oct 16 '21 at 08: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 Oct 16 '21 at 19:26

1 Answers1

3

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.

  • From [microsoft docs](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/cmd): `/k` Carries out the command specified by string and continues. (`/c` closes terminal on script end) – heap1 Jun 22 '22 at 00:18