Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why in Python sometimes from PIL import Image fails and import Image works?

Tags:

python

The below piece of code seems to fails for some people while the second one seems to works.

I would like to know why and which would be the best option to choose in order to minimize potential import failures?

from PIL import Image # Fails for some ?!
import Image
like image 821
sorin Avatar asked Jan 18 '23 03:01

sorin


2 Answers

"import Image" works because PIL makes use of the site-specific import hooks to add its install directory into the import path.

[me@oldserver]$ cat /usr/lib/python2.4/site-packages/PIL.pth
PIL

The only situation I can think of where "import Image" works but "from PIL import Image" doesn't is if the install directory for PIL is outside of the import path but a .pth file exists which points to /some/install/path/PIL.

like image 159
Shawn Chin Avatar answered Jan 28 '23 14:01

Shawn Chin


refering PIL Install

Step 1 : Install the build dependencies

sudo apt-get build-dep python-imaging

Step 2 : Symlink the libraries

sudo ln -s /usr/lib/`uname -i`-linux-gnu/libfreetype.so /usr/lib/
sudo ln -s /usr/lib/`uname -i`-linux-gnu/libjpeg.so /usr/lib/
sudo ln -s /usr/lib/`uname -i`-linux-gnu/libz.so /usr/lib/

Step 3 : Install

pip install PIL
like image 44
PiyusG Avatar answered Jan 28 '23 14:01

PiyusG