Search code examples
pythontkinterdesktopos.system

How to run .exe file in Desktop for Windows using Python?


I'm trying to run a .exe file written in Python by a click of a button located in Desktop. Need this to work on anyone's desktop so I tested below code but it's giving me errors:

Code:

import os

TestButton = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop', 'Test.exe')
os.system(TestButton)

Error:

C:\Users\<myuser>\Desktop\Test.exe' is not recognized as an internal or external command,

operable program or batch file.

How can I call test.exe from this location Desktop/Test/test.exe. Looking forward to your assist. Thank you very much.


Solution

  • I'm not sure if you are posting pseudocode but python.exe is not valid variable, let alone argument

    import os
    
    python_exe = os.path.join(os.environ['USERPROFILE'], 'Desktop', 'python.exe')
    os.system(python_exe)
    

    Edit: Try

    os.system(f'"{TestButton}"')