Home Python Tips on how to Create a pyd File in Python

Tips on how to Create a pyd File in Python

Tips on how to Create a pyd File in Python


Python has a number of different Python recordsdata moreover the normal *.py file that it helps. On this tutorial, you’ll be taught concerning the PYD file.

A PYD file is a dynamic hyperlink library (DLL) written in Python that may be imported like a Python file. Nonetheless, in the event you try to open a PYD file, one can find it’s an unreadable binary file. Once you create a PYD file, you’ll put it aside with a .pyd extension, reminiscent of hey.pyd

The PYD file is for use on the Home windows working system. The supply code shouldn’t be accessible within a PYD file.

Let’s get began and learn to create a PYD file!

Getting Dependencies

You will want to put in a few different modules to create PYD recordsdata efficiently. Particularly, you will have the next:

Technically, you should utilize distutilsas a substitute of setuptools, nevertheless, distutilsis not included with Python, beginning in model 3.12. Since you might be making a DLL, you will have Cython to compile your Python code to C after which into the DLL.

In the event you don’t have already got the 2 packages listed above, you should utilize pip to put in them. Open up a terminal, PowerShell or cmd.exe and run the next command:

python -m pip set up setuptools Cython

If the whole lot labored appropriately, it is best to now have setuptools and Cython put in in your machine!

You are actually able to learn to create a PYD file.

Making a PYD File

For this tutorial, you may be making a Python script named api.py. Open up your favourite Python IDE or textual content editor and add the next code:

# api.py
api_key = "MY_API_KEY"

def PyInit_api():

Notice that while you create this file,

Right here is the setup file, you must create a specifically named operate: PyInit_CUSTOM_NAME(). On this instance, your module is called api.py, so that you’ll wish to title your particular operate PyInit_api(). Your particular operate doesn’t want any code inside it. As a substitute, this operate will stay empty.

When the api module is named, it should execute the PyInit_api() operate, will run mechanically, and initialize all of the variables and different items of code within the module to be able to import them.

On this instance, you create module-level variable referred to as api_key that you may import and entry in your different Python code.

Now that the module is prepared, you should flip it right into a PYD file. To create your PYD file, you must create a setup.pyscript.

Create a brand new file in your Python IDE or textual content editor and enter the next code:

# setup.py

from Cython.Distutils import build_ext
from setuptools import Extension, setup

ext_modules = [Extension("api", ["api_key.py"])]

    title="Check Program",
    cmdclass={"build_ext": build_ext},

Right here, you create an inventory of extension modules. On this case, you solely create one extension. You go within the extension’s title and an inventory of sources relative to the distribution root. There are extra arguments you might go in, however you don’t want them for this instance.

Subsequent, you configure setup() so it should construct your PYD file. Because you wish to construct a number of extensions, you should set the cmdclass to build_ext and go in Cython’s particular extension builder module. You additionally go in your customized Extensions record.

Now you’ll must run setup to construct your PYD file. Open your terminal again up and navigate to your supply code’s folder. Then run the next command:

python setup.py build_ext --inplace

Once you run this command, you will notice a bunch of textual content output  to the display that may look much like the next:

working build_ext
Compiling api_key.py as a result of it modified.
[1/1] Cythonizing api_key.py
C:UsersMikeAppDataLocalProgramsPythonPython311Libsite-packagesCythonCompilerMain.py:381: FutureWarning: Cookscode_playapi_key.py
  tree = Parsing.p_module(s, pxd, full_module_name)
creating buildtemp.win-amd64-cpython-311
creating buildtemp.win-amd64-cpython-311Release
"C:Program Recordsdata (x86)Microsoft Visible Studio2022BuildToolsVCToolsMSVC14.37.32822binHostX86x64cl.exe" /c /nologo /O2 /W3 /GL /DNDEBUG /MD -IC:UsersMikeAppDataLocalProgramsPythonPython311include -IC:UsersMikeAppDSVC14.37.32822include" "-IC:Program Recordsdata (x86)Microsoft Visible Studio2022BuildToolsVCAuxiliaryVSinclude" "-IC.22621.0um" "-IC:Program Recordsdata (x86)Home windows Kits10include10.0.22621.0shared" "-IC:Program Recordsdata (x86)Home windows Kits10include10.0.22621.0winrt" "-IC:Program Recordsdata (x86)Home windows Kits10include10.0.22621.0cppwinrt" -IC:PROGRA~1IBMSQLLIBINCLUDE -IC:PROGRA~1IBMSQLLIBLIB /Tcapi_key.c /Fobuildtemp.win-amd64-cpython-311Releaseapi_key.obj
"C:Program Recordsdata (x86)Microsoft Visible Studio2022BuildToolsVCToolsMSVC14.37.32822binHostX86x64link.exe" /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:C:UsersMikeAppDataLocalProgramsPythonPython311libs /LIBPATH:C:UsersMikeAppDataLocalProgramsPythonPython311 /LIBPATH:C:UsersMikeAppDataLocalProgramsPythonPython311PCbuildamd64 "/LIBPATH:C:Program Recordsdata (x86)Microsoft Visible Studio2022BuildToolsVCToolsMSVC14.37.32822libx64" "/LIBPATH:C:Program Recordsdata (x86)Home windows Kits10lib10.0.22621.0ucrtx64" "/LIBPATH:C:Program Recordsdata (x86)Home windows Kits10lib10.0.22621.0umx64" /LIBPATH:C:PROGRA~1IBMSQLLIBLIB /EXPORT:PyInit_api buildtemp.win-amd64-cpython-311Releaseapi_key.obj /OUT:C:bookscode_playapi.cp311-win_amd64.pyd /IMPLIB:buildtemp.win-amd64-cpython-311Releaseapi.cp311-win_amd64.lib
   Creating library buildtemp.win-amd64-cpython-311Releaseapi.cp311-win_amd64.lib and object buildtemp.win-amd64-cpython-311Releaseapi.cp311-win_amd64.exp

If the whole lot labored appropriately, it is best to now have a file named one thing like this: api.cp311-win_amd64.pyd

Additionally, you will have the next recordsdata and folders:

  • construct (a folder)
  • __pycache__ (a folder)
  • api_key.c  (C file)

You need to rename your PYD file to one thing else to import it. For instance, you may rename it to api.pyd

Now that it’s renamed run Python in your terminal and ensure it really works:

$ python
Python 3.11.5 (tags/v3.11.5:cce6ba9, Aug 24 2023, 14:38:34) [MSC v.1936 64 bit (AMD64)] on win32
Kind "assist", "copyright", "credit" or "license" for extra info.
>>> import api
>>> api.api_key

Nice! You simply created a PYD file!

Wrapping Up

You need to use PYD recordsdata as a sort of obfuscation. You might be able to get some velocity will increase utilizing PYD recordsdata, in case you are cautious.

Most likely the very best use case for PYD recordsdata is permitting applications aside from Python to load them. Give it a try to see if you will discover a great use for some of these recordsdata!




Please enter your comment!
Please enter your name here