Skip to content

How to fix Fatal error: Python.h: No such file or directory?

  • by
  • 3 min read

Whether using an older language like C/C++ or something more modern like Python, you will have access to hundreds, if not thousands, of libraries that’ll make your life easier. However, as convenient as they are, they often cause errors that stump developers.

In this article, we’re talking about the “Fatal error: Python.h: No such file or directory” issue, its causes and what you can do to fix the problem. 


What causes this error?

The error is mainly triggered when your code fails to see the Python.h library. This can happen because of several reasons:

  • Header files and static libraries for Python dev haven’t been installed.
  • The included files might not have the same default include path. 
  • Incorrect import in the code. 

Also read: How to fix Javascript error: ipython is not defined?


How to fix this error?

Here are three fixes you can try out. 

Install the required Python-dev libraries

Open the terminal and use the following commands to install Python-dev based on your operating system.

# for Debian (Ubuntu)
sudo apt-get install python-dev build-essential # python2.x
sudo apt-get install python3-dev build-essential # python3.x

# for Redhat / CentOS
sudo yum install python-devel # python2.x
sudo yum install python3-devel # python3.x

# for Alpine Linux
sudo apk add python2-dev # python2.x
sudo apk add python3-dev # python3.x

# for openSUSE
sudo zypper in python-devel # python2.x
sudo zypper in python3-devel # python3.x

# for Cygwin
apt-cyg install python-devel # for python2.x
apt-cyg install python3-devel # for python3.x

# for Raspberry Pi
sudo apt-get install python3-dev #for python3.x

Keep in mind that simply installing python3-dev doesn’t necessarily cover all versions of Python on your machine. For example, if you’re running Python 3.8 on your machine, you might have to modify the command to sudo apt-get install python3.8-dev build-essential.

Also read: How to fix Error: That port is already in use?


Ensure Python version compatibility

Another thing to check for is whether or not the package you’re using support the version of Python you’re running. If you’re unsure, you can head to the PyPI website and look for your package, provided you used PyPi to download the package in the first place. 

Once there, check the Python version required for the package. If running an older version, update your Python installation and try again. 


Include the path and library

Finally, if you’ve got the right version of the library and Python installed, you can try including the path and library as an argument in the compile command itself. 

For Python 2:

-I/usr/include/python2.7 -lpython2.7

For Python 3:

-I/usr/include/python2.7 -lpython3.7

Also read: How to fix Attributeerror: bytes object has no attribute read?

>