|
简介python源码文件是py后缀,看到py扩展名的文件,那就可用判断其为python代码文件。在python系统里,还有pyc文件和pyd文件。注意:本操作使用的python为v3.11版本。pyc文件pyc是python编译后,生成的字节码文件。使用pyc可以加快程序的加载速度,但不能加快程序的实际执行速度,这就是解释为什么我们安装python目录很多第三方库下是pyc文件的原因,因为它可以使得import一些第三方库的速度加快。可以使用python解释器编译py文件成pyc字节码文件。我们正常执行pythonsource.py时,如果有import其它的模块,则会自动创建__pycache__目录,并在该目录下生成pyc文件。要手动生成pyc文件,使用python命令,使用-m调用compileall模块来进行编译,生成pyc自己码文件。生成的文件名添加了后缀,包括cython后python的版本号,如cython_311。执行命令如下:#编译指定的文件。python-mcompileallsource.py12或者#编译目录下的所有python文件。python-mcompileall./12pydpyd是由c程序编译生成的操作系统的动态连接库文件。它们不是python的字节码文件,而是对应os的可执行的动态连接库文件。使用时,把pyd文件放置到python安装目录的DLLs目录下,可用全局使用该模块。编译生成pyd准备编译生成pyd,需要使用2个模块:cython,如果没有此模块,请先安装pipinstallcython。distutils.core中的setup。过程在编译生成pyd时,会先使用cython模块功能来创建c代码,再使用c编译生成动态连接库文件。操作编写一个python脚本,来处理要编译的模块源码。##namepyd_setup.pyfromdistutils.coreimportsetupfromCython.Buildimportcythonizesetup(ext_modules=cythonize('my_module.py'))123456执行脚本pythonpyd_setup.py看看提示信息>pythonpyd_setup.pyusage:pyd_setup.py[global_opts]cmd1[cmd1_opts][cmd2[cmd2_opts]...]or:pyd_setup.py--help[cmd1cmd2...]or:pyd_setup.py--help-commandsor:pyd_setup.pycmd--help>pythonpyd_setup.py--help-commandsStandardcommands:buildbuildeverythingneededtoinstallbuild_py"build"purePythonmodules(copytobuilddirectory)build_extbuildC/C++extensions(compile/linktobuilddirectory)build_clibbuildC/C++librariesusedbyPythonextensionsbuild_scripts"build"scripts(copyandfixup#!line)cleancleanuptemporaryfilesfrom'build'commandinstallinstalleverythingfrombuilddirectoryinstall_libinstallallPythonmodules(extensionsandpurePython)install_headersinstallC/C++headerfilesinstall_scriptsinstallscripts(Pythonorotherwise)install_datainstalldatafilessdistcreateasourcedistribution(tarball,zipfile,etc.)registerregisterthedistributionwiththePythonpackageindexbdistcreateabuilt(binary)distributionbdist_dumbcreatea"dumb"builtdistributionbdist_rpmcreateanRPMdistributioncheckperformsomechecksonthepackageuploaduploadbinarypackagetoPyPIExtracommands:aliasdefineashortcuttoinvokeoneormorecommandsbdist_eggcreatean"egg"distributiondevelopinstallpackagein'developmentmode'dist_infocreatea.dist-infodirectoryeasy_installFind/get/installPythonpackageseditable_wheelcreateaPEP660'editable'wheelegg_infocreateadistribution's.egg-infodirectoryinstall_egg_infoInstallan.egg-infodirectoryforthepackagerotatedeleteolderdistributions,keepingNnewestfilessaveoptssavesuppliedoptionstosetup.cfgorotherconfigfilesetoptsetanoptioninsetup.cfgoranotherconfigfiletestrununittestsafterin-placebuild(deprecated)upload_docsUploaddocumentationtositesotherthanPyPisuchasdevpiusage:pyd_setup.py[global_opts]cmd1[cmd1_opts][cmd2[cmd2_opts]...]or:pyd_setup.py--help[cmd1cmd2...]or:pyd_setup.py--help-commandsor:pyd_setup.pycmd--help12345678910111213141516171819202122232425262728293031323334353637383940414243444546使用子命令build_ext,可用编译生成的c/C++源码,连接生成扩展的动态链接库.执行>python.exepyd_setup.pybuild_extrunningbuild_extbuilding'my_module'extensioncreatingbuildcreatingbuild\temp.win-amd64-cpython-311creatingbuild\temp.win-amd64-cpython-311\Release"C:\ProgramFiles\MicrosoftVisualStudio\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:\ProgramFiles\MicrosoftVisualStudio\2022\Community\VC\Tools\MSVC\14.39.33519\include""-IC:\ProgramFiles\MicrosoftVisualStudio\2022\Community\VC\Tools\MSVC\14.39.33519\ATLMFC\include""-IC:\ProgramFiles\MicrosoftVisualStudio\2022\Community\VC\Auxiliary\VS\include""-IC:\ProgramFiles(x86)\WindowsKits\10\include\10.0.22621.0\ucrt""-IC:\ProgramFiles(x86)\WindowsKits\10\\include\10.0.22621.0\\um""-IC:\ProgramFiles(x86)\WindowsKits\10\\include\10.0.22621.0\\shared""-IC:\ProgramFiles(x86)\WindowsKits\10\\include\10.0.22621.0\\winrt""-IC:\ProgramFiles(x86)\WindowsKits\10\\include\10.0.22621.0\\cppwinrt""-IC:\ProgramFiles(x86)\WindowsKits\NETFXSDK\4.8\include\um"/Tcmy_module.c/Fobuild\temp.win-amd64-cpython-311\Release\my_module.objmy_module.ccreatingD:\learning\python\basic\build\lib.win-amd64-cpython-311"C:\ProgramFiles\MicrosoftVisualStudio\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:\ProgramFiles\MicrosoftVisualStudio\2022\Community\VC\Tools\MSVC\14.39.33519\ATLMFC\lib\x64""/LIBPATH:C:\ProgramFiles\MicrosoftVisualStudio\2022\Community\VC\Tools\MSVC\14.39.33519\lib\x64""/LIBPATH:C:\ProgramFiles(x86)\WindowsKits\NETFXSDK\4.8\lib\um\x64""/LIBPATH:C:\ProgramFiles(x86)\WindowsKits\10\lib\10.0.22621.0\ucrt\x64""/LIBPATH:C:\ProgramFiles(x86)\WindowsKits\10\\lib\10.0.22621.0\\um\x64"/EXPORTyInit_my_modulebuild\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正在创建库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正在生成代码已完成代码的生成12345678910111213可用看到,创建了my_module.c文件,并使用本地的c编译器进行编译,再连接生成动态库。可用看到中间生成的目录及文件。>dir/sbuildD:\learning\python\basic\build的目录2024/04/1616:18.2024/04/1616:18..2024/04/1616:18lib.win-amd64-cpython-3112024/04/1616:18temp.win-amd64-cpython-3110个文件0字节D:\learning\python\basic\build\lib.win-amd64-cpython-311的目录2024/04/1616:18.2024/04/1616:18..2024/04/1616:1837,376my_module.cp311-win_amd64.pyd1个文件37,376字节D:\learning\python\basic\build\temp.win-amd64-cpython-311的目录2024/04/1616:18.2024/04/1616:18..2024/04/1616:18Release0个文件0字节D:\learning\python\basic\build\temp.win-amd64-cpython-311\Release的目录2024/04/1616:18.2024/04/1616:18..2024/04/1616:18766my_module.cp311-win_amd64.exp2024/04/1616:182,048my_module.cp311-win_amd64.lib2024/04/1616:18345,485my_module.obj3个文件348,299字节所列文件总数:4个文件385,675字节11个目录274,485,997,568可用字节123456789101112131415161718192021222324252627282930313233343536将pyd放置到系统位置将pyd文件拷贝到python系统下的DLLs下,则可用在python程序中方便地import和使用了。pyo在执行python解释器时,如果使用-O选项来进行优化,python3.5以前的版本运行上面的命令,就会产生pyo文件。从python3.5开始,将不再产生pyo文件,而是[name].cpython-311.opt-1.pyc文件。在生成的字节码文件中,文件名会添加opt-#后缀.
|
|