fatal error: Python.h: No such file or directory

匿名 (未验证) 提交于 2019-12-03 01:14:02

问题:

I am trying to build a shared library using a C extension file but first I have to generate the output file using the command below:

gcc -Wall utilsmodule.c -o Utilc 

After executing the command, I get this error message:

utilsmodule.c:1:20: fatal error: Python.h: No such file or directory compilation terminated.

in fact I have tried all the suggested solutions over the internet but the problem still exists ... also I have no problem with Python.h. I managed to locate the file on my machine ... anybody has faced the same problem before??

回答1:

Looks like you haven't properly installed the header files and static libraries for python dev. Use your package manager to install them system-wide.

For apt (Ubuntu, Debian...):

sudo apt-get install python-dev   # for python2.x installs sudo apt-get install python3-dev  # for python3.x installs 

For yum (CentOS, RHEL...):

sudo yum install python-devel   # for python2.x installs sudo yum install python34-devel   # for python3.4 installs 

For dnf (Fedora...):

sudo dnf install python2-devel  # for python2.x installs sudo dnf install python3-devel  # for python3.x installs 

For zypper (openSUSE...):

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


回答2:

On Ubuntu, I was running Python 3 and I had to install

sudo apt-get install python3-dev 

If you want to use a version of Python that is not linked to python3, install the associated python3.x-dev package. For example:

sudo apt-get install python3.5-dev 


回答3:

Two things you have to do.

Install development package for Python, in case of Debian/Ubuntu/Mint it's done with command:

sudo apt-get install python-dev 

Second thing is that include files are not by default in the include path, nor is Python library linked with executable by default. You need to add these flags (replace Python's version accordingly):

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

In other words your compile command ought to be:

gcc -Wall -I/usr/include/python2.7 -lpython2.7  utilsmodule.c -o Utilc  


回答4:

If you are using a Raspberry Pi:

sudo apt-get install python-dev 


回答5:

on Fedora run this for Python 2:

sudo dnf install python2-devel 

and for Python 3:

sudo dnf install python3-devel 


回答6:

If you are using tox to run tests on multiple versions of Python, you may need to install the Python dev libraries for each version of Python you are testing on.

sudo apt-get install python2.6-dev  sudo apt-get install python2.7-dev  etc. 


回答7:

I would like to add also the solution for Cygwin:

You need to install the package python2-devel or python3-devel, depending on the Python version you're using.

You can quickly install it using the 32-bit or 64-bit setup.exe (depending on your installation) from Cygwin.com.

Example (modify setup.exe's filename and Python's major version if you need):

$ setup.exe -q --packages=python3-devel 

You can also check my other answer for a few more options to install Cygwin's packages from the command-line.



回答8:

In AWS API (centOS) its

yum install python27-devel 


回答9:

For me, changing it to this worked:

#include 

I found the file /usr/include/python2.7/Python.h, and since /usr/include is already in the include path, then python2.7/Python.h should be sufficient.

You could also add the include path from command line instead - gcc -I/usr/lib/python2.7 (thanks @erm3nda).



回答10:

AWS EC2 install running python34:

sudo yum install python34-devel



回答11:

Make sure that the Python dev files come with your OS.

You should not hard code the library and include paths. Instead, use pkg-config, which will output the correct options for your specific system:

$ pkg-config --cflags --libs python2 -I/usr/include/python2.7 -lpython2.7 

You may add it to your gcc line:

gcc $(pkg-config --cflags --libs python2) -Wall utilsmodule.c -o Utilc 


回答12:

In my case, what fixed it in Ubuntu was to install the packages libpython-all-dev (or libpython3-all-dev if you use Python 3).



回答13:

If you use a virtualenv with a 3.6 python (edge right now), be sure to install the matching python 3.6 dev sudo apt-get install python3.6-dev, otherwise executing sudo python3-dev will install the python dev 3.3.3-1, which won't solve the issue.



回答14:

I managed to solve this issue and generate the .so file in one command

gcc -shared -o UtilcS.so -fPIC -I/usr/include/python2.7 -lpython2.7  utilsmodule.c 


回答15:

try apt-file. It is difficult to remember the package name where the missing file resides. It is generic and useful for any package files.

For example:

root@ubuntu234:~/auto# apt-file search --regexp '/Python.h$' pypy-dev: /usr/lib/pypy/include/Python.h python2.7-dbg: /usr/include/python2.7_d/Python.h python2.7-dev: /usr/include/python2.7/Python.h python3.2-dbg: /usr/include/python3.2dmu/Python.h python3.2-dev: /usr/include/python3.2mu/Python.h root@ubuntu234:~/auto#  

Now you can make an expert guess as to which one to choose from.



回答16:

For the OpenSuse comrades out there:

sudo zypper install python3-devel 


回答17:

It's not the same situation, but it also works for me and now I can use SWIG with Python3.5:

I was trying to compile:

gcc -fPIC -c existe.c existe_wrap.c -I /usr/include/python3.5m/

With Python 2.7 works fine, not with my version 3.5:

existe_wrap.c:147:21: fatal error: Python.h: No existe el archivo o el directorio compilation terminated.

After run in my Ubuntu 16.04 installation:

sudo apt-get install python3-dev  # for python3.x installs 

Now I can compile without problems Python3.5:

gcc -fPIC -c existe.c existe_wrap.c -I /usr/include/python3.5m/ 


回答18:

For CentOS 7:

sudo yum install python36u-devel 

I followed the instructions here for installing python3.6 on several VMs: https://www.digitalocean.com/community/tutorials/how-to-install-python-3-and-set-up-a-local-programming-environment-on-centos-7 and was then able to build mod_wsgi and get it working with a python3.6 virtualenv



回答19:

This error occurred when I attempted to install ctds on CentOS 7 with Python3.6. I did all the tricks mentioned here including yum install python34-devel. The problem was Python.h was found in /usr/include/python3.4m but not in /usr/include/python3.6m. I tried to use --global-option to point to include dir (pip3.6 install --global-option=build_ext --global-option="--include-dirs=/usr/include/python3.4m" ctds). This resulted in a lpython3.6m not found when linking ctds.

Finally what worked was fixing the development environment for Python3.6 needs to correct with the include and libs.

yum -y install https://dl.iuscommunity.org/pub/ius/stable/CentOS/7/x86_64/python36u-libs-3.6.3-1.ius.centos7.x86_64.rpm 

Python.h needs to be in your include path for gcc. Whichever version of python is used, for example if it's 3.6, then it should be in /usr/include/python3.6m/Python.h typically.



回答20:

Sure python-dev or libpython-all-dev are the first thing to (apt )install, but if that doesn't help as was my case, I advice you to install the foreign Function Interface packages by sudo apt-get install libffi-dev and pip install cffi.

This should help out especially if you see the error as/from c/_cffi_backend.c:2:20: fatal error: Python.h: No such file or directory.



回答21:

This means that Python.h isn't in your compiler's default include paths. Have you installed it system-wide or locally? What's your OS?

You could use the -I flag to specify an additional directory where your compiler should look for headers. You will probably have to follow up with -L so that gcc can find the library you'll be linking with using -l.



回答22:

If you're using Python 3.6 on Amazon Linux (based on RHEL, but the RHEL answers given here didn't work):

sudo yum install python36-devel 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!