You can get much more control and communication with the process by using subprocess.popen
. Do not block your script until the process returns and you can also communicate with the process via stdin and get stderr / stdout using pipes.
An example would be:
import subprocess
subprocess.Popen(['C:/Program Files/Mozilla Firefox/firefox.exe',
'https://es.stackoverflow.com/',
'-new-tab'])
This opens Mozilla Firefox or creates a new tab (if it is already open) showing the SOes page.
In this case, an absolute path to the executable is used. You can also call processes that are recognized by their name (see below) or use paths relative to the script that launches the process.
Neither the command start
or subprocess.popen
perform a search when a name of an application is provided, they simply pass it to the operating system (via API, for example using ShellExecuteEx
) that is responsible for performing a search Automatic that usually encompasses:
- Current work directory
- Windows Directory
- Windows Directory \ System32
- The directories listed in the PATH environment variable
- Application paths defined in the registry
You can look at the following link for more information:
link
Note that there is no way to call an executable if it is not in any of the previous directories where it can be found by the OS or without providing the absolute / relative path to the executable. It's not a matter of using a library or another or one language or another, it would just be like asking "find something that can run called" example.exe "and it will be somewhere in my file system and execute it "
In your case, open Chrome because in the installation the path was added in the registry (possibly in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
), it is not a matter of being a more or less common program.
Of course, you could create a script that would be responsible for looking for a certain executable in certain directories where you think it is possible that the executable is found and run it if it finds it. Something like the following, which is neither the safest nor the most efficient and which only aims to be a simple example :
import os
import subprocess
def ejecutar(file, directorios):
for ruta in directorios:
for root, dirs, files in os.walk(ruta):
if file in files:
subprocess.Popen([os.path.join(root, file),
'https://es.stackoverflow.com/',
'-new-tab'])
return
file = 'firefox.exe'
directorios = ['C:/Program Files', 'C:/Users/Fulanito']
ejecutar(file, directorios)
All this is referred to Windows as is obvious, in * nix systems the thing is different as expected.