Windows: run python command from clickable icon

前端 未结 4 732
北荒
北荒 2020-12-20 19:25

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 t

相关标签:
4条回答
  • 2020-12-20 19:37

    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.

    0 讨论(0)
  • 2020-12-20 19:47

    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
    
    
    0 讨论(0)
  • 2020-12-20 19:49

    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.

    0 讨论(0)
  • 2020-12-20 19:57

    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" %*
    
    0 讨论(0)
提交回复
热议问题