最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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文件,需要的朋友可以參考下

簡(jiǎn)介

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

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

pyc

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

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

可以使用 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模塊來(lái)進(jìn)行編譯,生成pyc自己碼文件。生成的文件名添加了后綴,包括cythonpython的版本號(hào), 如cython_311。

執(zhí)行命令如下:

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

或者

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

pyd

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

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

編譯生成pyd

準(zhǔn)備

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

  • cython,如果沒(méi)有此模塊,請(qǐng)先安裝pip install cython。
  • distutils.core中的setup。

過(guò)程

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

操作

  • 編寫一個(gè)python腳本,來(lái)處理要編譯的模塊源碼。
## 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)鏈接庫(kù). 執(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)建庫(kù) build\temp.win-amd64-cpython-311\Release\my_module.cp311-win_amd64.lib 和對(duì)象 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)庫(kù)。

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

>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)來(lái)進(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 如何用Python繪制棒棒糖圖表

    如何用Python繪制棒棒糖圖表

    這篇文章主要介紹了如何用Python繪制棒棒糖圖表,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-05-05
  • python run和debug程序的操作過(guò)程

    python run和debug程序的操作過(guò)程

    有的時(shí)候我們需要再vscode里面調(diào)試python代碼,但是我們還需要為不同的文件設(shè)置不同的調(diào)試參數(shù),如設(shè)置python解釋器,設(shè)置輸入?yún)?shù)等,下面我們聊聊如何再vscode進(jìn)行上述的調(diào)試和運(yùn)行,感興趣的朋友跟隨小編一起看看吧
    2026-02-02
  • Windows下實(shí)現(xiàn)將Pascal VOC轉(zhuǎn)化為TFRecords

    Windows下實(shí)現(xiàn)將Pascal VOC轉(zhuǎn)化為TFRecords

    今天小編就為大家分享一篇Windows下實(shí)現(xiàn)將Pascal VOC轉(zhuǎn)化為TFRecords,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02
  • Python的裝飾器模式與面向切面編程詳解

    Python的裝飾器模式與面向切面編程詳解

    這篇文章主要介紹了Python的裝飾器模式與面向切面編程詳解,概括的講,裝飾器的作用就是為已經(jīng)存在的對(duì)象添加額外的功能,本文詳細(xì)了裝飾器模式的方方面面,然后引出面向切面編程知識(shí),需要的朋友可以參考下
    2015-06-06
  • python使用hdfs3模塊對(duì)hdfs進(jìn)行操作詳解

    python使用hdfs3模塊對(duì)hdfs進(jìn)行操作詳解

    這篇文章主要介紹了python使用hdfs3模塊對(duì)hdfs進(jìn)行操作詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-06-06
  • tensorflow之變量初始化(tf.Variable)使用詳解

    tensorflow之變量初始化(tf.Variable)使用詳解

    今天小編就為大家分享一篇tensorflow之變量初始化(tf.Variable)使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02
  • 使用Python對(duì)Excel數(shù)據(jù)讀取與保存的全面指南

    使用Python對(duì)Excel數(shù)據(jù)讀取與保存的全面指南

    在數(shù)據(jù)分析與處理工作中,Excel文件是最常見的數(shù)據(jù)源之一,本文將詳細(xì)介紹如何使用Python的Pandas庫(kù)進(jìn)行Excel文件的讀寫操作,涵蓋常用函數(shù)、典型應(yīng)用場(chǎng)景、實(shí)例演示及常見問(wèn)題解決方案,需要的朋友可以參考下
    2025-12-12
  • python中os.path.dirname(path)詳細(xì)解釋和使用示例

    python中os.path.dirname(path)詳細(xì)解釋和使用示例

    這篇文章主要介紹了python中os.path.dirname(path)詳細(xì)解釋和使用示例,os.path.dirname是一個(gè)Python函數(shù),用于獲取文件路徑的目錄部分,它通常與os.path.basename結(jié)合使用,以分離路徑中的目錄和文件名,需要的朋友可以參考下
    2025-03-03
  • 3行Python代碼實(shí)現(xiàn)剪輯音樂(lè)

    3行Python代碼實(shí)現(xiàn)剪輯音樂(lè)

    你以為剪輯音樂(lè)要很久嗎?其余3行語(yǔ)句Python就能瞬間搞定。本文就來(lái)詳細(xì)為大家講講實(shí)現(xiàn)的步驟,文中的示例代碼講解詳細(xì),感興趣的可以動(dòng)手嘗試一下
    2022-06-06
  • Python趣味挑戰(zhàn)之pygame實(shí)現(xiàn)無(wú)敵好看的百葉窗動(dòng)態(tài)效果

    Python趣味挑戰(zhàn)之pygame實(shí)現(xiàn)無(wú)敵好看的百葉窗動(dòng)態(tài)效果

    最近寫了很多期關(guān)于pygame的案例和知識(shí)點(diǎn),自己也收獲了很多知識(shí),也在這個(gè)過(guò)程中成長(zhǎng)了不少, 這次還是圍繞surface對(duì)象進(jìn)行詳細(xì)介紹,并形成完整的案例過(guò)程,文中有非常詳細(xì)實(shí)現(xiàn)百葉窗動(dòng)態(tài)效果的代碼示例,需要的朋友可以參考下
    2021-05-05

最新評(píng)論

五指山市| 历史| 靖安县| 辛集市| 延川县| 南丰县| 惠东县| 永胜县| 乌海市| 天峨县| 札达县| 玛纳斯县| 金山区| 永修县| 钟祥市| 苍溪县| 图木舒克市| 来安县| 万安县| 阆中市| 清涧县| 海淀区| 鸡西市| 吴江市| 会昌县| 香港| 桐柏县| 阳江市| 方正县| 柳州市| 山阴县| 鄂温| 达拉特旗| 二手房| 金昌市| 旺苍县| 江源县| 彭阳县| 楚雄市| 光山县| 古浪县|