131

When I run:

sudo pip install ipython

I get the following error

OSError: [Errno: 1] Operation not permitted: '/System/Library/Frameworks/Python.framework/Versions/2.7/share'

The last command executed tries to create the directory given above.

Also, the following command fails to install iPython without providing any errors.

sudo pip install --user python

(I am on Mac OS X El Capitan in case other folks on this OS see the same issue.)

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Milad M
  • 1,538
  • 2
  • 13
  • 13
  • Relevant: [How to Use Pip after the El Capitan Mac OS X Upgrade](http://apple.stackexchange.com/questions/209572/how-to-use-pip-after-the-el-capitan-max-os-x-upgrade) – kojiro Feb 28 '16 at 13:48
  • I have added an answer that explains how to modify your `$PATH` so that you can call the executables for installed --user packages. https://stackoverflow.com/a/47102398/117471 – Bruno Bronosky Nov 03 '17 at 18:34
  • 2
    You shouldn't use `sudo` with `--user`. What `--user` does is install into `~/Library` rather than `/Library` You don't have to use `sudo` to install there. But once you install with `sudo`, the directories and files are all owned by `root`. Then you have to use `sudo` to do anything else to them. Which in turn will require you to use `sudo` for other things you shouldn't use it for. Then even an honest mistake can go from throwing an error, to destroying data. – Bruno Bronosky Nov 03 '17 at 18:44

12 Answers12

209

Instructions telling people to use sudo pip install are inherently wrong.

enter image description here

If there is any tutorial out there which says you should use sudo pip then please file a bug against this package. The author is dis-educating the Python community, as time has proven sudo pip to be a broken practice.

OSX El Capitan introduced mechanisms to prevent damaging the operating system files. /System/Library/Frameworks/Python.framework/Versions/2.7/share is one of the protected locations. A normal user has no reason to put or write any files there. This is because the operating system itself relies on these files and sudo pip, with all force given from the above, would unconditionally overwrite them. Usually bad things would not happen, but the chances are there. Apple wants to protect their OS users from accidentally bricking their installation.

Instead, you need to install a Python package, like IPython, locally to the home folder of your user. The easiest way is to create a virtual environment, activate it, and then run pip in the virtual environment.

Example:

cd ~  # Go to home directory
virtualenv my-venv
source my-venv/bin/activate
pip install IPython

More info

Alternatively, one should be able to use pip install --user. But again, sudo is not needed and you need to manually set up PATH environment variable.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
  • 5
    If no virtualenv is desired and you want to globally (eg /Library/Python/2.7/site-packages) install modules, try my solution below using `easy_install` http://stackoverflow.com/a/33463702/3078330 – smat88dd Nov 04 '15 at 17:51
  • 3
    Do you have a citation for this perspective on 'sudo pip' being bad ("as time has proven sudo pip to be a broken practice")? I don't see any discussion of sudo in the citations you give and the tutorial lists the first step to creating virtualenvs as 'pip install virtualenv' which seems to create a circular dependency trying to follow your advice. – Von Nov 26 '15 at 02:01
  • @Von: I can look a citation for you if you want. I have been developing Python since 2003, so I hope you just would take my word on this for now. – Mikko Ohtamaa Nov 26 '15 at 05:30
  • 1
    @Von: `pip install virtualenv` should no longer needed for Python 3.4+, as they come with built-in `python -m venv` virtualenv equivalent. https://docs.python.org/3/library/venv.html – Mikko Ohtamaa Nov 26 '15 at 05:32
  • @Von: I just re-read official Python tutorial. You are right. I'll update it to match full workflow, so it doesn't have circulat "install virtualenv to virtualenv" issue. – Mikko Ohtamaa Nov 26 '15 at 05:35
  • 1
    @MikkoOhtamaa I have no problem taking your word, but that limits the amount of education that can be done. Having this in official python docs would go a long way towards improving dissemination, I'm not a dev but would consider myself moderately well-informed on python & pip and have never heard of this until your comments. Also I'm interested to understand the problems people have seen as I used 'sudo pip' to modify the system install for a while without noticeable problems (I don't anymore, but that's because I switched to using homebrew, another story on file ownership...). – Von Nov 26 '15 at 15:51
  • 1
    @Von: Witness havoc and madness https://bugs.launchpad.net/ubuntu/+source/python-pip/+bug/1308714/comments/2 – Mikko Ohtamaa Nov 26 '15 at 22:26
  • 29
    With all due respect, that doesn't address the original question. I'm trying to install IPython and make it available globally (for all users on my machine), and neither a virtualenv nor `pip install --user IPython` help with that. Moreover, `pip install --user IPython` fails to place the `ipython` command in the `$PATH` (at least on my machine). – jpetazzo Feb 28 '16 at 11:56
  • @jpetazzo: Is there any specific issue with more details? Is your question how to place IPython to path? Usually this follows normal virtual environment workflow where virtual environment activation automatically updates your `PATH`: https://packaging.python.org/en/latest/installing/#creating-virtual-environments – Mikko Ohtamaa Feb 28 '16 at 19:37
  • 1
    `pip install --ignore-installed --user jupyter` works for my iPython installation. `sudo pip` didn't. – Sophia Feng Apr 13 '16 at 06:15
  • @jpetazzo this worked for me: ` export PATH=$PATH:~/Library/Python/2.7/bin` – Chet Jun 16 '16 at 17:06
  • 1
    I was getting the same error using pip to install a list of dependencies from a txt file, and was using sudo. I stopped using sudo and everything worked! Great answer. – jeffery_the_wind Sep 16 '16 at 09:30
  • 3
    You taught me and also saved my time. A big Thanks I stopped using sudo and everything worked! Great answer. – Parth Gupta Oct 19 '16 at 11:54
  • But still need "sudo pip" to install "virtualenv", is that right? – ZZ Shao Feb 07 '17 at 09:45
  • 1
    You cannot install anything with sudo. I suggest you get a third party, up-to-date, Python 3 install from Homebrew that comes with built-in virtual env. Furthermore you can install virtualenv locally to your home directory using the bootstrap script. – Mikko Ohtamaa Feb 07 '17 at 21:42
  • @MikkoOhtamaa How would Python 3 installed to one user account through Homebrew be made available to all other user accounts currently existing and hereinafter created on the same machine? – Damian Yerrick Jul 21 '17 at 17:25
  • @DamianYerrick I am not a HomeBrew expert, so I cannot answer this question. I suggest you seek answer on HomeBrew specific forum or open a new question. – Mikko Ohtamaa Aug 04 '17 at 11:10
  • 1
    I have added an answer that explains how to modify your `$PATH` so that you can call the executables for installed --user packages. https://stackoverflow.com/a/47102398/117471 – Bruno Bronosky Nov 03 '17 at 18:35
  • Best answer, just not give you the solution, but explain why you are facing this issue. Thumbs up. – Napster_X Sep 14 '18 at 08:31
  • @jpetazzo Since nobody gave you a satisfactory reason yet, the reason not to `sudo pip install ` is that you can easily screw up the system's Python environment (for example, by upgrading some dep to a version incompatible with other important system services or daemons). **The OS itself uses Python, and the OS's site-packages dir is for the OS alone**! If you *must* install libraries or apps system-wide (for all users), the best way to do it is using the package manager supported on the system (e.g. `yum`, `apt-get`, or *maybe* `brew`), since those versions should all be compatible. – wim Aug 13 '19 at 03:11
  • As an update Apple is looking to remove Python and other UNIX scripting languages from OS starting from macOS Catalina https://developer.apple.com/documentation/macos_release_notes/macos_catalina_10_15_beta_5_release_notes – Mikko Ohtamaa Aug 13 '19 at 13:00
24

I had the same problems, but using the easy_install "module" solved the problem for me.

I am not sure why, but pip and easy_install use different install locations, and easy_install chose the right ones.

Edit: without re-checking but because of the comments; it seems that different (OSX and brew-installed) installations interfere with each other which is why the tools mentioned point to different locations (since they belong to different installations). I understand that usually those tools from one install point to the same folder.

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
smat88dd
  • 2,258
  • 2
  • 25
  • 38
  • Do you have two different Python interpreter installed and `easy_install` is configured for another one? – Mikko Ohtamaa Nov 05 '15 at 03:13
  • I do have 2-3 different python versions installed, but I have `easy_install` for all of them: `easy_install-2.6`, `easy_install-2.7`, `easy_install-3.5` – smat88dd Nov 05 '15 at 09:57
  • Most likely `pip` and `easy_install` point to different Python's. You can figure out this with `which` UNIX command then figure out right `pip` command to use. – Mikko Ohtamaa Nov 06 '15 at 04:44
  • I have each python version installed just once (that is python 2.6 and 2.7 pre-installed by apple, and python 3.5 with homebrew). `/usr/local/bin/pip install nose` points to python 2.7 and failed for me with error, but `/usr/bin/easy_install-2.7 nose` which also points to py2.7 worked for me. – smat88dd Nov 06 '15 at 12:34
  • `/usr/local/bin/pip -V pip 7.1.2 from /Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg (python 2.7)`, and `/usr/bin/easy_install-2.7 --version setuptools 1.1.6`. I myself noticed the different path (once usr/bin and once usr/local/bin) but dont know why that is. – smat88dd Nov 06 '15 at 12:35
  • `/usr/bin` is OSX Apple supplied system-wide which you cannot write. `/usr/local/bin` is most likely Homebrew installed which is what you want as a developer. – Mikko Ohtamaa Nov 06 '15 at 14:59
  • I was thinking the same, but: `pwd /System/Library/Frameworks/Python.framework/Versions` -> `2.6, 2.7` which means they really both are Apple-provided, since /System is OSX SIP protected which one cannot write to, too. Please believe me, python 2.6 and 2.7 both seem to be pre-installed from Apple. – smat88dd Nov 06 '15 at 16:00
  • 1
    Might be prior install from old OSX before upgrade? – Mikko Ohtamaa Nov 06 '15 at 18:50
  • Haha, okay, maybe? I did upgrade, and when El Capitan Upgrade/SIP didnt fully overwrite the /System directory, it might be from before. Another question: do you own a Mac with El Capitan? You have only Python 2.7 installed? – smat88dd Nov 07 '15 at 01:29
  • `which python2.6 /opt/local/bin/python2.6` – Mikko Ohtamaa Nov 08 '15 at 06:02
  • 1
    *pip and easy_install use different install locations* – no, they use the exact same locations. It *worked* by accident as @MikkoOhtamaa was trying to show you in his comments. This is clearly non-answer. – Piotr Dobrogost Apr 08 '17 at 21:03
  • They __were__ pointing to different locations, because they were from different installs maybe. I understand that usually those tools from one install point to the same location, but well - things get out of order sometimes, right? As you can see this answer helped people with maybe the same situation, so whats your problem? – smat88dd Apr 11 '17 at 09:19
  • This worked for me as well, after first failing with sudo pip install --ignore-installed ipython (failed with the "share" msg). I guess easy_install found the partially installed ipython and "rescued" it.. – BjornW Mar 20 '18 at 15:41
19

You should reinstall Python:

brew reinstall python

To get brew see the brew homepage.

nbkhope
  • 7,360
  • 4
  • 40
  • 58
david euler
  • 714
  • 8
  • 12
  • I second. i run `brew install python` then i think `pip` is already in place. After that `pip` install worked (i had similar problems w/ permissions and pip bailing on installs) – travelingbones May 27 '17 at 00:28
15

pip install --ignore-installed six

This will do the job, then you can try your first command.

Via http://github.com/pypa/pip/issues/3165

Alireza
  • 175
  • 1
  • 6
13

Used pip3 install <package> instead and solved the permission problem with pip.

Keng
  • 854
  • 10
  • 10
  • 11
    FYI, this worked because `pip3` uses Python 3, which is usually installed by user. Python 2 (2.7), on the other hand, is shipped with macOS, and is protected as the top rated answer explains. – Khanh Nguyen Jan 08 '17 at 07:23
10

Same error

Installing collected packages: six, pyparsing, packaging, appdirs, setuptools
Exception:
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/commands/install.py", line 342, in run
    prefix=options.prefix_path,
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_set.py", line 784, in install
    **kwargs
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_install.py", line 851, in install
    self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_install.py", line 1064, in move_wheel_files
    isolated=self.isolated,
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/wheel.py", line 345, in move_wheel_files
    clobber(source, lib_dir, True)
  File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/wheel.py", line 323, in clobber
    shutil.copyfile(srcfile, destfile)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 83, in copyfile
    with open(dst, 'wb') as fdst:
IOError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/six.py'

and here I use --user without sudo to solve this issue

$ pip install --user scikit-image h5py keras pygame
Collecting scikit-image
  Downloading http://mirrors.aliyun.com/pypi/packages/65/69/27a1d55ce8f77c8ac757938707105b1070ff4f2ae47d2dc99461bfae4491/scikit_image-0.13.0-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (28.1MB)
    100% |████████████████████████████████| 28.1MB 380kB/s
Collecting h5py
  Downloading http://mirrors.aliyun.com/pypi/packages/b7/cc/1c29b0815b12de2c92b5323cad60f724ac8f0e39d0166d0b9dfacbcb70dd/h5py-2.7.0-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (4.5MB)
    100% |████████████████████████████████| 4.5MB 503kB/s
Requirement already satisfied: keras in /Library/Python/2.7/site-packages
Requirement already satisfied: pygame in /Library/Python/2.7/site-packages
Requirement already satisfied: matplotlib>=1.3.1 in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from scikit-image)
Requirement already satisfied: six>=1.7.3 in /Library/Python/2.7/site-packages (from scikit-image)
Requirement already satisfied: pillow>=2.1.0 in /Library/Python/2.7/site-packages (from scikit-image)
Requirement already satisfied: networkx>=1.8 in /Library/Python/2.7/site-packages (from scikit-image)
Requirement already satisfied: PyWavelets>=0.4.0 in /Library/Python/2.7/site-packages (from scikit-image)
Collecting scipy>=0.17.0 (from scikit-image)
  Downloading http://mirrors.aliyun.com/pypi/packages/72/eb/d398b9f63ee936575edc62520477d6c2353ed013bacd656bd0c8bc1d0fa7/scipy-0.19.0-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (16.2MB)
    100% |████████████████████████████████| 16.2MB 990kB/s
Requirement already satisfied: numpy>=1.7 in /Library/Python/2.7/site-packages (from h5py)
Requirement already satisfied: theano in /Library/Python/2.7/site-packages (from keras)
Requirement already satisfied: pyyaml in /Library/Python/2.7/site-packages (from keras)
Requirement already satisfied: python-dateutil in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from matplotlib>=1.3.1->scikit-image)
Requirement already satisfied: tornado in /Library/Python/2.7/site-packages (from matplotlib>=1.3.1->scikit-image)
Requirement already satisfied: pyparsing>=1.5.6 in /Users/qiuwei/Library/Python/2.7/lib/python/site-packages (from matplotlib>=1.3.1->scikit-image)
Requirement already satisfied: nose in /Library/Python/2.7/site-packages (from matplotlib>=1.3.1->scikit-image)
Requirement already satisfied: olefile in /Library/Python/2.7/site-packages (from pillow>=2.1.0->scikit-image)
Requirement already satisfied: decorator>=3.4.0 in /Library/Python/2.7/site-packages (from networkx>=1.8->scikit-image)
Requirement already satisfied: singledispatch in /Library/Python/2.7/site-packages (from tornado->matplotlib>=1.3.1->scikit-image)
Requirement already satisfied: certifi in /Library/Python/2.7/site-packages (from tornado->matplotlib>=1.3.1->scikit-image)
Requirement already satisfied: backports_abc>=0.4 in /Library/Python/2.7/site-packages (from tornado->matplotlib>=1.3.1->scikit-image)
Installing collected packages: scipy, scikit-image, h5py
Successfully installed h5py-2.7.0 scikit-image-0.13.0 scipy-0.19.0 

Hope it will help someone who encounter similar issue!

GoingMyWay
  • 16,802
  • 32
  • 96
  • 149
  • 4
    You shouldn't use `sudo` with `--user`. What `--user` does is install into `~/Library` rather than `/Library` You don't have to use `sudo` to install there. But once you install with `sudo`, the directories and files are all owned by `root`. Then you have to use `sudo` to do anything else to them. Which in turn will require you to use `sudo` for other things you shouldn't use it for. Then even an honest mistake can go from throwing an error, to destroying data. – Bruno Bronosky Nov 03 '17 at 18:43
10

TL;DR $PATH fix

  1. Use pip install --user package_name to install a package that should include CLI executables.
  2. Launch a python shell and import package_name
  3. Find where lib/python/... occurs in the output and replace it all with bin
  4. It's likely to be $HOME/Library/Python/2.7/bin

Details

Because of the new System Integrity Protection in macOS 10.11 El Capitan, you can no longer sudo pip install. We won't debate the merits of that here.

Another answer explains that you should pip install --user which is correct. But they sent you to the back alleys to figure out what to do about your $PATH so that you could get access to installed executables. Luckily, I've already solved a similar need for an unrelated question.

Here is a transcript of how I solved the problem on one of my systems. I'm including it all rather just than the $PATH that worked for me, because your system may be different from mine. This process should work for everybody.

$ pip install --user jp
Collecting jp
  Downloading jp-0.2.4.tar.gz
Installing collected packages: jp
  Running setup.py install for jp ... done
Successfully installed jp-0.2.4

$ python -c 'import jp; print jp'
<module 'jp' from '/Users/bbronosky/Library/Python/2.7/lib/python/site-packages/jp/__init__.pyc'>

$ find /Users/bbronosky/Library/Python -type f -perm -100
/Users/bbronosky/Library/Python/2.7/bin/jp

$ which jp

$ echo -e '\n''export PATH=$HOME/Library/Python/2.7/bin:$PATH' >> ~/.bashrc

$ bash # starting a new bash process for demo, but you should open a new terminal

$ which jp
/Users/bbronosky/Library/Python/2.7/bin/jp

$ jp
usage: jp <expression> <filepath>
Bruno Bronosky
  • 66,273
  • 12
  • 162
  • 149
9

I had the same issues. As others have mentioned, don't run pip install with sudo. Run

brew doctor 

and fix the warnings and you should be able to proceed with your pip install.

apadana
  • 13,456
  • 15
  • 82
  • 98
6

It is hard to get pip working on El Capitan for several reasons:

  1. OS X doesn't set some distutils variables correctly, so pip tries to install ancillary files in locations under /System/Library/. El Capitan blocks this, which is the error you are running into.
  2. OS X includes a number of outdated packages under /System/Library/. pip often wants to upgrade these but cannot on El Capitan.
  3. OS X places /System/Library/ higher in the python search order than /Library/Python/2.7/site-packages (the system-wide python package location), so even if you manage to install newer versions of some packages, the old ones still get loaded, breaking some dependencies.

There are workarounds for all of these at https://apple.stackexchange.com/a/223163/143849 . But you may be best off installing your own version of Python via the standard Python installer, Homebrew or Anaconda.

Community
  • 1
  • 1
Matthias Fripp
  • 17,670
  • 5
  • 28
  • 45
4

I guess you have some conflict with other package. For me it was six. So you need to use a command like this:

pip install google-api-python-client --upgrade --ignore-installed six

or

pip install --ignore-installed six

Ruhul Amin
  • 1,751
  • 15
  • 18
0

I fully agree with Mikko, but if you still want to do it, here is the way:

  • Restart in recovery mode (Hold cmd + R)
  • Open terminal from utilities
  • Use the command csrutil disable
wjandrea
  • 28,235
  • 9
  • 60
  • 81
gilliM
  • 41
  • 1
  • 6
    This could be dangerous advice without explaining the implications of disabling System Integrity Protection. One consideration is the expectation that it won't ever work properly again if you don't re-enable it relatively quickly, much like Windows UAC and Virtualization. – andy magoon Mar 27 '16 at 15:20
0

I have python2.7 installed via brew and the following solved my problem

brew install numpy

It installs python3, but it still works and sets it up for 2.7 as well.

ah bon
  • 9,293
  • 12
  • 65
  • 148
Aerodyno
  • 471
  • 3
  • 12