Page 211 - CodePilot V5.0 C8
P. 211

Install a C Compiler

                 To compile Cython code on Windows, you need a C compiler. Here’s how to install one:

                 Windows: Microsoft C++ Build Tools
                 Step   1     Go to https://visualstudio.microsoft.com/visual-cpp-build-tools/.

                 Step   2     Click Download Build Tools and run the installer.
                 Step  3       In the installer Go to Select “C++ build tools”>Make sure Windows 10 SDK or Windows

                              11 SDK is checked and click Install.

                 Write a Cython File

                 A .pyx file is where you write your Cython code. It looks almost like regular Python but allows you
                 to add C-style type declarations for better performance.

                 Steps to Create a .pyx File
                 Step   1     Open any text editor — Notepad, VS Code.

                 Step   2     Write your Cython code and Save the file with a .pyx extension.

                 Create setup.py File

                 To compile your Cython file, you need a setup.py script that uses Cython’s build system.

                 Compile the Module
                 After creating Cython file and setup file, you need to compile the module so it can be used in

                 Python.

                 PROGRAM ON CYTHON

                 Here’s a basic Cython program that defines a simple function to add two numbers.
                 Step   1     Create a ‘.pyx’ File named as math_ops.pyx
















                 Step   2     Create a ‘setup.py’ File.















                                                                                                                  209
                                                                                            Bonus Byte - 1 (Cython)
   206   207   208   209   210   211   212   213   214   215   216