It looks like you're new here. If you want to get involved, click one of these buttons!
Hello Matthias,
May I know how to compile a .py file into a .pyd? I tried using MSYS2, but it only supports Python 3.12, not 3.11, so I wasn’t successful. I believe compiling to .pyd can improve efficiency and help protect the code from accidental modification.
I also tried using the official Python package, and I got a file named cp311-win_amd64.pyd, instead of the more common filename cp311-mingw_x86_64.pyd that I see in the folder.
Good day
Vincent
Comments
Hi @Vincent Lin,
.pyd to my knowledge is just some bytecode that Python generates and caches internally. You do not need to generate it yourself - Python does that automatically.
Matthias
Dear Matthias,
Thank you very much for your kind reply.
I was wondering if it’s possible to convert a Python script into something like the attached “math module”—a compiled form that can be imported and used to call specific functions. My goal is to avoid accidental modifications during execution.
This comes from a recent incident where a critical mistake was nearly made just before a mask tape-out. As I don’t have a strong background in software development, I’m not sure if this is an appropriate topic to raise—please feel free to disregard it if not.
That said, I noticed a “Cython” folder in the directory. Perhaps, as you mentioned, Python already handles such things automatically, though I’m unsure how or where to find the compiled result.
I truly appreciate your time and guidance.
Good day
Vincent
Hi @Vincent Lin
I think you're right. What I meant is ".pyc" - this is bytecode that Python generates itself. ".pyd" is a compiled version (essentially a DLL) which is generated by Cython.
MSYS is a rolling release, which is very bad for adoption. I am trying to keep the environment stable are rarely update by MSYS installation. Hence it is still on Python 3.11, while a fresh installation of MSYS will give you something more recent. AFAIK you cannot roll back to a specific MSYS release, so for compatibility, you will have to build KLayout on your MSYS installation together with your extensions.
In general, however, to prevent data loss and a reproducible flow, you need to take stricter measures than compiling or obfuscating your modules. I don't know what went wrong exactly, but in a production flow, you usually would work with centralized and versioned installations which are managed by some admin accounts and only permit use of these installations. In KLayout, you can set the "KLAYOUT_PATH" environment variable to include such a central installation rather than letting individual users have their own copies.
Matthias
Thank you for your comment. I’ll try to follow the approach you suggested.
Vincent