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 require you to use
#include <Python.h>
I do this, but when I compile it complains that there is no such file or directory.
The computer has Python (at least it has the python
command in the terminal, and we can run whatever Python code we want).
I typed in locate Python.h
in the terminal, but it found nothing.
I have two questions:
Can I write C code that I can call in Python without Python.h
?
Am I missing something, and the computer actually has Python.h
?
You encounter "Python. h: No such file or directory" error while trying to build a shared library using the file extension of another language ( e.g. 'C' ). If you are trying to build a shared library using the file extension of another language, you need to install the correct development version of Python.
If you are using the standard flavour of Linux, open up the bash shell and type the following phrase, export PATH=”$PATH:/usr/local/bin/python” and press Enter. If you have access to either sh or ksh shell, then open up the terminal and type the following, PATH=”$PATH:/usr/local/bin/python” and press Enter.
Running Python in UbuntuPython comes preinstalled on almost every Linux system and is available on official distribution repositories as well. If you still don't have Python installed on your computer, you can easily download it using Ubuntu's package manager.
Python.h is nothing but a header file. It is used by gcc to build applications. You need to install a package called python-dev. This package includes header files, a static library and development tools for building Python modules, extending the Python interpreter or embedding Python in applications.
You need the python-dev
package which contains Python.h
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
Download the source tarball.
Unzip with
tar xjf Python-2.7.2.tar.bz2
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With