C language to see and remember Part 1 Call C language from Python (hello world)

C is useful for doing things that python can't do.

The GO language is more convenient and faster than the C language, but I'm addicted to it with 89 pointers. Because it is interfaced by value (by Value is basic and by reference is not good), index cannot be taken even if it is passed with a pointer.

--Speed up, etc. --Library --I want to control the experimental equipment. --I want to read and write IO directly. --The interface for C language is prepared, but not for Python.

Let's look at an example sentence based on the purpose of "seeing and remembering".

What you need is

1. GCC ** gcc (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0** 2. Python Python 3.7.4 3. Advanced editor like Visual studio code Version: 1.41.1 (system setup) OS: Windows_NT x64 10.0.18362

First step

Create a folder with a suitable name. Let's copy the program below!

> mkdir capi

** Call python test library **

capi.py


import myModule as capi #Give it a suitable name(I'm addicted to test
capi.hello()

** Library body **

capilib.c


#include <Python.h>
static PyObject* hello(PyObject* self, PyObject* args)
{
    printf("Hello World Tokyo\n");
    return Py_None;
}
//Definition of function name, specifications, etc.
static PyMethodDef myMethods[] = {
    { "hello", hello, METH_NOARGS, "Hello World"},
    { NULL }
};
// Module Definition struct
static struct PyModuleDef myModule = {
 PyModuleDef_HEAD_INIT,"myModule","C API Module",-1,myMethods
};
//Initialize the above structure
PyMODINIT_FUNC PyInit_myModule(void)
{
    return PyModule_Create(&myModule);
}

Library creation script

setup.py


from distutils.core import setup, Extension
setup(name = 'myModule', version = '1.0.0',  \
   ext_modules = [Extension('myModule', ['capilib.c'])])

Create a library.

python setup.py install
running install
running build
running build_ext
running install_lib
running install_egg_info
Removing C:\ProgramData\Anaconda3\Lib\site-packages\myModule-1.0.0-py3.7.egg-info
Writing C:\ProgramData\Anaconda3\Lib\site-packages\myModule-1.0.0-py3.7.egg-info

How to check the library

\capi>pip freeze
...
myModule==1.0.0
...

Measures against garbled characters

> chcp 65001

Execute

capi>python capi.py
Hello World Tokyo

Recommended Posts

C language to see and remember Part 1 Call C language from Python (hello world)
C language to see and remember Part 2 Call C language from Python (argument) string
C language to see and remember Part 4 Call C language from Python (argument) double
C language to see and remember Part 5 Call C language from Python (argument) Array
Go language to see and remember Part 8 Call GO language from Python
C language to see and remember Part 3 Call C language from Python (argument) c = a + b
Go language to see and remember Part 7 C language in GO language
Call c language from python (python.h)
Call C language functions from Python to exchange multidimensional arrays
[Python] How to call a c function from python (ctypes)
Call Matlab from Python to optimize
Call popcount from Ruby / Python / C #
Introduction to Protobuf-c (C language ⇔ Python)
Python to switch from another language
Introduction to Ansible Part 1'Hello World !!'
Call C / C ++ from Python on Mac
[Python] Hit Keras from TensorFlow and TensorFlow from c ++ to speed up execution
From building a Python environment for inexperienced people to Hello world
Call your own C language shared library from Python using ctypes
[It's not too late to learn Python from 2020] Part 3 Python Language Basic (1)
[Python] Web application from 0! Hands-on (2) -Hello World-
From Python to using MeCab (and CaboCha)
Python to remember only with hello, worlds
Porting and modifying doublet-solver from python2 to python3.
Socket communication by C language and Python
Flask tutorial (from installation to hello world)
Generate C language from S-expressions in Python
How to display Hello world in python
Hello World and face detection with OpenCV 4.3 + Python
How to generate permutations in Python and C ++
From Kivy environment construction to displaying Hello World
Writing logs to CSV file (Python, C language)
Try porting the "FORTRAN77 Numerical Computing Programming" program to C and Python (Part 3)
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping Classes Part ➀
Try porting the "FORTRAN77 Numerical Computing Programming" program to C and Python (Part 2)
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping Classes Part ➁
Call a Python script from Embedded Python in C ++ / C ++
List of Python code to move and remember
I want to make C ++ code from Python code!
An easy way to call Java from Python
The story of porting code from C to Go and getting hooked (and to the language spec)
Try to make a Python module in C language
Study from the beginning of Python Hour1: Hello World
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Introduction to Python language
Call Rust from Python to speed it up! PyO3 Tutorial: Wrapping a Simple Function Part ➁
[Introduction to cx_Oracle] (Part 6) DB and Python data type mapping
Python regular expression basics and tips to learn from scratch
How to connect to various DBs from Python (PEP 249) and SQLAlchemy
I tried to illustrate the time and time in C language
From setting up a Rust environment to running Hello World
RabbitMQ Tutorial (1) -From Installation to Hello World (Official Site Translation)
Object-oriented in C: Refactored "○ ✕ game" and ported it to Python
Use a scripting language for a comfortable C ++ life-OpenCV-Port Python to C ++-
Pass OpenCV data from the original C ++ library to Python
How to call Python or Julia from Ruby (experimental implementation)
python / tensorflow beginners build jupyter + tensorflow environment and do Hello World
Call CPLEX from Python (DO cplex)
Post from Python to Slack
Cheating from PHP to Python