Windows10下notify2模块因缺失dbus报错,安装dbus-python失败求助
Got it, let's break this down. The core issue here is that notify2 is built specifically for Linux desktop environments that rely on DBus—Windows doesn't have a native DBus service, which is why installing dbus-python via pip throws that [WinError 193] error. Let's cover two solutions, from the simplest (and most Windows-friendly) to the more complex if you really need to stick with notify2.
Since notify2 isn't designed for Windows, the easiest fix is to swap it out for a library built specifically for Windows 10 notifications. Two great options are win10toast or plyer (which works cross-platform but has solid Windows support). Here's how to use win10toast:
First, clean up the broken
dbus-pythoninstallation (if you installed it):pip uninstall dbus-python -yInstall
win10toast:pip install win10toastReplace your
notify2code with this Windows-native alternative. For example, if your original code looked like this:import notify2 # Initialize the notification service notify2.init("My Application Name") # Create and show a notification notification = notify2.Notification("Hello!", "This is a test notification from notify2") notification.show()You can rewrite it to use
win10toastlike this:from win10toast import ToastNotifier # Initialize the toaster toaster = ToastNotifier() # Show a notification (duration is in seconds) toaster.show_toast( "Hello!", "This is a test notification for Windows 10", duration=5, icon_path=None # Optional: add a .ico file path here for custom icons )
This will show native Windows 10 toast notifications, no DBus required.
If you absolutely need to keep using notify2, you'll have to manually set up a DBus environment on Windows. This is more complex, but here's how to do it:
Install DBus for Windows
Download the official DBus Windows installer from the dbus-win32 project. Make sure to pick the version that matches your Python architecture (32-bit or 64-bit). Run the installer and follow the prompts—leave the default installation path unless you have a reason to change it.Add DBus to your system PATH
After installation, add the DBusbindirectory to your Windows system PATH. For example, if you installed DBus toC:\Program Files\DBus, addC:\Program Files\DBus\binto your PATH. You can do this via:- Right-click "This PC" → Properties → Advanced system settings → Environment Variables
- Under "System variables", find
Path, click Edit, then add the DBus bin path.
Install a Windows-compatible dbus-python
The PyPI version ofdbus-pythondoesn't support Windows, so use Conda to install a precompiled version instead (you'll need Anaconda or Miniconda installed first):conda install -c conda-forge dbus-pythonStart the DBus session daemon
Before running yournotify2code, you need to start the DBus session. Open a command prompt and run:dbus-daemon --session --address=unix:path=/tmp/dbus-session --nofork --print-addressKeep this command prompt open while your Python script is running.
Test notify2
Now try running your originalnotify2code—it should initialize and send notifications correctly, assuming the DBus daemon is running.
内容的提问来源于stack exchange,提问作者Akhil Ranjan

