Search code examples
pythonwindowsinstallationsetuptoolsstartmenu

How to create a shortcut in startmenu using setuptools windows installer


I want to create a start menu or Desktop shortcut for my Python windows installer package. I am trying to follow https://docs.python.org/3.4/distutils/builtdist.html#the-postinstallation-script

Here is my script;

import sys

from os.path import dirname, join, expanduser

pyw_executable = sys.executable.replace('python.exe','pythonw.exe')
script_file = join(dirname(pyw_executable), 'Scripts', 'tklsystem-script.py')
w_dir = expanduser(join('~','lsf_files'))

print(sys.argv)

if sys.argv[1] == '-install':
    print('Creating Shortcut')
    create_shortcut(
        target=pyw_executable,
        description='A program to work with L-System Equations',
        filename='L-System Tool',
        arguments=script_file,
        workdir=wdir
    )

I also specified this script in scripts setup option, as indicated by aforementioned docs.

Here is the command I use to create my installer;

python setup.py bdist_wininst --install-script tklsystem-post-install.py

After I install my package using created windows installer, I can't find where my shorcut is created, nor I can confirm whether my script run or not?

How can I make setuptools generated windows installer to create desktop or start menu shortcuts?


Solution

  • If you want to confirm whether the script is running or not, you can print to a file instead of the console. Looks like text you print to console in the post-install script won't show up.

    Try this:

    import sys
    from os.path import expanduser, join
    
    pyw_executable = join(sys.prefix, "pythonw.exe")
    shortcut_filename = "L-System Toolsss.lnk"
    working_dir = expanduser(join('~','lsf_files'))
    script_path = join(sys.prefix, "Scripts", "tklsystem-script.py")
    
    if sys.argv[1] == '-install':
        # Log output to a file (for test)
        f = open(r"C:\test.txt",'w')
        print('Creating Shortcut', file=f)
    
        # Get paths to the desktop and start menu
        desktop_path = get_special_folder_path("CSIDL_COMMON_DESKTOPDIRECTORY")
        startmenu_path = get_special_folder_path("CSIDL_COMMON_STARTMENU")
    
        # Create shortcuts.
        for path in [desktop_path, startmenu_path]:
            create_shortcut(pyw_executable,
                        "A program to work with L-System Equations",
                        join(path, shortcut_filename),
                        script_path,
                        working_dir)