SoFunction
Updated on 2025-04-14

pyc, pyd files and generation in python use complete instance code

Introduction

The python source code file is a py suffix. If you see the file with the py extension, you can judge it as a python code file. In the python system, there are also pyc files and pyd files.

Note: The python used in this operation is v3.11 version.

pyc

The file pyc is a bytecode file generated after python is compiled.

Using pyc can speed up the loading speed of the program, but not the actual execution speed of the program. This is why we install many third-party libraries in the python directory, because it can speed up the speed of importing some third-party libraries.

You can use the python interpreter to compile pyc bytecode files. We perform normallypython When importing other modules, they will be automatically created__pycache__directory, and generate pyc files under this directory.

To generate pyc files manually, use python commands, use-mCallcompileallThe module is used to compile and generate pyc's own code file. The generated file name has been suffixed, includingcythonbackpython version number, likecython_311

Execute the command as follows:

# Compile the specified file.python -m compileall   

or

# Compile all python files in the directory.python -m compileall ./

pyd

pyd is a dynamic connection library file of the operating system generated by the c program. They are not bytecode files for python, but executable dynamic connection library files corresponding to os.

When using it, place the pyd file in the python installation directoryDLLsIn the directory, the module can be used globally.

Compile and generate pyd

Prepare

To compile and generate pyd, you need to use 2 modules:

  • cython,If there is no such module, please install it firstpip install cython
  • In-housesetup

process

When compiling and generating pyd, the C code will be created first using the cython module function, and then using c compile to generate dynamic connection library files.

operate

  • Write a python script to process the module source code to be compiled.
## name pyd_setup.py
from  import setup
from  import cythonize

setup(ext_modules=cythonize('my_module.py'))

  • Execute scriptpython pyd_setup.pyCheck out the prompt information
>python pyd_setup.py
usage: pyd_setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
   or: pyd_setup.py --help [cmd1 cmd2 ...]
   or: pyd_setup.py --help-commands
   or: pyd_setup.py cmd --help

>python pyd_setup.py --help-commands
Standard commands:
  build             build everything needed to install
  build_py          "build" pure Python modules (copy to build directory)
  build_ext         build C/C++ extensions (compile/link to build directory)
  build_clib        build C/C++ libraries used by Python extensions
  build_scripts     "build" scripts (copy and fixup #! line)
  clean             clean up temporary files from 'build' command
  install           install everything from build directory
  install_lib       install all Python modules (extensions and pure Python)
  install_headers   install C/C++ header files
  install_scripts   install scripts (Python or otherwise)
  install_data      install data files
  sdist             create a source distribution (tarball, zip file, etc.)
  register          register the distribution with the Python package index
  bdist             create a built (binary) distribution
  bdist_dumb        create a "dumb" built distribution
  bdist_rpm         create an RPM distribution
  check             perform some checks on the package
  upload            upload binary package to PyPI

Extra commands:
  alias             define a shortcut to invoke one or more commands
  bdist_egg         create an "egg" distribution
  develop           install package in 'development mode'
  dist_info         create a .dist-info directory
  easy_install      Find/get/install Python packages
  editable_wheel    create a PEP 660 'editable' wheel
  egg_info          create a distribution's .egg-info directory
  install_egg_info  Install an .egg-info directory for the package
  rotate            delete older distributions, keeping N newest files
  saveopts          save supplied options to  or other config file
  setopt            set an option in  or another config file
  test              run unit tests after in-place build (deprecated)
  upload_docs       Upload documentation to sites other than PyPi such as devpi

usage: pyd_setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
   or: pyd_setup.py --help [cmd1 cmd2 ...]
   or: pyd_setup.py --help-commands
   or: pyd_setup.py cmd --help

Use subcommandsbuild_ext, The compiled c/C++ source code can be used to connect to generate an extended dynamic link library. Execute

>   pyd_setup.py build_ext
running build_ext
building 'my_module' extension
creating build
creating build\-amd64-cpython-311
creating build\-amd64-cpython-311\Release
"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\HostX86\x64\" /c /nologo /O2 /W3 /GL /DNDEBUG /MD -IC:\Python311\include -IC:\Python311\Include "-IC:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include" "-IC:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\ATLMFC\include" "-IC:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\VS\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\um" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\shared" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\winrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\cppwinrt" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um" /Tcmy_module.c /Fobuild\-amd64-cpython-311\Release\my_module.obj
my_module.c
creating D:\learning\python\basic\build\-amd64-cpython-311
"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\HostX86\x64\" /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:C:\Python311\libs /LIBPATH:C:\Python311 /LIBPATH:C:\Python311\PCbuild\amd64 "/LIBPATH:C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\ATLMFC\lib\x64" "/LIBPATH:C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\lib\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\lib\um\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.22621.0\ucrt\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\\lib\10.0.22621.0\\um\x64" /EXPORT:PyInit_my_module build\-amd64-cpython-311\Release\my_module.obj /OUT:build\-amd64-cpython-311\my_module.cp311-win_amd64.pyd /IMPLIB:build\-amd64-cpython-311\Release\my_module.cp311-win_amd64.lib
  Creating a library build\-amd64-cpython-311\Release\my_module.cp311-win_amd64.lib and objects build\-amd64-cpython-311\Release\my_module.cp311-win_amd64.exp
Generating code
Completed code generation

You can see that the my_module.c file is created, and compiled using the local c compiler, and then connected to generate a dynamic library.

You can see the directories and files generated in the middle.

>dir /s build
 
 D:\learning\python\basic\build Table of Contents

2024/04/16  16:18    <DIR>          .
2024/04/16  16:18    <DIR>          ..
2024/04/16  16:18    <DIR>          -amd64-cpython-311
2024/04/16  16:18    <DIR>          -amd64-cpython-311
               0 A file              0 byte

 D:\learning\python\basic\build\-amd64-cpython-311 Table of Contents

2024/04/16  16:18    <DIR>          .
2024/04/16  16:18    <DIR>          ..
2024/04/16  16:18            37,376 my_module.cp311-win_amd64.pyd
               1 A file         37,376 byte

 D:\learning\python\basic\build\-amd64-cpython-311 Table of Contents

2024/04/16  16:18    <DIR>          .
2024/04/16  16:18    <DIR>          ..
2024/04/16  16:18    <DIR>          Release
               0 A file              0 byte

 D:\learning\python\basic\build\-amd64-cpython-311\Release Table of Contents

2024/04/16  16:18    <DIR>          .
2024/04/16  16:18    <DIR>          ..
2024/04/16  16:18               766 my_module.cp311-win_amd64.exp
2024/04/16  16:18             2,048 my_module.cp311-win_amd64.lib
2024/04/16  16:18           345,485 my_module.obj
               3 A file        348,299 byte

     Total number of listed files:
               4 A file        385,675 byte
              11 Table of contents 274,485,997,568 可用byte

Put pyd to system location

Copy the pyd file to the python systemDLLsNext, it can be easily imported and used in python programs.

pyo

When executing the python interpreter, if-O Options to optimize. Run the above commands before python3.5 and generate a Pyo file. Starting from python 3.5, the pyo file will no longer be generated, but the [name]. file. In the generated bytecode file, the file name will be addedopt-#suffix.

Summarize

This is the article about pyc, pyd files and generation and use in python. For more related contents of python pyc, pyd files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!