Windows: run python command from clickable icon

30,727

Solution 1

The simplest solution will be creating batch file containing command like:

c:\python27\python.exe c:\somescript.py

With this solution you will have to have installed python interpreter. If you need more portable solution you can try for e.g. py2exe and bundle python scripts into executable file, able to run without requiring a Python installation.

Solution 2

This tutorial shows how to create a batch file that runs a python script.

Note that many of the other answers are out of date - py2exe is out of support past python 3.4. More info here.

Solution 3

This comes straight from Python Docs (https://docs.python.org/3.3/using/windows.html):

3.3.5. Executing scripts without the Python launcher

Without the Python launcher installed, Python scripts (files with the extension .py) will be executed by python.exe by default. This executable opens a terminal, which stays open even if the program uses a GUI. If you do not want this to happen, use the extension .pyw which will cause the script to be executed by pythonw.exe by default (both executables are located in the top-level of your Python installation directory). This suppresses the terminal window on startup.

You can also make all .py scripts execute with pythonw.exe, setting this through the usual facilities, for example (might require administrative rights):

Launch a command prompt.

Associate the correct file group with .py scripts:

assoc .py=Python.File

Redirect all Python files to the new executable:

ftype Python.File=C:\Path\to\pythonw.exe "%1" %*

Solution 4

As an alternative to py2exe you could use the pyinstaller package with the onefile flag. This is a solution which works for python 3.x.

  1. install pyinstaller via pip

  2. Package your file to a single exe with the onefile flag

pyinstaller --onefile your_file.py

Solution 5

A better way (in my opinion):

Create a shortcut:

Set the target to %systemroot%\System32\cmd.exe /c "python C:\Users\MyUsername\Documents\MyScript.py"

Start In: C:\Users\MyUsername\Documents\

Obviously change the path to the location of your script. May need to add escaped quotes if there is a space in it.

Share:
30,727
Admin
Author by

Admin

Updated on January 16, 2022

Comments

  • Admin
    Admin over 2 years

    I have a python script I run using Cygwin and I'd like to create a clickable icon on the windows desktop that could run this script without opening Cygwin and entering in the commands by hand. how can I do this?

  • Jason Etheridge
    Jason Etheridge over 2 years
    Using pythonw.exe in the Windows shortcut worked perfectly for me, allowing the script to be run without the CMD window. Perfect!