Linux fatal error python h no such file or directory

Fatal error: Python.h no such file or directory — but python-dev is already installed

I am trying to install mod_wsgi on Ubuntu. When running the «sudo make» command as described on the mod_wsgi website I receive the error:

src/server/wsgi_python.h:24:10: fatal error: Python.h: No such file or directory 

The mod_wsgi troubleshoot section and other posts on SO (1,2,3) say to install python-dev of the correct version. This is already done based on the below output. Python3 running 3.6.7 and apt-get saying python3-dev is on the newest version of 3.6.7.

$ python3 Python 3.6.7 (default, Oct 22 2018, 11:32:17) $ sudo apt-get install python3-dev . python3-dev is already the newest version (3.6.7-1~18.04). 0 upgraded, 0 newly installed, 0 to remove and 11 not upgraded. 

So it seems like the Python.h file is there but make can’t find it. I looked at this SO post (4) but I don’t think it applies to me as I am able to locate Python.h.

$ locate Python.h /usr/include/python3.6m/Python.h 

I am unsure where to go next with this. Python3.6-dev is installed and I can locate Python.h, but make cannot. I am using a venv that Pycharm created for me automatically and I have a hunch it’s involved in the problem but I’m not sure how to follow up on this hunch. I am running make on the same terminal as python3 and apt-get so they should be using same environment vars (is my understanding). Any suggestions from anyone on next steps?

Источник

Решение проблемы с ошибкой «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. Для установки их на системном уровне используйте менеджер пакетов для вашего дистрибутива.

Читайте также:  Astra linux общая папка windows

Для 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.

Связанные статьи:

Источник

Linux fatal error python h no such file or directory

Last updated: Feb 20, 2023
Reading time · 5 min

banner

# 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

install python3 dev

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.

get python version

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

install python dev specific version

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).

Читайте также:  Vhd to iso linux

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'

locate your python header files

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

python no such file or directory

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.

locate python h

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

find python h file

Notice that we aren’t specifying the path to the Python.h file, but to the directory that contains it.

  1. The directory in which your Python.h file is /usr/include/python3.11 .
  2. 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.

locate python h

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

find python h file

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 .

supported python versions by package

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.

install specific python version

# 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»:

  1. Install the header files and the static library for your version of Python.
  2. 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: No such file or directory (Unsolved with python-devel)

When trying to compile pycaffe on Ubuntu 16.04, with Anaconda 3.6 installed I get the following error:

python/caffe/_caffe.cpp:1:52: fatal error: Python.h: No such file or directory 
sudo apt-get install python3-dev sudo apt-get install python3.5-dev sudo apt-get install python3.6-dev 

But even after trying with each of those separately the same error occurs. The correct PATH seems to be added in .bashrc

export PATH="home/jdevezas/anaconda/bin:$PATH" 
# NOTE: this is required only if you will compile the python interface. # We need to be able to find Python.h and numpy/arrayobject.h. #PYTHON_INCLUDE := /usr/include/python2.7 \ /usr/lib/python2.7/dist-packages/numpy/core/include # Anaconda Python distribution is quite popular. Include path: # Verify anaconda location, sometimes it's in root. ANACONDA_HOME := /home/jdevezas/anaconda #PYTHON_INCLUDE := $(ANACONDA_HOME)/include \ # $(ANACONDA_HOME)/include/python2.7 \ # $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include # Uncomment to use Python 3 (default is Python 2) PYTHON_LIBRARIES := boost_python3 python3.5m PYTHON_INCLUDE := /usr/include/python3.5m \ /usr/lib/python3.5/dist-packages/numpy/core/include 

I have also tried with Python 2.7 with no results. I have checked the /usr/include/python3.5m and Python.h is there. Suggestions?

Источник

Оцените статью
Adblock
detector