Explain Codes LogoExplain Codes Logo

How to install PIL with pip on Mac OS?

python
pip-installation
mac-os
virtual-environment
Anton ShumikhinbyAnton Shumikhin·Sep 17, 2024
TLDR

To bask in the glory of Pillow, a more maintained fork of PIL, on your Mac OS, execute this on your terminal:

pip install Pillow

Permission issues? Run like Forrest (sudo) or better yet, enjoy a mess-free virtual environment.

Prepare your installation batter

Before cracking eggs, we arrange all the ingredients, don't we? Thus, before installing Pillow, let's make sure you've got Xcode and Xcode Command Line Tools. They ensure a smooth sail and keep the monstrous 'clang' errors at bay.

Here's the magic spell:

  1. Download and install Xcode from the Mac App Store. Be careful, it's a large download!
  2. Once you've slayed the Xcode beast, get the Xcode Command Line Tools by muttering these words in your Terminal:
    xcode-select --install
  3. Update pip to the latest version for maximal effectiveness. Pip also needs to stay in shape!
    pip install --upgrade pip
  4. Optionally, installing libraries such as python-dev, libjpeg-dev, libpng-dev, and libfreetype6-dev with brew can be a good idea, especially if you're looking to do more with image manipulation:
    brew install jpeg libpng freetype

Undo potential jinxes

Troubles on your way? Here's your patronus charm:

  • Add the --verbose flag to check progress, like watching your own DIY video.
  • Symbolic links for libfreetype.so, libjpeg.so, and libz.so might be needed if the terminal screams about missing these files.
  • To install a particular version of Pillow, specify the version in the command, like ordering your favorite coffee:
    pip install Pillow==x.x.x # Double, no triple shot, please!
  • Beware of security warnings. In the world of Internet, it's always Halloween.

Juggling dependencies and permissions

Getting Pillow to play nicely with your Mac involves understanding dependencies and system permissions. It's a bit like a reality show, let the games begin:

  • Dependencies like libjpeg and libpng might require special installation procedures. Brew is your friend here:
    brew install libjpeg libpng # Brew, do your magic!
  • If you run into a brick wall of permission issues, break it down with sudo or opt for a comfortable virtual environment. It's less messy!

Cross platform but not cross complications

A Mac isn't just any machine. There might still be a hoop or two to jump through:

  • If you're on the Python 3 track, use the following spell:
    python3 -m pip install Pillow # Python 3, pillow fight!
  • To make sure all your efforts were worthwhile, summon Python to check the installation:
    from PIL import Image # Hello from the Pillow side!
  • For those unfortunate times when pip gives up, easy_install steps up to the plate:
    easy_install Pillow # Easy does it
  • Code refactor? Simply move from PIL to 'from PIL import Image' when switching on to Pillow.

Get the most from your Pillow kingdom. Explore every nook and cranny:

  • Use Pillow's native operations to perform image resizing, filtering and more. Fast and furious!
  • Marvel at Pillow’s ImageEnhance module to quickly enhance image quality.
  • Work with Pillow in Python’s virtual environments for dependency joyrides.