Subprocess library package: How To Call Executable Using Its Absolute Path

Hello,

using the subprocess library package, is there a way to run an executable by providing its absolute path? I know that you can run the following apps without it:

import subprocess

subprocess.run('notepad')

# or

subprocess.run('start winword', shell=True)

# or

subprocess.run('start excel', shell=True)

Is there a way to call / open an application using its absolute path?

For example, C:\Program Files\JetBrains\PyCharm Community Edition 2024.2.1\bin\pycharm64.exe

Any help would be appreciated.

Just use the absolute path and it will work.

Do you think that subprocess.run does not take an absolute path?

I have already tried it but the following exception is generated:

FileNotFoundError: [WinError 2] The system cannot find the file specified

How can this be if you’re literally including the absolute path as the argument?

subprocess.run('C:\Program Files\JetBrains\PyCharm Community Edition 2024.2.1\bin\pycharm64.exe')

Use a raw string literal to avoid expanding escape sequences like \b in the file name. I’d also recommend passing the command as a list (not a single string) with shell=False (the default) unless you know what you’re doing and want an intermediate shell process.

subprocess.run([r'C:\Program Files\JetBrains\PyCharm Community Edition 2024.2.1\bin\pycharm64.exe'])

See Frequently Used Arguments from the docs for more on how the command argument is process and what shell=True does.

1 Like

Perfecto mundo!

Got it working.

subprocess.run(r'C:\Program Files\JetBrains\PyCharm Community Edition 2024.2.1\bin\pycharm64.exe')

Thank you. Much appreciated. :handshake: :grinning: