在Ubuntu下写python代码的时候,有时会需要安装一些第三方的库,安装的时候提示:
SystemError: Cannot compile 'Python.h' . Perhaps you need to install python-dev.
然后按照提示sudo apt-get install Python -dev又出错,提示:
The following packages have unmet dependencies: python-dev : Depends: python2.7 -dev (>= 2.7 . 3 ) but it is not going to be installed E: Unable to correct problems, you have held broken packages.
于是我又sudo apt-get install python2.7-dev,显示依赖错误:
The following packages have unmet dependencies: python2.7 -dev : Depends: python2. 7 (= 2.7 . 3 - 0ubuntu3 ) but 2.7 . 3 - 0ubuntu3 . 1 is to be installed Depends: libpython2.7 (= 2.7 . 3 - 0ubuntu3 ) but 2.7 . 3 - 0ubuntu3 . 1 is to be installed
出现这个问题的原因主要是默认情况下,Ubuntu为python2.7-dev提供的软件源与python-2.7 2.7.3-0ubuntu3.1的源不一致, 需要更新软件源。
可以使用命令 apt-cache show python2.7 查看到你安装python-2.7 2.7.3-0ubuntu3.1是从源precise-updates/main安装的,而python2.7-dev默认是 从源precise/main安装的,因此安装python2.7-dev之前需要更新软件源。
使用如下的代码可以安装成功:
echo "deb http://us.archive.ubuntu.com/ubuntu/ precise-updates main restricted" | sudo tee -a /etc/apt/sources.list.d/precise-updates.list sudo apt-get update sudo apt-get install python2.7 -dev sudo apt-get install python-dev
如果这样不行的话,可以参考这个网站:http://askubuntu.com/questions/275861/problem-installing-python-dev