您的位置:首页 > 编程语言 > Python开发

fatal error: Python.h: 没有那个文件或目录 解决方法

2017-12-26 10:01 513 查看
这几天安装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和scra
a556
py就成功了。

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: