Cython use case: output Hello World
-
Cython
Cython
is containing C data typesPython
." Cython Research Data Summary "
python
is a scripting language;cpython
is usedc
to achievepython
interpreter;cython
is another programming language,python
andc
between;
Actually
cython
The original intention of the design is also the same, both to usepython
Fast programming speed, but also havec
The operating efficiency of the language.cython
andpython
A notable difference is thatcython
All variables of the variable type must be explicitly declared - that alone, the same program,cython
operating efficiency is higher thanpython
35% higher!Although
cython
It is an independent programming language, but it seems that everyone does not use it to write programs independently, but uses it to writepython
ofc extension
(Use c to efficiently implement some programs, and then call python
) -
Cython outputs Hello World
-
Create test.py
The contents of the file are as follows:
def say_hello(): print("Hello World")
- 1
- 2
The official documents used in this link are
test.pyx
file with the same content..pyx
file is made byCython
programming language "written"Python
Extension module source code files.pyx
file ispython
ofc
Expansion file, the code must conform tocython
specification, any editor can be used to write it. -
Create setup.py
above
.py
orpyx
The file is also just a source code file, if you want to bepython
To call and run, just writing the source code is not enough. Specifically, it is also necessary to convert.c
or.c++
, and further convert it into.pyd
document.pyd
A file is a file that can be used directly. In order to achieve the above purpose, it is necessary to write asetup.py
script, as follows:from distutils.core import setup from Cython.Build import cythonize setup( ext_modules = cythonize("test.py") )
- 1
- 2
- 3
- 4
- 5
- 6
-
Build Cython files
From the command line, enter the path where the file is located and run the following code:
python setup.py build_ext --inplace
- 1
build_ext
is specifiedpython
generateC/C++
extension module (build C/C++ extensions (compile/link to build directory)
);
--inplace
Indicates that the compiled extension module is placed directly with thetest.py
in the same directorygenerate a
helloworld.pyd
file, two errors may occur during operation: -
run in Python
exist
Python
interface orCMD >> python
, import the newly created fileimport test as ts ts.say_hello()
- 1
- 2
in,
cmd
to go firsttest
The directory where the above code is located will succeed.
-
-
explain
The above process is divided into two parts:
- .py files are compiled into .c files using Cython;
- .c files use C compiler to generate .pyd (windos) or .so (linux) files
unlike
Python
language that can be directly interpreted using.py
document,.py
(or.pyx
) file must first be compiled into.c
file, which is then compiled into.pyd
(Windows
platform) or.so
(Linux
platform) file to be used as a moduleimport
Import using..c
file istest.py
transformedC code
file, relatively large;.pyd
YesPython
The dynamic link library , when usingimport
will be loaded when.pyc
It is bytecode, binary, which is the code directly run by the Python virtual machine;build
The directory contains temporary files generated during compilation..pyx file
Yespython
ofc extension file
, the code must conform tocython
specification, any editor can be used to write it.
## References
- 1
- The usage of Cython and the posture of filling the pit Key reference materials
- Official Documentation: Basics of Cython
- (2019.11.22 has been resolved) Cython error: Unable to find vcvarsall.bat
- (2019.12.17 resolved) Cython compile ImportError: No module named Cython.Build
- Cython - A static compiler for writing Python's C extension modules based on Pyrex
- pyx files generate pyd files for cython calls