I have Python on my Ubuntu system, but gcc can't find Python.h

后端 未结 14 1725
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 13:10

I am on a school computer, so I can\'t install anything.

I am trying to create C code which can be run in Python. It seems all the articles I am finding on it requi

相关标签:
14条回答
  • 2020-11-27 13:24

    On ubuntu you can just type sudo apt-get install python-dev -y in terminal to install the python-dev package.

    0 讨论(0)
  • 2020-11-27 13:30

    On Ubuntu, you would need to install a package called python-dev. Since this package doesn't seem to be installed (locate Python.h didn't find anything) and you can't install it system-wide yourself, we need a different solution.

    You can install Python in your home directory -- you don't need any special permissions to do this. If you are allowed to use a web browser and run a gcc, this should work for you. To this end

    1. Download the source tarball.

    2. Unzip with

      tar xjf Python-2.7.2.tar.bz2
      
    3. Build and install with

      cd Python-2.7.2
      ./configure --prefix=/home/username/python --enable-unicode=ucs4
      make
      make install
      

    Now, you have a complete Python installation in your home directory. Pass -I /home/username/python/include to gcc when compiling to make it aware of Python.h. Pass -L /home/username/python/lib and -lpython2.7 when linking.

    0 讨论(0)
  • 2020-11-27 13:31

    The header files are now provided by libpython2.7-dev.

    You can use the search form at packages.ubuntu.com to find out what package provides Python.h.

    0 讨论(0)
  • 2020-11-27 13:32

    It happens because Python.h is not located in the default include folder (which is /usr/include/ ).

    Installing Python-dev might help:

    $ sudo apt-get install python-dev 
    

    But mostly the problem will persist because the development packages are made inside a separate folder inside the include folder itself ( /usr/include/python2.7 or python3).

    So you should either specify the library folder using -I option in gcc or by creating soft-links to everything inside those folders to just outside (I'd prefer the former option).

    Using -I option in gcc:

    $ gcc -o hello -I /usr/include/python2.7 helloworld.c
    

    Creating soft-links :

    $ sudo ln -sv /usr/include/python2.7/* /usr/include/
    
    0 讨论(0)
  • 2020-11-27 13:35

    Go to Synaptic package manager. Reload -> Search for python -> select the python package you want -> Submit -> Install Works for me ;)

    Exactly, the package you need to install is python-dev.

    0 讨论(0)
  • 2020-11-27 13:36
    locate Python.h
    

    If the output is empty, then find your python version

    python --version
    

    lets say it is X.x i.e 2.7 or 3.6, 3.7, 3.8 Then with the same version install header files and static libraries for python

    sudo apt-get install pythonX.x-dev
    
    
    0 讨论(0)
提交回复
热议问题