2015-11-26 79 views
8

我想在Mac OS X El Capitan上使用Scrapy。我已經安裝了zsh,並且已經嘗試了所有我可以在網上找到的解決此問題的方法。我也看過Scrapy throws ImportError: cannot import name xmlrpc_client,無法解決我的問題!通過BREW安裝Mac OS X El Capitan - Scrapy/Python導入錯誤:無法導入名稱xmlrpc_client

Python和添加的 「PIP安裝scrapy」:

➜ DriverEBV which python 
/usr/local/bin/python 

我的.zshrc具有以下行:

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

這是錯誤我得到:

➜ 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」時,這就是我所看到的:

➜ 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) 

有人能幫助我嗎?

+0

[Scrapy引發ImportError:無法導入名稱xmlrpc \ _client]可能的重複(http://stackoverflow.com/questions/30964836/scrapy-throws-importerror-cannot-import-name-xmlrpc-client) – eLRuLL

+2

不幸的是不是一個副本。我已經嘗試了您發佈的文章中討論過的所有內容。所以我嘗試了幾乎所有可用的在線方式,從最少侵入性到最具侵入性。 –

+0

你爲什麼在這裏設置python路徑? – cel

回答

1

什麼幫助我是卸載六scrapy,然後重新安裝:如果有必要sudo

pip uninstall six 
pip uninstall scrapy 

pip install six 
pip install scrapy 

運行。


或者,你也可以嘗試升級六scrapy:

pip install --upgrade scrapy 
pip install --upgrade six 
+0

我認爲這解決了我在命令行上運行scrapy時遇到的問題。當在PyCharm中運行時,我得到這個錯誤。任何建議要解決? –

+0

@GuyDaher設置中的項目解釋器是否正確? (我相信在這個例子中它應該是默認的python安裝) – mkaran

2

我有什麼,但疼痛與安裝在/Library/Python目錄中的Mac OS X系統的Python庫搞混。什麼工作以及對我來說是MacPorts的和virtualenv中的組合:

  1. 安裝MacPorts

  2. 從MacPorts的安裝Python,畫中畫,和的virtualenv:

    /opt/local/bin/port install python27 
    /opt/local/bin/port install py27-pip 
    /opt/local/bin/port install py27-virtualenv 
    
  3. 設置的virtualenv:

    /opt/local/bin/virtualenv-2.7 myenv 
    
  4. 激活的virtualenv(不要忘了點!)

    . myenv/bin/activate 
    
  5. 安裝scrapy

    pip install scrapy 
    

這樣,系統的Python庫是原封不動,你可以安裝任何你喜歡的包而不必須刪除或升級現有的軟件包。

+0

我認爲這解決了我在命令行運行scrapy時遇到的問題。當在PyCharm中運行時,我得到這個錯誤。任何建議要解決? –

+0

PyCharm現在似乎支持virtualenv。您應該可以通過[Project Interpreter](https:// www [https://www.jetbrains.com/help/pycharm/2016.1/adding-existing-virtual-environment.html])[添加現有的virtualenv] .jetbrains.com /幫助/ pycharm/2016.1 /項目interpreter.html)。希望這可以幫助。 –

+0

是的,我發現發佈此評論後,我覺得。感謝上述信息,授予您賞金 –

-1

我相信OS X上的最佳解決方案應該是「不要使用系統python」。它會讓生活更輕鬆。 This link顯示如何做到這一點。

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:

  1. Install homebrew following the instructions in http://brew.sh/
  2. 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

  1. Reload .bashrc to ensure the changes have taken place:

source ~/.bashrc

  1. Install python:

brew install python

  1. 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

+0

您可以使用此方法安裝並導入「Scrapy」嗎? – cpburnz

+0

@cpburnz沒問題。它適用於我。 –

0

嘗試通過pip卸載,然後使用easy_install命令重新安裝。我對另一個python模塊也有同樣的問題,這樣做可以解決Mac OS X El Capitan中的問題。

相關問題