Python tries to install everything into /lib on os x
Question: I think that somehow the path /lib is stored in my python dist where it should
not be.

It started when I was having troubles installing python modules using pip. Pip
seemed to install everything into /lib/python2.7/site-packages where python
could not find it.

Sidenote: pip uninstall could not find the package in /lib either, but it is
where pip install would install it.

I tried:

    
    
    which pip
    $/usr/bin/pip
    $which python
    /usr/bin/python
    

I decided to uninstall pip, but then

    
    
    $ easy_install uninstall pip
    error: can't create or remove files in install directory
    
    The following error occurred while trying to add or remove files in the
    installation directory:
    
    [Errno 13] Permission denied: '/lib'
    
    The installation directory you specified (via --install-dir, --prefix, or
    the distutils default setting) was:
    
    /lib/python2.7/site-packages/
    

It seemed that even in my easy-install, the '/lib' location was used. I
googled a bit, and decided to reinstall easy-install. I removed it:

    
    
    $sudo rm /usr/local/bin/easy_install
    

And tried to install it again:

    
    
    $ sudo curl https://bootstrap.pypa.io/ez_setup.py -o - | python
    Checking .pth file support in /lib/python2.7/site-packages/
    error: can't create or remove files in install directory
    

So my problem is basically that I want to get my python installation as clean
as possible, and that this /lib location is stored somewhere.

Some side information

  * I am getting more familiar with the file structure of python now but I used to know little about it. I also had many problems installing python packages so I used many different python versions trough tutorials. (Via brew, canopy, anaconda, ipython). I uninstall most of them because I just want a clean installation as possible. (I once had tried to uninstall a site-package and I discovered that it was stored in 4 different locations simultaniously!)

  * > $ which python
>
> /usr/bin/python

  * Most of my site-packages right now are installed in:

> /usr/local/lib/python2.7/site-packages
>
> /Users/myusersname/Library/Python/2.7/lib/python/site-packages

  * Empty:

> $ echo $PYTHONPATH

    * OS-X 10.9.5

I hope you guys can help me!

easy install pip

I want to get everything as clean as possible so I uninstalled my homebrew
version of python.

**EDIT** :

**Python from homebrew** So I uninstalled all python versions except the
system one (/usr/bin/python). Now I tried to install python via homebrew
(/usr/local/bin/python does link to cellar).

When I try to run pip:

    
    
    $which pip
    /usr/local/pip
    
    $pip
    Traceback (most recent call last):
     File "/usr/local/bin/pip", line 5, in <module>
     from pkg_resources import load_entry_point
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 2603, in <module>
    working_set.require(__requires__)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 666, in require
    needed = self.resolve(parse_requirements(requirements))
     File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 565, in resolve
    raise DistributionNotFound(req)  # XXX put more info here
    pkg_resources.DistributionNotFound: pip==1.5.6
    

When I try to

    
    
    sudo easy_install -U pip
    TEST FAILED: /lib/python2.7/site-packages/ does NOT support .pth files
    error: bad install directory or PYTHONPATH
    

**Python from python.org** I uninstalled homebrew python and installed python
using the GUI installer from the website. I checked that /usr/local/bin/python
does link to this python.

This python does not come with pip or easy install. So I run setuptools:

    
    
    $ sudo python ez_setup.py 
    Extracting in /tmp/tmpR80Ydp
    Now working in /tmp/tmpR80Ydp/setuptools-7.0
    Installing Setuptools
    running install
    Checking .pth file support in /lib/python2.7/site-packages/
    /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python -E -c pass
    TEST FAILED: /lib/python2.7/site-packages/ does NOT support .pth files
    error: bad install directory or PYTHONPATH
    You are attempting to install a package to a directory that is not
    on PYTHONPATH and which Python does not read ".pth" files from.  The
    installation directory you specified (via --install-dir, --prefix, or
    the distutils default setting) was:
    /lib/python2.7/site-packages/
    

This is the error I am allways getting. It is very persistent and I hope you
guys can help me with it. I allready tried some of the solutions here:

  * [Python pip broken after OS X 10.8 upgrade](http://stackoverflow.com/questions/11704379/python-pip-broken-after-os-x-10-8-upgrade)
  * [pip install on Mac OS X - PYTHONPATH](http://stackoverflow.com/questions/25978475/pip-install-on-mac-os-x-pythonpath)

but nothing helps.

Setting the PYTHONPATH or running with or witouth sudo doesn't help eigther.

    
    
    export PYTHONPATH='/Library/Python/2.7/site-packages'
    


Answer: If you want to get your python installation as clean as possible you should
consider using a virtual environment.

    
    
    $ sudo pip install virtualenv
    $ pyvenv env                   # create a virtual environment
    $ source env/bin/activate      # activate the virtual environment
    (env) $ curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python   # install pip in the virtualenv
    

This code works for python 3.4 but it should be something similar for python
2.7. Then you can install packages as you would normally do:

    
    
    (env) $ pip install [package name]
    

All the packages you install this way will be saved in the "env" directory. If
you want to run a program inside the virtual environment you have to activate
it first. When you are done, you can simply deactivate it.

    
    
    (env) $ deactivate
    

