- How to fix the “Python.h: No such file or directory” error
- How to fix the error “Python.h: No such file or directory”?
- Reason 1: Bugged or missing Python-dev library
- Solution
- Reason 2: Library path not located
- Solution
- Conclusion
- Ubuntu include python h
- # Fatal error: Python.h: No such file or directory
- # Installing python3-dev for a specific Python version
- # Locate your Python header files
- # Make sure the specified path is correct
- # Check if your Python version is supported by the package
- # Try running pip install in verbose mode
- # Conclusion
- Решение проблемы с ошибкой «fatal error: Python.h: Нет такого файла или каталога»
- Для apt (Ubuntu, Debian, Kali Linux, Linux Mint…):
- Для yum (CentOS, RHEL…):
- Для dnf (Fedora…):
- Для zypper (openSUSE…):
- Связанные статьи:
How to fix the “Python.h: No such file or directory” error
When writing a program using a certain language you might come across the need to import libraries or extensions from various languages. One of these libraries is the “Python.h” library. It is a header file that is commonly added to C or C++ code which helps to embed Python code into the other language.
Running this header file in the code is a little tricky and can lead to the error “Python.h: No such file or directory”. This article will elaborate on the causes behind this error and what we can do to fix it.
How to fix the error “Python.h: No such file or directory”?
Including extensions from another language is slightly complex. There are various different reasons that can invoke the error mentioned above. Let’s have a look at some of the major causes and their solutions.
Reason 1: Bugged or missing Python-dev library
The most obvious reason that is invoking this error refers to a fault in the header files that you have installed on your system. It is likely that they are not working or have become corrupted. When this happens, your code cannot locate the library on your system and it will prompt the “Python.h: No such file or directory” error.
Solution
The solution to the issue stated above is straightforward. If the Python-dev library is bugged or missing, reinstalling it on your system will easily fix the issue. You can choose one of the following methods to install the Python-dev library as per your system:
For Ubuntu/Debian
Run this statement in your terminal to install the library on Ubuntu/Debian system:
$ sudo apt install python3-dev
Use this command to install the library on your Fedora system:
$ sudo dnf install python3-devel
Use this command to install the library on your CentOS system:
$ sudo yum install python3-devel
Reason 2: Library path not located
Another reason that will cause this error to pop up is that the program is not able to locate the header file with its correct path. When this happens, the code throws a “Python.h: No such file or directory” problem. Take a sample code as shown below in the image:
Use the below-mentioned command to run the code:
This command will compile the code but will be unable to locate the python library and resulting in the error shown below:
Solution
To solve the issue of locating the Python.h file, it is important to let the compiler know where Python is located. This can be done using these steps.
Step 1: Find the location of Python.h
To find the location of your Python file uses the following command:
Step 2: Compile using the location
Once you know the location, use it to execute this command:
$ gcc -I /usr/include/python3.10/ sample.c
Where sample.c is your file and the rest is your path. Through these steps, your compiler will be able to locate the library and successfully compile your program.
Conclusion
The two main causes for the “Python.h: No such file or directory” error are missing the library and the compiler being unable to locate the library. To solve them you can either install the Python-dev onto your system or you can help the compiler in locating the library from the system.
TUTORIALS ON LINUX, PROGRAMMING & TECHNOLOGY
Ubuntu include python h
Last updated: Feb 20, 2023
Reading time · 5 min
# Fatal error: Python.h: No such file or directory
To solve the «Fatal error: Python.h: No such file or directory», install the header files and the static library for your version of Python by adding the python-dev package system-wide.
Copied!error: command 'gcc' failed with exit status 1 fatal error: Python.h: No such file or directory #include "Python.h" ^ compilation terminated.
Open your terminal and run the command that is suitable for your operating system and package manager.
Copied!# 👇️ 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 Fedora sudo dnf install python2-devel sudo dnf install python3-devel # 👇️ 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
Installing the header files and the static library for your version of Python should be sufficient as it is not recommended to edit python.h files directly.
# Installing python3-dev for a specific Python version
If that didn’t help, you have to install python3-dev for your specific version of Python.
Use the python —version command to get your version of Python first.
For example, my Python version is 3.11, so I’d scope the python-dev package to Python 3.11 .
Copied!# python3.11 sudo apt-get install python3.11-dev build-essential
If your Python version is 3.10, you’d scope the python-dev package to Python 3.10 .
Copied!# python3.10 sudo apt-get install python3.10-dev build-essential
If your Python version is 3.8, you’d install python3.8-dev .
Copied!# python3.8 sudo apt-get install python3.8-dev build-essential
The python3-dev package includes header files and a static library for Python (v3.X).
Installing the header files and the python-dev library should resolve the error without you having to edit python.h files directly.
# Locate your Python header files
If the error is not resolved, run the following command.
Copied!sudo find / -iname 'Python.h'
The command will try to locate the Python headers.
The output will look something like this.
Copied!/usr/include/python3.7/Python.h /usr/include/python3.8/Python.h /home/borislav/anaconda3/include/python3.7m/Python.h /home/linuxbrew/.linuxbrew/Cellar/python@3.8/3.8.13_1/include/python3.8/Python.h
Make sure to install the python-dev files for the specific versions of Python, e.g. 3.7 and 3.8 in the example.
Copied!# 👇️ python3.7 sudo apt-get install python3.7-dev build-essential # 👇️ python3.8 sudo apt-get install python3.8-dev build-essential
# Make sure the specified path is correct
The error also occurs when the path to the Python.h file cannot be found on your machine.
Copied!gcc -o output example_file.c
Make sure you haven’t misspelled the name of the file in the command.
For example, if you have a main.c file with the following contents.
Copied!#include int main() printf("Hello World!"); return 0; >
Install the locate command if you don’t already have it.
Copied!# 👇️ for Debian/Ubuntu sudo apt install mlocate # 👇️ for CentOS/RHEL sudo yum install mlocate
Run the command to find the Python.h executable.
The output of the command will contain a path to the Python.h file, e.g. /usr/include/python3.11/Python.h .
If the file is not found, then you haven’t installed python-dev as shown in the previous subheading.
If the path is /usr/include/python3.11/Python.h , issue the following command.
Copied!gcc -I/usr/include/python3.11 main.c
Notice that we aren’t specifying the path to the Python.h file, but to the directory that contains it.
- The directory in which your Python.h file is /usr/include/python3.11 .
- The file you are working with is called main.c .
The path will likely be different in your case, so make sure to update the path with the output from the locate Python.h command.
Make sure to specify the path to the directory that contains your Python.h file, not the complete path to the file.
Copied!gcc -I/usr/include/python3.11 main.c
My Python.h file is located in the /usr/include/python3.11 directory.
# Check if your Python version is supported by the package
Google for the name of the package you’re trying to install and check if your Python version is supported by the package.
For example, if I google «requests pypi» and click on the pypi.org page, I can see the supported Python versions in the sidebar on the left, under Meta > Requires .
The screenshot shows that the package supports Python 3.7+.
If your Python version doesn’t meet the requirements, the «Fatal error: Python.h: No such file or directory» occurs.
If the package doesn’t support the latest version of Python, try running the pip install command with the —pre option.
Copied!pip install requests --pre pip3 install requests --pre python -m pip install requests --pre python3 -m pip install requests --pre py -m pip install requests --pre
The —pre option makes it so pip includes pre-release and development versions of the package. By default pip only finds stable versions.
If that doesn’t work, you have to install a Python version that is in the specified range and then run the pip install command.
You can upgrade your Python version by downloading the installer from the official python.org website and running it.
Make sure to tick the following options if you get prompted:
- Install launcher for all users (recommended)
- Add Python to PATH (this adds Python to your PATH environment variable)
You can download a specific Python version that is supported by the package if the package doesn’t support the latest Python version.
Different versions are available in the «Looking for a specific release» table.
# Try running pip install in verbose mode
If none of the suggestions helped, try running the pip install command in verbose mode.
Copied!pip install requests -vvv pip3 install requests -vvv python -m pip install requests -vvv
The -v option stands for verbose mode and can be used up to 3 times.
When the pip install command is run in verbose mode, the command shows more output and how the error occurred.
# Conclusion
To solve the «Fatal error: Python.h: No such file or directory»:
- Install the header files and the static library for your version of Python.
- Make sure your Python version is supported by the package you’re trying to install.
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
Решение проблемы с ошибкой «fatal error: Python.h: Нет такого файла или каталога»
Если при компиляции программы вы получаете ошибку, что отсутствует файл Python.h, то необходимо установить дополнительный пакет.
Вам нужно обратить внимание, какая версия Python используется для компиляции программы: 2.x или 3.x. Файлы заголовков помещены в различные пакеты для этих версий, поэтому вам нужно установить правильный пакет, в соответствии с используемой при компиляции версией Python. В большинстве популярных дистрибутивов требуемый пакет имеется в стандартном репозитории, поэтому установка выполняется в одну команду.
Текст ошибки может чуть различаться, в зависимости от того, в каком файле она возникла. Примеры сообщений:
url/url.cpp:4:10: fatal error: Python.h: Нет такого файла или каталога #include "Python.h" ^~~~~~~~~~ compilation terminated. error: Setup script exited with error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
utilsmodule.c:1:20: fatal error: Python.h: No such file or directory compilation terminated.
Самой вероятной причиной этого является то, что не установлены файлы заголовка и статичных библиотек для python dev. Для установки их на системном уровне используйте менеджер пакетов для вашего дистрибутива.
Для apt (Ubuntu, Debian, Kali Linux, Linux Mint…):
Если программа компилируется для python2.x, то выполните команду:
sudo apt install python-dev
Если программа компилируется для python3.x, то выполните команду:
sudo apt install python3-dev
Для yum (CentOS, RHEL…):
sudo yum install python-devel
sudo yum install python34-devel
Если вам нужно установить для других версий Python, то замените цифры на нужные, например:
sudo yum install python36u-devel
Для dnf (Fedora…):
sudo dnf install python2-devel
sudo dnf install python3-devel
Для zypper (openSUSE…):
sudo zypper in python-devel
sudo zypper in python3-devel
Если после установки заголовков проблема не исчезла, то возможно, что вы выбрали неверную версию Python.