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 installpython3-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


[plain] view plain copy
  1. 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值