已安装Pillow仍遇ModuleNotFoundError: No module named 'PIL'问题求助
Hey there, let's work through this Pillow import issue you're facing. It sounds like a classic case of Python package path mismatch—where pip is installing packages to a directory your Python interpreter isn't checking. Here's how to fix it:
Step 1: Verify pip and Python Version Alignment
First, make sure the pip3 you're using is linked to the same python3 interpreter you're running. Run these commands in your terminal:
# Check where pip3 is installing packages pip3 --version # Check which python3 you're running which python3
Compare the paths in the output. For example, if pip3 points to /usr/local/bin/pip3 (linked to Homebrew's Python) but python3 points to /Library/Frameworks/Python.framework/Versions/3.6/bin/python3 (the official framework build), that's the root of the problem—they're different Python installations.
Step 2: Fix the Path Mismatch
You have a few options to resolve this:
Option 1: Add the Missing Path to Python's sys.path (Temporary & Permanent)
Since your Pillow is installed in /usr/local/lib/python3.6/site-packages, you can tell Python to look there:
- Temporary fix (for current session only):
Openpython3and run:import sys sys.path.append('/usr/local/lib/python3.6/site-packages') import PIL # This should work now - Permanent fix:
Add the path to your shell configuration file (.bash_profile,.zshrc, etc.—depending on which shell you use):- Open the file in a text editor:
nano ~/.bash_profile # or nano ~/.zshrc if you use zsh - Add this line at the end:
export PYTHONPATH="/usr/local/lib/python3.6/site-packages:$PYTHONPATH" - Save and exit (Ctrl+O, then Ctrl+X in nano), then reload the configuration:
source ~/.bash_profile # or source ~/.zshrc
python3, it will automatically check that directory for packages. - Open the file in a text editor:
Option 2: Install Pillow Using the Correct pip
If you want to install Pillow directly into the Python interpreter you're using, find the full path to its pip executable. From your sys.path output, your Python is at /Library/Frameworks/Python.framework/Versions/3.6, so use its pip:
/Library/Frameworks/Python.framework/Versions/3.6/bin/pip3 install Pillow
This will install Pillow into /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages, which is already in your sys.path.
Option 3: Check for Virtual Environments
If you've ever set up a virtual environment for your projects, make sure you're not accidentally in one. Run deactivate in your terminal to exit any active virtual environment, then try importing PIL again.
Why sklearn Works?
Odds are sklearn was installed into both Python directories (the /usr/local one and the /Library/Frameworks one), so even though it's not in your sys.path, the interpreter finds the copy in its own site-packages folder. Pillow only got installed to the /usr/local version, hence the error.
Give these steps a try—one of them should get PIL importing correctly for you!
内容的提问来源于stack exchange,提问作者Treaser

