Search code examples
shellpython-3.xpython-imaging-libraryanacondapython-idle

Cannot import PIL into Python Shell, but can into Terminal


I have installed Pillow on may Macintosh using sudo pip install Pillow in Terminal. I know it is installed because whenever I run import PIL in the Terminal window after typing python, this is my output:

Terminal output:

Python 3.5.1 |Anaconda 2.4.1 (x86_64)| (default, Dec  7 2015, 11:24:55) 
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import PIL
>>> 

However, the weird thing is that in the Python Shell, when i do the same thing, I get this:

Python Shell (IDLE) output:

Python 3.5.1 (v3.5.1:37a07cee5969, Dec  5 2015, 21:12:44) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> import PIL
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    import PIL
ImportError: No module named 'PIL'

Why is it that I can import the Python Imaging Library (PIL) in the Macintosh Terminal, but not in the Python Shell, and how would I fix this issue?


Solution

  • You should never use sudo to install a python module.

    When you have multiple versions of python installed, each one has its own name, its own pip, and its own IDLE. The command you issue on the command line determines which python will launch. One of the versions of python that you installed will be linked to the name python while other versions will have a name that includes the version, e.g. python3.5.

    To keep straight which version of python you are launching, you can launch python with its version name, python3.5, execute pip with its version name, pip3.5, and start IDLE with its version name, idle3.5.

    Here are some commands that will help you figure out where pip install Pillow is installing modules:

    ~$ which pip
    /usr/local/bin/pip
    
    ~$ cd /usr/local/bin
    
    /usr/local/bin$ ls -al pip*
        lrwxrwxr-x  1 root  admin  65 Apr 10  2015 pip ->
     ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pip
        lrwxrwxr-x  1 root  admin  66 Apr 10  2015 pip2 ->
     ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pip2
        lrwxrwxr-x  1 root  admin  68 Apr 10  2015 pip2.7 ->
     ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pip2.7
        lrwxrwxr-x  1 root  admin  66 Apr  7  2015 pip3 ->
     ../../../Library/Frameworks/Python.framework/Versions/3.4/bin/pip3
        lrwxrwxr-x  1 root  admin  68 Apr  7  2015 pip3.4 ->
     ../../../Library/Frameworks/Python.framework/Versions/3.4/bin/pip3.4
    

    The syntax pip -> means that the name pip is linked to the file after the arrow. You can see where those names are pointing and therefore which python that pip is installing modules into.

    ===

    You have Anaconda installed, and Anaconda hijacks the names python and pip for its own use, so you will see something like this:

    ~$ which python
    /Users/7stud/anaconda/bin/python
    
    ~$ which pip
    /Users/7stud/anaconda/bin/pip
    

    Anaconda does not hijack the name idle, so idle points to another python install:

    ~$ which idle
    /usr/local/bin/idle
    
    ~$ cd /usr/local/bin
    
    /usr/local/bin$ ls -al idle*
        lrwxr-xr-x  1 root  wheel  66 Apr 10  2015 idle ->
     ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/idle
        lrwxr-xr-x  1 root  wheel  67 Apr 10  2015 idle2 ->
     ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/idle2
        lrwxr-xr-x  1 root  wheel  69 Apr 10  2015 idle2.7 ->
     ../../../Library/Frameworks/Python.framework/Versions/2.7/bin/idle2.7
        lrwxr-xr-x  1 root  wheel  67 Apr  7  2015 idle3 -> 
    ../../../Library/Frameworks/Python.framework/Versions/3.4/bin/idle3
        lrwxr-xr-x  1 root  wheel  69 Apr  7  2015 idle3.4 -> 
    ../../../Library/Frameworks/Python.framework/Versions/3.4/bin/idle3.4
    

    Anaconda hijacks the names python and pip by adding the following line to the end of the file ~/.bash_profile:

    export PATH="/Users/7stud/anaconda/bin:$PATH"
    

    When you issue a command on the command line, the terminal window searches for that command in the directories specified in your PATH environment variable--in order. Because the Anaconda path is on the front of the PATH variable, the Anaconda directory is searched first for the commands python and pip and idle. The python command is found in that directory:

    ~$ which python
    /Users/7stud/anaconda/bin/python
    
    ~$ cd /Users/7stud/anaconda/bin/
    
    ~/anaconda/bin$ ls -al python*
    lrwxr-xr-x  1 7stud  staff     9 Dec 22 05:29 python -> python3.5
    -rwxrwxr-x  1 7stud  staff   272 Dec 22 05:29 python-argcomplete-check-easy-install-script
    -rwxrwxr-x  1 7stud  staff   129 Dec 22 05:29 python.app
    lrwxr-xr-x  1 7stud  staff     9 Dec 22 05:29 python3 -> python3.5
    lrwxr-xr-x  1 7stud  staff    17 Dec 22 05:29 python3-config -> python3.5m-config
    -rwxrwxr-x  1 7stud  staff  9096 Dec  7 10:27 python3.5 
    

    (In addition to the name python, you can also see the names python3 and python3.5. Because python and python3 both point to python3.5, the commands python, python3, and python3.5 all execute the same python.)

    The pip command is also found in the Anaconda directory:

    $ which pip
    /Users/7stud/anaconda/bin/pip
    
    $ cd /Users/7stud/anaconda/bin
    
    ~/anaconda/bin$ ls -al pip*
    -rwxrwxr-x  1 7stud  staff  125 Dec 22 05:29 pip
    

    There's only one name for pip.

    But, the idle command is not found in the Anaconda directory:

    ~$ which idle
     /usr/local/bin/idle
    

    Double checking:

    ~$ cd ~/anaconda/bin
    
    ~/anaconda/bin$ ls -al idle*
    lrwxr-xr-x  1 7stud  staff    7 Dec 22 05:29 idle3 -> idle3.5
    -rwxrwxr-x  1 7stud  staff  108 Dec 22 05:29 idle3.5
    

    The name idle is not the same as idle3 or idle3.5, so the search for idle continues on in the other directories in the PATH variable. If you want to launch the Anaconda idle, you can use either the name idle3 or idle3.5.

    Because I hate the way Anaconda hijacks all those python names, which prevents me from using those names to run my other python versions, when I'm not using Anaconda I comment out the following line in ~/.bash_profile:

    # export PATH="/Users/7stud/anaconda/bin:$PATH"
    

    To get changes you make in ~./bash_profile to take effect, you need to either close the terminal window and launch a new window, or issue the command:

    $ source ~/.bash_profile
    

    Another way to keep Anaconda from hijacking all those python names is by doing the following:

    ~/anaconda/bin$ mv python xpython  #change the name to xpython
    ~/anaconda/bin$ mv python3 xpython3 
    ~/anaconda/bin$ cp python3.5 pyana  #copy to a new name
    ~/anaconda/bin$ mv python3.5 xpython3.5
    

    That preserves all the original names under pseudonyms (in case something goes wrong you can change the x-names back to the originals), and it also creates the name pyana for your Anaconda python. Now the names python, python3, etc. will still point to the same python versions they did before you installed Anaconda:

    ~/anaconda/bin$ which python
    /usr/local/bin/python
    
    ~/anaconda/bin$ which python3
    /usr/local/bin/python3
    
    ~/anaconda/bin$ which pyana
    /Users/7stud/anaconda/bin/pyana
    
    ~/anaconda/bin$ pyana --version
    Python 3.5.1 :: Anaconda 2.4.1 (x86_64)
    
    ~/anaconda/bin$ pyana
    Python 3.5.1 |Anaconda 2.4.1 (x86_64)| (default, Dec  7 2015, 11:24:55) 
    [GCC 4.2.1 (Apple Inc. build 5577)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 
    

    Edit: Some time later I found that openssl is found in Anaconda's directory as well, so I've gone back to commenting out the line in .bash_profile when I'm not using Anaconda--Anaconda just hijacks too many names.