Mac下pip install Permission denied解决方法

in cn •  7 years ago  (edited)

pip install出现 Permission denied 权限问题,在网上找到以下几种方法:

  • brew install Python
    Chaim:electrum Chaim$ brew install Python
    Warning: python 2.7.14_3 is already installed, it's just not linked.
    You can use `brew link python` to link this version.

    Chaim:electrum Chaim$ brew link python
    Linking /usr/local/Cellar/python/2.7.14_3... 26 symlinks created


    版本不对,那安装Python3
    Chaim:electrum Chaim$ brew install Python3
    Updating Homebrew...
    Warning: python3 3.6.4_2 is already installed, it's just not linked.
    You can use `brew link python3` to link this version.

    Chaim:electrum Chaim$ brew link python3
    Linking /usr/local/Cellar/python3/3.6.4_2... 
    Error: Could not symlink bin/2to3-3.6
    Target /usr/local/bin/2to3-3.6
    already exists. You may want to remove it:
      rm '/usr/local/bin/2to3-3.6'

    To force the link and overwrite all conflicting files:
      brew link --overwrite python3

    To list all files that would be deleted:
      brew link --overwrite --dry-run python3

    Chaim:electrum Chaim$ brew link --overwrite python3
    Linking /usr/local/Cellar/python3/3.6.4_2... 19 symlinks created

还是出同样错误,可能我哪个地方没弄清楚...

  • pip install xxx –user
    Chaim:electrum Chaim$ pip3 install pyqt5-macos-built --user
    Collecting pyqt5-macos-built
      Using cached pyqt5-macos-built-5.5.0.tar.gz
    Requirement already satisfied: docopt in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from pyqt5-macos-built)
    Installing collected packages: pyqt5-macos-built
      Running setup.py install for pyqt5-macos-built ... done
    Successfully installed pyqt5-macos-built-5.5.0

    Chaim:electrum Chaim$ pyrcc5
    /usr/local/bin/python3.6: No module named PyQt5.pyrcc_main

Mac中自带python的package目录在/Library/Python/2.7/site-packages/,如果直接用pip install则默认是在此目录中,也即系统目录。
而–user的安装目录在/Users/Ussopo/Library/Python/2.7/lib/python/site-packages/,不触及SIP,因此install success。

网上说这种方法安装是成功,但调用可能会有问题,结果的确还是出同样错误,可能还是有哪个地方没弄清楚...

  • 关闭SIP

1、重启电脑,按住Command+R(直到出现苹果标志)进入Recovery Mode(恢复模式),左上角菜单里找到实用工具终端;

2、输入csrutil disable回车重启Mac即可,如果想重新启动SIP机制重复上述步骤改用csrutil enable即可。

这个方法以前好像试过,不想试了,还是保留系统的安全模式吧,只能用大家都推荐的pyenv了...

  • anaconda

这个网上有反应说千万别在mac上装,弄得乱七八糟,好吧...

  • pyenv

大家都说好的方法,希望能解决问题!

传送门 https://www.cnblogs.com/anpengapple/p/5907416.html

下载virtualenv,拷贝成2、3两个版本,并建立链接,如下:

Chaim:Downloads Chaim$ sudo cp -r virtualenv-15.1.0 /usr/local/virtualenv2
Chaim:Downloads Chaim$ sudo cp -r virtualenv-15.1.0 /usr/local/virtualenv3

编辑第一行最后加个“3”,启动python3
Chaim:Downloads Chaim$ sudo vi /usr/local/virtualenv3/virtualenv.py

Chaim:Downloads Chaim$ sudo ln -s /usr/local/virtualenv2/virtualenv.py /usr/sbin/virtualenv2
ln: /usr/sbin/virtualenv2: Operation not permitted

链接不允许建在/usr/sbin目录下,那就建到/usr/local/bin目录下吧
Chaim:Downloads Chaim$ sudo ln -s /usr/local/virtualenv2/virtualenv.py /usr/local/bin/virtualenv2
Chaim:Downloads Chaim$ sudo ln -s /usr/local/virtualenv3/virtualenv.py /usr/local/bin/virtualenv3

进入项目目录,执行如下命令,即切换到一个新的python环境:

Chaim:electrum Chaim$ virtualenv3 env
Using base prefix '/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/Chaim/Documents/workspace/electrum/env/bin/python3
Also creating executable in /Users/Chaim/Documents/workspace/electrum/env/bin/python
Installing setuptools, pip, wheel...done.

Chaim:electrum Chaim$ source env/bin/activate
(env) Chaim:electrum Chaim$ 
注意前面是env,切换到本项目的python环境了

用virtualenv的确好用,python不可不知啊!

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!