关于macos:Mac OS X El Capitan – Scrapy / Python ImportError:无法导入名称xmlrpc_client

Mac OS X El Capitan - Scrapy/Python ImportError: cannot import name xmlrpc_client

我想在Mac OS X El Capitan上使用Scrapy。我已经安装了zsh,我已经尝试了所有可以在网上找到的方法来解决这个问题。我还查看了scrappy throws importError:无法导入名称xmlrpc_客户端,无法解决我的问题!

通过BREW安装python并添加"pip install scrapy":

1
2
?  DriverEBV  which python
/usr/local/bin/python

my.zshrc有以下行:

1
2
export PATH=/usr/local/bin:$PATH
export PYTHONPATH="/Library/Python/2.7/site-packages"

这是我得到的错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
?  DriverEBV  scrapy runspider DriverEBV.py
Traceback (most recent call last):
  File"/usr/local/bin/scrapy", line 7, in <module>
    from scrapy.cmdline import execute
  File"/Library/Python/2.7/site-packages/scrapy/__init__.py", line 48, in <module>
    from scrapy.spiders import Spider
  File"/Library/Python/2.7/site-packages/scrapy/spiders/__init__.py", line 10, in <module>
    from scrapy.http import Request
  File"/Library/Python/2.7/site-packages/scrapy/http/__init__.py", line 12, in <module>
    from scrapy.http.request.rpc import XmlRpcRequest
  File"/Library/Python/2.7/site-packages/scrapy/http/request/rpc.py", line 7, in <module>
    from six.moves import xmlrpc_client as xmlrpclib
ImportError: cannot import name xmlrpc_client

当我运行"pip install scrapy"时,我看到的是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
?  DriverEBV  pip install scrapy
Requirement already satisfied (use --upgrade to upgrade): scrapy in /Library/Python/2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): cssselect>=0.9 in /Library/Python/2.7/site-packages (from scrapy)
Requirement already satisfied (use --upgrade to upgrade): queuelib in /usr/local/lib/python2.7/site-packages (from scrapy)
Requirement already satisfied (use --upgrade to upgrade): pyOpenSSL in /usr/local/lib/python2.7/site-packages (from scrapy)
Requirement already satisfied (use --upgrade to upgrade): w3lib>=1.8.0 in /Library/Python/2.7/site-packages (from scrapy)
Requirement already satisfied (use --upgrade to upgrade): lxml in /Library/Python/2.7/site-packages (from scrapy)
Requirement already satisfied (use --upgrade to upgrade): Twisted>=10.0.0 in /Library/Python/2.7/site-packages (from scrapy)
Requirement already satisfied (use --upgrade to upgrade): six>=1.5.2 in /usr/local/lib/python2.7/site-packages (from scrapy)
Requirement already satisfied (use --upgrade to upgrade): service-identity in /Library/Python/2.7/site-packages (from scrapy)
Requirement already satisfied (use --upgrade to upgrade): cryptography>=0.7 in /usr/local/lib/python2.7/site-packages (from pyOpenSSL->scrapy)
Requirement already satisfied (use --upgrade to upgrade): zope.interface>=3.6.0 in /usr/local/lib/python2.7/site-packages (from Twisted>=10.0.0->scrapy)
Requirement already satisfied (use --upgrade to upgrade): characteristic>=14.0.0 in /Library/Python/2.7/site-packages (from service-identity->scrapy)
Requirement already satisfied (use --upgrade to upgrade): pyasn1-modules in /Library/Python/2.7/site-packages (from service-identity->scrapy)
Requirement already satisfied (use --upgrade to upgrade): pyasn1 in /Library/Python/2.7/site-packages (from service-identity->scrapy)
Requirement already satisfied (use --upgrade to upgrade): setuptools in /usr/local/lib/python2.7/site-packages (from cryptography>=0.7->pyOpenSSL->scrapy)
Requirement already satisfied (use --upgrade to upgrade): enum34 in /usr/local/lib/python2.7/site-packages (from cryptography>=0.7->pyOpenSSL->scrapy)
Requirement already satisfied (use --upgrade to upgrade): ipaddress in /usr/local/lib/python2.7/site-packages (from cryptography>=0.7->pyOpenSSL->scrapy)
Requirement already satisfied (use --upgrade to upgrade): idna>=2.0 in /usr/local/lib/python2.7/site-packages (from cryptography>=0.7->pyOpenSSL->scrapy)
Requirement already satisfied (use --upgrade to upgrade): cffi>=1.1.0 in /usr/local/lib/python2.7/site-packages (from cryptography>=0.7->pyOpenSSL->scrapy)
Requirement already satisfied (use --upgrade to upgrade): pycparser in /usr/local/lib/python2.7/site-packages (from cffi>=1.1.0->cryptography>=0.7->pyOpenSSL->scrape)

有人能帮我吗?


对于安装在/Library/Python目录中的mac os x系统python库,我除了痛苦之外什么都没有。对我来说很好的是MacPorts和VirtualEnv的结合:

  • 安装MacPorts

  • 从MacPorts安装python、pip和virtualenv:

    1
    2
    3
    /opt/local/bin/port install python27
    /opt/local/bin/port install py27-pip
    /opt/local/bin/port install py27-virtualenv
  • 设置虚拟环境:

    1
    /opt/local/bin/virtualenv-2.7 myenv
  • 激活virtualenv(不要忘记点!)

    1
    . myenv/bin/activate
  • 安装刮削

    1
    pip install scrapy
  • 这样,系统python库就不会受到影响,您可以安装任何您喜欢的包,而无需删除或升级现有的包。


    帮助我的是卸载Six和Scrapy,然后重新安装:

    1
    2
    3
    4
    5
    pip uninstall six
    pip uninstall scrapy

    pip install six
    pip install scrapy

    如有必要,使用sudo运行。

    或者,您也可以尝试升级Six和Scrapy:

    1
    2
    pip install --upgrade scrapy
    pip install --upgrade six


    尝试通过pip卸载,然后使用easy_install命令重新安装。我在另一个python模块上也遇到了同样的问题,用这种方法解决了mac os x el capitan上的问题。


    我认为OSX上最好的解决方案应该是"不要使用系统python"。它会让生活更轻松。此链接显示如何执行此操作。

    There’s a known issue that prevents pip from updating system packages. This has to be addressed to successfully install Scrapy and its dependencies. Here are some proposed solutions:

    (Recommended) Don’t use system python, install a new, updated version that doesn’t conflict with the rest of your system. Here’s how to do it using the homebrew package manager:

  • Install homebrew following the instructions in http://brew.sh/
  • Update your PATH variable to state that homebrew packages should be used before system packages (Change .bashrc to .zshrc accordantly if you’re using zsh as default shell):
  • echo"export PATH=/usr/local/bin:/usr/local/sbin:$PATH">> ~/.bashrc

  • Reload .bashrc to ensure the changes have taken place:
  • source ~/.bashrc

  • Install python:
  • brew install python

  • Latest versions of python have pip bundled with them so you won’t need to install it separately. If this is not the case, upgrade python:
  • brew update; brew upgrade python