You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已安装Pillow仍遇ModuleNotFoundError: No module named 'PIL'问题求助

Fixing PIL Import Error on Mac OS X 10.12.6

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):
    Open python3 and 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):
    1. Open the file in a text editor:
      nano ~/.bash_profile  # or nano ~/.zshrc if you use zsh
      
    2. Add this line at the end:
      export PYTHONPATH="/usr/local/lib/python3.6/site-packages:$PYTHONPATH"
      
    3. Save and exit (Ctrl+O, then Ctrl+X in nano), then reload the configuration:
      source ~/.bash_profile  # or source ~/.zshrc
      
    Now when you run python3, it will automatically check that directory for packages.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:31:11