45

I am using Ubuntu 12.04 and want to use python 3.4 side by side with python 2.7.

The installation of python 3.4 worked properly. However, I cannot install the numpy package for python 3 (and as a consequence I can't install scipy, pandas etc.).

Using

 sudo pip3 install numpy

spits out the following error:

File "numpy/core/setup.py", line 289, in check_types

"Cannot compile 'Python.h'. Perhaps you need to "\

SystemError: Cannot compile 'Python.h'. Perhaps you need to install python-dev|python-devel.

Btw, I already have python-dev installed.

Moreover, installing numpy via

 sudo apt-get install python-numpy

does not work either since I already installed numpy for python 2.7 and the installer responds that numpy is already up to date.

What can I do? Thanks!

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
SmCaterpillar
  • 6,683
  • 7
  • 42
  • 70

2 Answers2

86

You have not installed the Python 3 development package. Install python3.4-dev:

apt-get install python3.4-dev

The main package never includes the development headers; Debian (and by extension Ubuntu) package policy is to put those into a separate -dev package. To install numpy however, you need these files to be able to compile the extension.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 5
    For those arriving in 2015 with a similar query (as I did) need to change the above command to `apt-get install python3.5-dev`. – tschoppi Nov 27 '15 at 14:09
  • 3
    And to anyone in the future: adjust the command to fit your current Python version. Yes, Ubuntu will not read your mind or the calendar. :-P – Martijn Pieters Nov 27 '15 at 14:15
  • 4
    or you can just use `apt-get install python3-dev` and ubuntu WILL read your mind and install the correct point release for your version of Ubuntu. – Dave LeBlanc Sep 26 '16 at 13:38
  • 2
    @DaveLeBlanc: it depends on the Ubuntu release which version is 'current'. On 12.04 that's [Python 3.2](https://launchpad.net/ubuntu/precise/+package/python3-dev), not 3.4. – Martijn Pieters Sep 26 '16 at 13:43
  • 1
    Any idea how I could fix this error in MSYS2? I tried installing mingw-w64-x86_64-python3 and I'm getting this error when I try to install numpy using pip: `SystemError: Cannot compile 'Python.h'. Perhaps you need to install python-dev|python-devel` – Alex Eastman Jul 20 '20 at 19:40
  • @AlexEastman: sorry, no experience with MingW. If `mingw-w64-x86_64-python3` is a package, look for a `-devel` or `-dev` package to install the Python headers, I guess. – Martijn Pieters Jul 25 '20 at 16:05
4

Solved by incrementing the python-dev package until I hit the right one. May need to be incremented further in the future. Poor implementation by python developers.

sudo apt-get install python3.7-dev

Mi Po
  • 1,123
  • 2
  • 12
  • 21
  • 1
    You can run `sudo apt-get install python3-dev`, and it will install the dev package corresponding to your version of python3. – Nick ODell Jan 19 '21 at 22:08