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

Time:02-01

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.

CodePudding user response:

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}"')

CodePudding user response:

Those whitespaces can really be a bother. Try os.chdir('C:/Documents\ and\ Settings/') followed by relative paths for os.system, subprocess methods, or whatever...

If best-effort attempts to bypass the whitespaces-in-path hurdle keep failing, then my next best suggestion is to avoid having blanks in your crucial paths. Couldn't you make a blanks-less directory, copy the crucial .exe file there, and try that? Are those havoc-wrecking space absolutely essential to your well-being...?

  • Related