Cython use case: output Hello World

  • Cython

    Cythonis containing C data typesPython.

    " Cython Research Data Summary "

    1. pythonis a scripting language;
    2. cpythonis usedcto achievepythoninterpreter;
    3. cythonis another programming language,pythonandcbetween;

    ActuallycythonThe original intention of the design is also the same, both to usepythonFast programming speed, but also havecThe operating efficiency of the language.cythonandpythonA notable difference is thatcythonAll variables of the variable type must be explicitly declared - that alone, the same program,cythonoperating efficiency is higher thanpython35% higher!

    AlthoughcythonIt is an independent programming language, but it seems that everyone does not use it to write programs independently, but uses it to writepythonofc extension(Use c to efficiently implement some programs, and then call python)

  • Cython outputs Hello World

    1. Create

      The contents of the file are as follows:

      def say_hello():
          print("Hello World")
      • 1
      • 2

      The official documents used in this link aretest.pyxfile with the same content.

      .pyxfile is made byCythonprogramming language "written"PythonExtension module source code files.

      pyxfile ispythonofcExpansion file, the code must conform tocythonspecification, any editor can be used to write it.

    2. Create

      above.pyorpyxThe file is also just a source code file, if you want to bepythonTo call and run, just writing the source code is not enough. Specifically, it is also necessary to convert.cor.c++, and further convert it into.pyddocument.pydA file is a file that can be used directly. In order to achieve the above purpose, it is necessary to write asetup.pyscript, as follows:

      from distutils.core import setup
      from Cython.Build import cythonize
          ext_modules = cythonize("")
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
    3. Build Cython files

      From the command line, enter the path where the file is located and run the following code:

      python build_ext --inplace
      • 1

      build_extis specifiedpythongenerateC/C++extension module (build C/C++ extensions (compile/link to build directory));
      --inplaceIndicates that the compiled extension module is placed directly with thetest.pyin the same directory

      generate ahelloworld.pydfile, two errors may occur during operation:

    4. run in Python

      existPythoninterface orCMD >> python, import the newly created file

      import test as ts
      • 1
      • 2

      in,cmdto go firsttestThe directory where the above code is located will succeed.

      insert image description here

  • explain

    The above process is divided into two parts:

    1. .py files are compiled into .c files using Cython;
    2. .c files use C compiler to generate .pyd (windos) or .so (linux) files

    unlikePythonlanguage that can be directly interpreted using.pydocument,.py(or.pyx) file must first be compiled into.cfile, which is then compiled into.pyd(Windowsplatform) file to be used as a moduleimportImport using.

    .cfile istest.pytransformedC codefile, relatively large;

    .pydYesPythonThe dynamic link library , when usingimportwill be loaded when

    .pycIt is bytecode, binary, which is the code directly run by the Python virtual machine;

    buildThe directory contains temporary files generated during compilation.

    .pyx fileYespythonofc extension file, the code must conform tocythonspecification, any editor can be used to write it.

## References
  • 1
  1. The usage of Cython and the posture of filling the pit Key reference materials
  2. Official Documentation: Basics of Cython
  3. (2019.11.22 has been resolved) Cython error: Unable to find vcvarsall.bat
  4. (2019.12.17 resolved) Cython compile ImportError: No module named Cython.Build
  5. Cython - A static compiler for writing Python's C extension modules based on Pyrex
  6. pyx files generate pyd files for cython calls

Related: Cython use case: output Hello World