fatal error: Python.h: 沒有那個文件或目錄 解決方法

這幾天安裝scrapy

先說下環境  虛擬機Ubuntu16.04   系統默認Python 2.7.12 ,系統還自帶3.5版本的,後來自己又裝了3.6版本。


在系統安裝過程中,總是到twisted時報錯,因爲是源碼安裝的,所以系統要進行編譯,但是找不到python.h文件!!!

報錯如下

creating build/temp.linux-x86_64-2.7/src
creating build/temp.linux-x86_64-2.7/src/twisted
creating build/temp.linux-x86_64-2.7/src/twisted/test
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/usr/include/python3.6m -c src/twisted/test/raiser.c -o build/temp.linux-x86_64-2.7/src/twisted/test/raiser.o
src/twisted/test/raiser.c:4:20: fatal error: Python.h: 沒有那個文件或目錄
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1


解決方法:

1、之前一直都是:sudo apt install python-dev  或者  sudo apt install python3-dev,結果都不成功,看了下原因:

1)

正在讀取軟件包列表... 完成
正在分析軟件包的依賴關係樹       
正在讀取狀態信息... 完成       
python-dev 已經是最新版 (2.7.11-1)。
升級了 0 個軟件包,新安裝了 0 個軟件包,要卸載 0 個軟件包,有 0 個軟件包未被升級。

2)

正在讀取軟件包列表... 完成
正在分析軟件包的依賴關係樹       
正在讀取狀態信息... 完成       
python3-dev 已經是最新版 (3.5.1-3)。
升級了 0 個軟件包,新安裝了 0 個軟件包,要卸載 0 個軟件包,有 0 個軟件包未被升級。

發現一個是2.7,一個是3.5

2、所以最後安裝3.6版本的python-dev就可以了。

sudo apt install python3.6-dev


3、再安裝Twisted和scrapy就成功了。

sudo pip install Twisted

sudo pip install scrapy


kainchow@ubuntu:/$ scrapy

Scrapy 1.4.0 - no active project


Usage:
  scrapy <command> [options] [args]


Available commands:
  bench         Run quick benchmark test
  fetch         Fetch a URL using the Scrapy downloader
  genspider     Generate new spider using pre-defined templates
  runspider     Run a self-contained spider (without creating a project)
  settings      Get settings values
  shell         Interactive scraping console
  startproject  Create new project
  version       Print Scrapy version
  view          Open URL in browser, as seen by Scrapy


  [ more ]      More commands available when run from project directory


Use "scrapy <command> -h" to see more info about a command


4、終端鍵入scrapy時可能還會報錯,例如:

import lxml.html
  File "/usr/lib/python3/dist-packages/lxml/html/__init__.py", line 54, in <module>
    from .. import etree
ImportError: cannot import name 'etree'

這說明lxml包未升級到最新版本,需要更新升級,當把所有scrapy的依賴包升級完,scrapy就可以正常使用了。

升級:sudo pip install --upgrade requests lxml

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章