How to create a .pyd file?

You have to run a setup.py file in a terminal. This one is an example that uses numpy

try:
    from setuptools import setup
    from setuptools import Extension
except ImportError:
    from distutils.core import setup
    from distutils.extension import Extension

from Cython.Distutils import build_ext
import numpy as np

ext_modules = [Extension("my_code_cython",["my_code_cython.pyx"]),
               Extension("another_code_cython",["another_code_cython.pyx"])]

setup(
    name= 'Generic model class',
    cmdclass = {'build_ext': build_ext},
    include_dirs = [np.get_include()],
    ext_modules = ext_modules)

In the terminal (cmd in Windows) you have to execute the command

python setup.py build_ext --inplace

It is important that I suppose you have installed the compiler (Microsoft Visual C++ Compiler Package for Python 2.7 for example). You can find more information in https://github.com/cython/cython/wiki/CythonExtensionsOnWindows


If you try to find your answer using cython with Visual Studio to convert python code into pyd( Python Dynamic Module ) then, you will have a blurry answer. As, visual code that you expect to work might not due to compatibility issue with later versions. For instance, 1900, 1929 of msvc.

You will need to edit cygwin-compiler in disutils to get things done. If you want to use MingW then also you need to include the configuration of compiler used in disutils.

A very simple way is that we can use Nuitka, it is very simplifed and reliable to convert python code into C and Python Dynamic Library. No configuration, no support addition required.

Let's grab basics

1). Install nuitka, pip install nuitka

2). Install MingW, from Sourceforge

3). Add mingW to path

And everything is good to go now. 4).Open cmd as admin, type python -m nuitka --module file.py

Nuitka will create file.c, file.pyi(For imports) and file.cp39_architecture.pyd in current directory. From file.pyd you can import the module directly into your main.py and it will be lightning fast.

But, it you want to create standalone application then try, python -m nuitkafile.py