欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python中pyc、?pyd文件及生成使用完整實(shí)例代碼

 更新時(shí)間:2025年04月11日 09:34:52   作者:whereismatrix  
這篇文章主要介紹了python中pyc、?pyd文件及生成使用的相關(guān)資料,重點(diǎn)講解了如何使用Python解釋器編譯.py文件為.pyc文件,以及如何使用Cython和distutils工具將C/C++代碼編譯為.pyd文件,需要的朋友可以參考下

簡介

python 源碼文件是py后綴,看到py擴(kuò)展名的文件,那就可用判斷其為python代碼文件。在python系統(tǒng)里,還有pyc文件和pyd文件。

注意: 本操作使用的python為v3.11版本。

pyc

文件pyc是python編譯后,生成的字節(jié)碼文件。

使用 pyc 可以加快程序的加載速度,但不能加快程序的實(shí)際執(zhí)行速度,這就是解釋為什么我們?安裝 python 目錄很多第三方庫下是 pyc 文件的原因,因?yàn)樗梢允沟?import 一些第三方庫的速度加快?。

可以使用 python 解釋器編譯 py 文件 成 pyc 字節(jié)碼文件。我們正常執(zhí)行python source.py時(shí),如果有import其它的模塊,則會(huì)自動(dòng)創(chuàng)建__pycache__目錄,并在該目錄下生成pyc文件。

要手動(dòng)生成pyc文件,使用python命令,使用-m調(diào)用compileall模塊來進(jìn)行編譯,生成pyc自己碼文件。生成的文件名添加了后綴,包括cythonpython的版本號, 如cython_311

執(zhí)行命令如下:

# 編譯指定的文件。
python -m compileall source.py  

或者

# 編譯目錄下的所有python文件。
python -m compileall ./

pyd

pyd是由c程序編譯生成的操作系統(tǒng)的動(dòng)態(tài)連接庫文件。它們不是python的字節(jié)碼文件,而是對應(yīng)os的可執(zhí)行的動(dòng)態(tài)連接庫文件。

使用時(shí),把pyd文件放置到python安裝目錄的DLLs目錄下,可用全局使用該模塊。

編譯生成pyd

準(zhǔn)備

編譯生成pyd,需要使用2個(gè)模塊:

  • cython,如果沒有此模塊,請先安裝pip install cython。
  • distutils.core中的setup

過程

在編譯生成pyd時(shí),會(huì)先使用cython模塊功能來創(chuàng)建c代碼, 再使用c編譯生成動(dòng)態(tài)連接庫文件。

操作

  • 編寫一個(gè)python腳本,來處理要編譯的模塊源碼。
## name pyd_setup.py
from distutils.core import setup
from Cython.Build import cythonize

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

  • 執(zhí)行腳本 python pyd_setup.py看看提示信息
>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 setup.cfg or other config file
  setopt            set an option in setup.cfg 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

使用子命令build_ext,可用編譯生成的c/C++源碼,連接生成擴(kuò)展的動(dòng)態(tài)鏈接庫. 執(zhí)行

> python.exe  pyd_setup.py build_ext
running build_ext
building 'my_module' extension
creating build
creating build\temp.win-amd64-cpython-311
creating build\temp.win-amd64-cpython-311\Release
"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\HostX86\x64\cl.exe" /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\temp.win-amd64-cpython-311\Release\my_module.obj
my_module.c
creating D:\learning\python\basic\build\lib.win-amd64-cpython-311
"C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\HostX86\x64\link.exe" /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\temp.win-amd64-cpython-311\Release\my_module.obj /OUT:build\lib.win-amd64-cpython-311\my_module.cp311-win_amd64.pyd /IMPLIB:build\temp.win-amd64-cpython-311\Release\my_module.cp311-win_amd64.lib
  正在創(chuàng)建庫 build\temp.win-amd64-cpython-311\Release\my_module.cp311-win_amd64.lib 和對象 build\temp.win-amd64-cpython-311\Release\my_module.cp311-win_amd64.exp
正在生成代碼
已完成代碼的生成

可用看到,創(chuàng)建了my_module.c文件,并使用本地的c編譯器進(jìn)行編譯,再連接生成動(dòng)態(tài)庫。

可用看到中間生成的目錄及文件。

>dir /s build
 
 D:\learning\python\basic\build 的目錄

2024/04/16  16:18    <DIR>          .
2024/04/16  16:18    <DIR>          ..
2024/04/16  16:18    <DIR>          lib.win-amd64-cpython-311
2024/04/16  16:18    <DIR>          temp.win-amd64-cpython-311
               0 個(gè)文件              0 字節(jié)

 D:\learning\python\basic\build\lib.win-amd64-cpython-311 的目錄

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 個(gè)文件         37,376 字節(jié)

 D:\learning\python\basic\build\temp.win-amd64-cpython-311 的目錄

2024/04/16  16:18    <DIR>          .
2024/04/16  16:18    <DIR>          ..
2024/04/16  16:18    <DIR>          Release
               0 個(gè)文件              0 字節(jié)

 D:\learning\python\basic\build\temp.win-amd64-cpython-311\Release 的目錄

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 個(gè)文件        348,299 字節(jié)

     所列文件總數(shù):
               4 個(gè)文件        385,675 字節(jié)
              11 個(gè)目錄 274,485,997,568 可用字節(jié)

將pyd放置到系統(tǒng)位置

將pyd文件拷貝到python系統(tǒng)下的DLLs下,則可用在python程序中方便地import和使用了。

pyo

在執(zhí)行python解釋器時(shí),如果使用-O 選項(xiàng)來進(jìn)行優(yōu)化,python3.5以前的版本運(yùn)行上面的命令,就會(huì)產(chǎn)生pyo文件。從python3.5開始,將不再產(chǎn)生pyo文件,而是[name].cpython-311.opt-1.pyc文件。在生成的字節(jié)碼文件中,文件名會(huì)添加opt-#后綴.

總結(jié)

到此這篇關(guān)于python中pyc、 pyd文件及生成使用的文章就介紹到這了,更多相關(guān)python pyc, pyd文件生成使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論