Raspbian环境下出现ImportError: No module named PIL问题求助
Hey there, let’s work through this frustrating error together— there’s nothing worse than a previously working script breaking after a plugin misstep. Let’s break down the most likely fixes, since this almost always boils down to a Python environment mismatch or confusion between PIL and its maintained fork, Pillow.
First, Understand: PIL vs. Pillow
PIL is no longer maintained— the active, drop-in replacement is Pillow. Most scripts that import PIL will work perfectly with Pillow installed, but you need to make sure it’s installed in the right Python environment that Domoticz is using.
Step 1: Confirm Which Python Version Domoticz Is Using
Domoticz might be using a different Python version than the one you’re installing packages for (e.g., system Python 2.7 vs. a Python 3 virtual environment). Here’s how to check:
- Look in Domoticz’s log files (usually in
/var/log/domoticz.logor your Domoticz data directory) for lines mentioning Python. - Run this command to see the Python process tied to Domoticz:
Look for the path to Python (e.g.,ps aux | grep domoticz/usr/bin/python2.7or/home/domoticz/venv/bin/python3)
Step 2: Install Pillow for the Correct Python Environment
Once you know the Python version/path, install Pillow accordingly:
- If using system Python 2.7 (Debian/Ubuntu):
sudo apt-get update && sudo apt-get install python-pil - If using system Python 3:
sudo apt-get update && sudo apt-get install python3-pil - If using a virtual environment:
Activate the environment first, then install:source /path/to/domoticz/virtualenv/bin/activate pip install pillow deactivate - If Domoticz runs as a dedicated user (e.g.,
domoticz):
Install Pillow directly for that user to avoid permission issues:sudo -u domoticz pip install pillow
Step 3: Fix Path Issues (If Steps 1-2 Fail)
If Pillow is installed but Domoticz’s Python can’t find it, you can manually add the Pillow installation path to your script:
- Find where Pillow is installed:
# For Python 2 pip show pillow | grep Location # For Python 3 pip3 show pillow | grep Location - Add this path to the top of your script:
This is a quick workaround— ideally, you’ll want to fix the environment mismatch long-term, but this can get your script running again fast.import sys sys.path.append("/path/you/found/from/pip/show")
Step 4: Reset Plugin Dependencies
If the error started right after installing the Domoticz plugin, it might have overwritten or removed dependencies. Try:
- Uninstalling the problematic plugin via Domoticz’s interface.
- Reinstalling the plugin’s required dependencies (check the plugin’s README for a list) before reinstalling the plugin itself.
Give these steps a shot in order— checking the Python environment Domoticz uses is usually the key to solving this. Let me know if any of these work, or if you run into more details to debug!
内容的提问来源于stack exchange,提问作者Frederic

