34

我正在尝试使用刮,但我有一个问题。

从 Six.moves 导入 xmlrpc_client 作为 xmlrpclib

ImportError:无法导入名称 xmlrpc_client

然后,我尝试了pip install --upgrade six scrape,但是:

  Found existing installation: six 1.4.1
    DEPRECATION: Uninstalling a distutils installed project (six) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project.
Uninstalling six-1.4.1:
Exception:
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/pip/basecommand.py", line 211, in main
status = self.run(options, args)
  File "/Library/Python/2.7/site-packages/pip/commands/install.py", line 311, in run
root=options.root_path,
  File "/Library/Python/2.7/site-packages/pip/req/req_set.py", line 640, in install
requirement.uninstall(auto_confirm=True)
  File "/Library/Python/2.7/site-packages/pip/req/req_install.py", line 716, in uninstall
paths_to_remove.remove(auto_confirm)
  File "/Library/Python/2.7/site-packages/pip/req/req_uninstall.py", line 125, in remove
renames(path, new_path)
  File "/Library/Python/2.7/site-packages/pip/utils/__init__.py", line 315, in renames
shutil.move(old, new)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 302, in move
copy2(src, real_dst)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 131, in copy2
copystat(src, dst)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 103, in copystat
os.chflags(dst, st.st_flags)

OSError: [Errno 1] Operation not permitted: '/var/folders/3h/r_2cxlvd1sjgzfgs4xckc__c0000gn/T/pip-5h86J8-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six-1.4.1-py2.7.egg-info'
4

4 回答 4

52

快速解决:

我刚刚解决了我认为同样的问题。你可以考虑试试这个(sudo,如有必要):

pip install scrape --upgrade --ignore-installed six

Github最终是我得到这个答案的地方(如果这个不能解决你的问题,你可以考虑更多的建议)。这似乎也是一个 El Capitan 问题。

此外,这在技术上可能是重复的。但是另一篇文章给出的答案是安装自己的 Python,而不是依赖默认的 osx Python,这让我觉得更费力。


更长,真正的修复:

最终我遇到了一种情况,我实际上需要升级six才能安装其他一些库,此时忽略是不够的。对我来说升级六最简单的方法是不使用pip,而是手动.tar.gz六页面下载,解压,移动到新解压的六包目录,然后手动运行安装(sudo,如有必要):

python setup.py install

感谢这个答案的指导。

于 2015-11-08T21:13:21.767 回答
20

我不认为这是重复的,但实际上这个问题在 pip GitHub 存储库问题列表中讨论过

注意: 根据您要安装的软件包,您可能需要修改解决方案中引用的命令。在下面的例子中,我试图设置 virtualenv 和 virtualwrapper。如果您在安装 aws-cli 或任何其他软件包时失败,只需将其插入脚本

问题的核心与 Apple 与 El Capitan 一起提供的新 SIP 相关。更具体地说

OS X 10.11 的 python 保留了自己的六个副本,这是不可删除的,因为 Apple 对其 python 发行版进行了修改。1.4.1 不是最新的,1.10.0 是。它也出现在他们的 python 导入路径的早期,因此它通常会覆盖您安装的更高版本。

我建议现在使用不同的 python。Python.org,或通过 Homebrew 或 Anaconda Python 安装。

Ask Different Stack Exchange 上进行了非常详细的讨论,其中涵盖了自 El Capitan 最初版本以来如何识别、解决和发展 SIP 的问题。虽然我觉得它很吸引人,但您按照以下说明花费的时间比阅读它所花费的时间要少,所以我建议您在完成以下操作后查看它...

我在尝试升级 VirtualEnv 和 VirtualEnvWrapper 时遇到了完全相同的错误。在上面的线程上有几个建议,但最后最稳定的是

  1. 利用对 sudo OPTION 的内置支持来指定 HOME 环境变量
$ man sudo

  -H   The -H (HOME) option option sets the HOME environment variable 
        to the home directory of the target user (root by default) as specified 
        HOME environment variable depends on sudoers(5) settings.  By default, 
        sudo will set HOME if env_reset or always_set_home are set, or if
        set_home is set and the -s option is specified on the command line.
  1. 利用 pip 的选项强制升级并忽略任何预先存在的包
$ pip install --help | grep upgrade

  -U, --upgrade   Upgrade all specified packages to the newest available 
        version. This process is recursive regardless of whether a dependency 
        is already satisfied.

beejhuff@ignatius:~/mac_setup$ pip install --help | grep ignore-installed

  -I, --ignore-installed  Ignore the installed packages (reinstalling instead).

首先,我最初的尝试和错误:

$ sudo pip install virtualenv virtualenvwrapper

The directory '/Users/beejhuff/Library/Caches/pip/http' or its parent directory 
is not owned by the current user and the cache has been disabled. 
Please check the permissions and owner of that directory. If executing 
pip with sudo, you may want sudo's -H flag.

The directory '/Users/beejhuff/Library/Caches/pip' or its parent directory 
is not owned by the current user and caching wheels has been disabled. 
check the permissions and owner of that directory. If executing pip with 
sudo, you may want sudo's -H flag.

Collecting virtualenv
  Downloading virtualenv-15.0.0-py2.py3-none-any.whl (1.8MB)
    100% |████████████████████████████████| 1.8MB 335kB/s 
Collecting virtualenvwrapper
  Downloading virtualenvwrapper-4.7.1-py2.py3-none-any.whl
Collecting virtualenv-clone (from virtualenvwrapper)
  Downloading virtualenv-clone-0.2.6.tar.gz
Collecting stevedore (from virtualenvwrapper)
  Downloading stevedore-1.12.0-py2.py3-none-any.whl
Collecting pbr>=1.6 (from stevedore->virtualenvwrapper)
  Downloading pbr-1.8.1-py2.py3-none-any.whl (89kB)
    100% |████████████████████████████████| 92kB 362kB/s 
Collecting six>=1.9.0 (from stevedore->virtualenvwrapper)
  Downloading six-1.10.0-py2.py3-none-any.whl
Installing collected packages: virtualenv, virtualenv-clone, pbr, six, stevedore, virtualenvwrapper
  Running setup.py install for virtualenv-clone ... done
  Found existing installation: six 1.4.1
    DEPRECATION: Uninstalling a distutils installed project (six) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project.
    Uninstalling six-1.4.1:
Exception:
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/pip-8.1.0-py2.7.egg/pip/basecommand.py", line 209, in main
    status = self.run(options, args)
  File "/Library/Python/2.7/site-packages/pip-8.1.0-py2.7.egg/pip/commands/install.py", line 317, in run
    prefix=options.prefix_path,
  File "/Library/Python/2.7/site-packages/pip-8.1.0-py2.7.egg/pip/req/req_set.py", line 726, in install
    requirement.uninstall(auto_confirm=True)
  File "/Library/Python/2.7/site-packages/pip-8.1.0-py2.7.egg/pip/req/req_install.py", line 746, in uninstall
    paths_to_remove.remove(auto_confirm)
  File "/Library/Python/2.7/site-packages/pip-8.1.0-py2.7.egg/pip/req/req_uninstall.py", line 115, in remove
    renames(path, new_path)
  File "/Library/Python/2.7/site-packages/pip-8.1.0-py2.7.egg/pip/utils/__init__.py", line 267, in renames
    shutil.move(old, new)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 302, in move
    copy2(src, real_dst)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 131, in copy2
    copystat(src, dst)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 103, in copystat
    os.chflags(dst, st.st_flags)
OSError: [Errno 1] Operation not permitted: '/tmp/pip-GQL8Gi-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six-1.4.1-py2.7.egg-info'

解决方案

它需要以三种特定方式修改我的安装命令:

  1. 我不得不将-H标志添加到sudo
  2. 我必须在要--upgrade升级的软件包名称之后添加选项(virtualenv
  3. 我必须使用--ignore-installed标志并指定six包是要忽略的包。
  4. *注意:通用命令是$ sudo -H pip install <packagename> --upgrade --ignore-installed six- 替换<packagename>为您需要安装的特定包`

最终工作示例

第一次升级 virtualenv

$ sudo -H pip install virtualenv --upgrade --ignore-installed six

Password:

Collecting virtualenv
  Using cached virtualenv-15.0.0-py2.py3-none-any.whl
Collecting six
  Using cached six-1.10.0-py2.py3-none-any.whl
Installing collected packages: virtualenv, six
Successfully installed six-1.4.1 virtualenv-15.0.0

第二次升级 virtualenvwrapper

$ sudo -H pip install virtualenvwrapper --upgrade --ignore-installed six

Password:

  Downloading virtualenvwrapper-4.7.1-py2.py3-none-any.whl
Collecting six
  Downloading six-1.10.0-py2.py3-none-any.whl
Collecting virtualenv (from virtualenvwrapper)
  Downloading virtualenv-15.0.0-py2.py3-none-any.whl (1.8MB)
    100% |████████████████████████████████| 1.8MB 751kB/s 
Collecting virtualenv-clone (from virtualenvwrapper)
  Downloading virtualenv-clone-0.2.6.tar.gz
Collecting stevedore (from virtualenvwrapper)
  Downloading stevedore-1.12.0-py2.py3-none-any.whl
Collecting pbr>=1.6 (from stevedore->virtualenvwrapper)
  Downloading pbr-1.8.1-py2.py3-none-any.whl (89kB)
    100% |████████████████████████████████| 92kB 417kB/s 
Installing collected packages: virtualenv, virtualenv-clone, pbr, six, stevedore, virtualenvwrapper
  Running setup.py install for virtualenv-clone ... done
Successfully installed pbr-1.8.1 six-1.4.1 stevedore-1.12.0 virtualenv-15.0.0 virtualenv-clone-0.2.6 virtualenvwrapper-4.7.1
于 2016-03-15T21:11:05.447 回答
4

以前的两个答案都对我不起作用。最后,我从GitHub 获得了解决方案 aws/aws-cli,摘录如下。

在 OS X 上,如果您看到有关 El Capitan 中 distutils 附带的 6 版本的错误,请使用 --ignore-installed 选项:

$ sudo pip install awscli --ignore-installed six
于 2016-09-16T15:29:44.057 回答
1

这会工作!

sudo pip install awsebcli --upgrade --ignore-installed six
于 2017-08-03T14:50:58.787 回答