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