You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在Python中无需指定路径,仅通过名称打开操作系统应用?

How to Open Applications by Name (Not Path) in Python

Awesome question—this is such a common headache when you’re trying to make your Python scripts work across different machines. The good news is you absolutely can ditch hardcoded paths and launch apps just by their name, and there are a few reliable ways to do it depending on your target OS.

1. Use subprocess with OS-specific built-in commands

The subprocess module is the modern, more flexible replacement for os.system, and it lets you use your operating system’s native commands that automatically handle application lookup:

Windows

On Windows, the start command can launch apps by name as long as they’re in your system’s PATH or registered as a recognized application (like Notepad, Chrome, etc.):

import subprocess
# Launch Notepad
subprocess.run(["start", "notepad"], shell=True)
# Launch Google Chrome
subprocess.run(["start", "chrome"], shell=True)

Note: shell=True is required here because start is a shell command, not a standalone executable.

macOS

On macOS, use the open command with the -a flag to target an app by its display name, or -b for its bundle ID (great for avoiding name conflicts):

import subprocess
# Launch Safari by name
subprocess.run(["open", "-a", "Safari"])
# Launch Safari using its unique bundle ID
subprocess.run(["open", "-b", "com.apple.Safari"])

Linux

Linux systems use xdg-open to launch apps via their registered desktop entries (works for most properly installed apps):

import subprocess
# Launch Firefox
subprocess.run(["xdg-open", "firefox"])
# Bonus: Open a file with its default associated app
subprocess.run(["xdg-open", "/home/you/documents/note.txt"])

2. Use the webbrowser module (for web browsers)

If you’re specifically targeting web browsers, the webbrowser module is perfect—it’s cross-platform and doesn’t require any paths at all. It’ll even use the system’s default browser by default:

import webbrowser
# Open a URL in the default browser
webbrowser.open("https://stackoverflow.com")
# Or specify a browser by name (if it's in your system PATH)
webbrowser.get("chrome").open("https://stackoverflow.com")

3. Ditch os.startfile for portability

os.startfile is Windows-only and relies entirely on file paths, so replacing it with the subprocess methods above will make your script work seamlessly across Windows, macOS, and Linux.

Pro Tip: Check if the app exists first

To avoid errors if the app isn’t installed or isn’t in the system PATH, use shutil.which() to verify before launching:

import shutil
import subprocess

app_name = "chrome"
if shutil.which(app_name):
    # Adjust the command based on your OS
    subprocess.run(["start", app_name], shell=True)  # Windows
    # subprocess.run(["open", "-a", app_name])  # macOS
    # subprocess.run(["xdg-open", app_name])  # Linux
else:
    print(f"Oops: {app_name} wasn't found on this system!")

内容的提问来源于stack exchange,提问作者Resadesker

火山引擎 最新活动