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

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

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

簡介

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

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

pyc

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

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

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

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

執(zhí)行命令如下:

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

或者

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

pyd

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

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

編譯生成pyd

準備

編譯生成pyd,需要使用2個模塊:

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

過程

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

操作

  • 編寫一個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++源碼,連接生成擴展的動態(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編譯器進行編譯,再連接生成動態(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 個文件              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 個文件         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 個文件              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 個文件        348,299 字節(jié)

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

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

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

pyo

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

總結

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

相關文章

  • Django中使用第三方登錄的示例代碼

    Django中使用第三方登錄的示例代碼

    這篇文章主要介紹了Django中使用第三方登錄的示例代碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • python標準庫turtle海龜繪圖實現簡單奧運五環(huán)

    python標準庫turtle海龜繪圖實現簡單奧運五環(huán)

    這篇文章主要為大家介紹了python使用turtle實現最簡單簡單奧運五環(huán)繪圖,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • 淺談Scrapy網絡爬蟲框架的工作原理和數據采集

    淺談Scrapy網絡爬蟲框架的工作原理和數據采集

    在python爬蟲中:requests + selenium 可以解決目前90%的爬蟲需求,難道scrapy 是解決剩下的10%的嗎?顯然不是。scrapy框架是為了讓我們的爬蟲更強大、更高效。接下來我們一起學習一下它吧。
    2019-02-02
  • python將pandas datarame保存為txt文件的實例

    python將pandas datarame保存為txt文件的實例

    今天小編就為大家分享一篇python將pandas datarame保存為txt文件的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • python中shape[0]與shape[1]的說明

    python中shape[0]與shape[1]的說明

    這篇文章主要介紹了python中shape[0]與shape[1]的說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • python中匿名函數的應用方法

    python中匿名函數的應用方法

    這篇文章主要介紹了python中匿名函數的應用方法,匿名函數是無需使用def定義的函數,只需使用關鍵字lambda進行聲明,且只可使用一次,只有一個返回值,需要的朋友可以參考下
    2023-07-07
  • DES加密解密算法之python實現版(圖文并茂)

    DES加密解密算法之python實現版(圖文并茂)

    這篇文章主要介紹了DES加密解密算法之python實現版,圖文并茂的為大家分享一下,需要的朋友可以參考下
    2018-12-12
  • python排序算法之選擇排序

    python排序算法之選擇排序

    這篇文章主要介紹了python排序算法之選擇排序,選擇排序表示從無序的數組中,每次選擇最小或最大的數據,從無序數組中放到有序數組的末尾,以達到排序的效果,需要的朋友可以參考下
    2023-04-04
  • python實現簡單的udp發(fā)送和接收

    python實現簡單的udp發(fā)送和接收

    這篇文章主要介紹了python實現簡單的udp發(fā)送和接收方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • 關于Django使用 django-celery-beat動態(tài)添加定時任務的方法

    關于Django使用 django-celery-beat動態(tài)添加定時任務的方法

    本文給大家介紹Django使用 django-celery-beat動態(tài)添加定時任務的方法,安裝對應的是celery版本,文中給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2021-10-10

最新評論