|
distutils(包分发的始祖)简介distutils是Python的一个标准库,从命名上很容易看出它是一个分发(distribute)工具(utlis),它是Python官方开发的一个分发打包工具,所有后续的打包工具,全部都是基于它进行开发的distutils的精髓在于编写setup.py,它是模块分发与安装的指导文件setuptools简介setuptools是distutils增强版,不包括在标准库中。其扩展了很多功能,能够帮助开发者更好的创建和分发Python包。大部分Python用户都会使用更先进的setuptools模块。setup.py命令help查看所有支持的命令(practice)tangsiqi@tangsiqideMacBook-Prodemo%pythonsetup.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_infoDONOTCALLDIRECTLY,INTERNALONLY:create.dist-infodirectoryeasy_installFind/get/installPythonpackageseditable_wheelDONOTCALLDIRECTLY,INTERNALONLY:createPEP660editablewheelegg_infocreateadistribution's.egg-infodirectoryinstall_egg_infoInstallan.egg-infodirectoryforthepackagerotatedeleteolderdistributions,keepingNnewestfilessaveoptssavesuppliedoptionstosetup.cfgorotherconfigfilesetoptsetanoptioninsetup.cfgoranotherconfigfiletestrununittestsafterin-placebuild(deprecated)upload_docsUploaddocumentationtositesotherthanPyPisuchasdevpibdist_wheelcreateawheeldistributionusage:setup.py[global_opts]cmd1[cmd1_opts][cmd2[cmd2_opts]...]or:setup.py--help[cmd1cmd2...]or:setup.py--help-commandsor:setup.pycmd--help1234567891011121314151617181920212223242526272829303132333435363738394041build构建安装时所需的所有内容:pythonsetup.pybuild1install安装包到系统环境中。该命令会将当前项目安装到当前Python环境的”site-packages”目录下,这样其他项目就可以像导入标准库一样导入该项目的代码了pythonsetup.pyinstall1develop以开发方式安装包。如果项目在开发过程中会频繁变更,每次安装还需要先将原来的版本卸掉,会很麻烦。使用”develop”开发方式安装的话,项目代码不会真的被拷贝到本地Python环境的”site-packages”目录下,而是在”site-packages”目录里创建一个指向当前项目位置的链接。这样如果当前位置的源码被改动,就会马上反映到”site-packages”里。pythonsetup.pydevelop1register、upload用于包的上传发布pythonsetup.pyregister#发布源码包pythonsetup.pysdistupload123sdist构建源码分发包。该命令会在当前目录下的”dist”目录内创建一个压缩包,默认在Windows下为zip格式,Linux下为tag.gz格式,也可以通过指定–formats参数指定压缩包格式。执行sdist命令时,默认会被打包的文件:所有py_modules或packages指定的源码文件所有ext_modules指定的文件所有package_data或data_files指定的文件所有scripts指定的脚本文件README、README.txt、setup.py和setup.cfg文件该命令构建的包主要用于发布,例如上传到pypi上pythonsetup.pysdist1bdist构建一个二进制的分发包pythonsetup.pybdist1bdist_egg构建一个egg分发包,经常用来替代基于bdist生成的模式。该命令会在当前目录下的”dist”目录内创建一个”egg”文件,名为”MyApp-1.0-py2.7.egg”。文件名格式就是”项目名-版本号-Python版本.egg”。同时你会注意到,当前目录多了”build”和”MyApp.egg-info”子目录来存放打包的中间结果pythonsetup.pybdist_egg1bdist_wheel构建一个wheel分发包,egg包是过时的,whl包是新的标准。同上面类似,只是打成的包的后缀是.whlpythonsetup.pybdist_wheel1简单例子python中安装包的方式有很多种:源码包:pythonsetup.pyinstall在线安装:pipinstall包名/easy_install包名pipinstall的东西从哪里来的?从PyPI(PythonPackageIndex)来的,官网是:https://pypi.python.org/pypi执行pipinstallterminaltranslator命令的时候,它就会去从官方网站搜terminaltranslator,搜到了就下载压缩包并解压安装,如果没有搜索到就会报错。源码包安装源码包安装就是你在本地编写好了一个模块,自己安装在本地使用,别人(即使是你自己)都不能pipinstallxxx下载你的模块准备工作#1.首先创建我们需要的目录结构和文件(自行创建)#当前测试的目录是:/tmp/demo`--demo|--helloapp||--hello.py|`--__init__.py`--setup.py#2.编辑setup.pyfromsetuptoolsimportsetup,find_packagessetup(name="myhello",version="1.0",author="tsq",author_email="865466036@qq.com",packages=find_packages(),)#3.编辑hello.pydefhello_func():print("HelloWorld")if__name__=='__main__':hello_func()1234567891011121314151617181920212223242526打tar包#进入setup.py所在的那层目录cddemo#检查setup.py是否有错误(warning不是错误)pythonsetup.pycheck#安装(practice)tangsiqi@tangsiqideMacBook-Prodemo%pythonsetup.pysdistrunningsdistrunningegg_infocreatingmyhello.egg-infowritingmyhello.egg-info/PKG-INFOwritingdependency_linkstomyhello.egg-info/dependency_links.txtwritingtop-levelnamestomyhello.egg-info/top_level.txtwritingmanifestfile'myhello.egg-info/SOURCES.txt'readingmanifestfile'myhello.egg-info/SOURCES.txt'writingmanifestfile'myhello.egg-info/SOURCES.txt'warning:sdist:standardfilenotfound:shouldhaveoneofREADME,README.rst,README.txt,README.mdrunningcheckcreatingmyhello-1.0creatingmyhello-1.0/myhellocreatingmyhello-1.0/myhello.egg-infocopyingfilestomyhello-1.0...copyingsetup.py->myhello-1.0copyingmyhello/__init__.py->myhello-1.0/myhellocopyingmyhello/hello.py->myhello-1.0/myhellocopyingmyhello.egg-info/PKG-INFO->myhello-1.0/myhello.egg-infocopyingmyhello.egg-info/SOURCES.txt->myhello-1.0/myhello.egg-infocopyingmyhello.egg-info/dependency_links.txt->myhello-1.0/myhello.egg-infocopyingmyhello.egg-info/top_level.txt->myhello-1.0/myhello.egg-infoWritingmyhello-1.0/setup.cfgcreatingdistCreatingtararchiveremoving'myhello-1.0'(andeverythingunderit)(practice)tangsiqi@tangsiqideMacBook-Prodemo%lsdistmyhellomyhello.egg-infosetup.py(practice)tangsiqi@tangsiqideMacBook-Prodemo%cddist(practice)tangsiqi@tangsiqideMacBook-Prodist%lsmyhello-1.0.tar.gz(practice)tangsiqi@tangsiqideMacBook-Prodist%pipinstallmyhello-1.0.tar.gzLookinginindexes:https://pypi.tuna.tsinghua.edu.cn/simpleProcessing./myhello-1.0.tar.gzPreparingmetadata(setup.py)...doneBuildingwheelsforcollectedpackages:myhelloBuildingwheelformyhello(setup.py)...doneCreatedwheelformyhello:filename=myhello-1.0-py3-none-any.whlsize=1338sha256=9a7be51148e7290b12310f8a5fbf77e8a3a8ad5d5402ece97ae3c8fd18d2d34cStoredindirectory:/Users/tangsiqi/Library/Caches/pip/wheels/3a/db/cf/d984476e0b41811a7a7e36a258d5f6419a69c66d8a5986311dSuccessfullybuiltmyhelloInstallingcollectedpackages:myhelloSuccessfullyinstalledmyhello-1.0(practice)tangsiqi@tangsiqideMacBook-Prodist%piplist|grepmyhellomyhello1.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354结果打包之后多出两个文件夹,分别是demo.egg-info和dist。demo.egg-info是必要的安装信息,而dist中的压缩包就是安装包,此时默认的tar包(practice)tangsiqi@tangsiqideMacBook-Prodemo%pythonPython3.8.17|packagedbyconda-forge|(default,Jun162023,07:11:32)[Clang14.0.6]ondarwinType"help","copyright","credits"or"license"formoreinformation.>>>frommyhelloimporthello>>>hello.hello_func()HelloWorld>>>exit()12345678打egg包(practice)tangsiqi@tangsiqideMacBook-Prodemo%lsmyhellosetup.py(practice)tangsiqi@tangsiqideMacBook-Prodemo%pythonsetup.pybdist_eggrunningbdist_eggrunningegg_infocreatingmyhello.egg-infowritingmyhello.egg-info/PKG-INFOwritingdependency_linkstomyhello.egg-info/dependency_links.txtwritingtop-levelnamestomyhello.egg-info/top_level.txtwritingmanifestfile'myhello.egg-info/SOURCES.txt'readingmanifestfile'myhello.egg-info/SOURCES.txt'writingmanifestfile'myhello.egg-info/SOURCES.txt'installinglibrarycodetobuild/bdist.macosx-11.0-arm64/egg/Users/tangsiqi/miniconda3/envs/practice/lib/python3.8/site-packages/setuptools/_distutils/cmd.py:66:SetuptoolsDeprecationWarning:setup.pyinstallisdeprecated.!!********************************************************************************Pleaseavoidrunning``setup.py``directly.Instead,usepypa/build,pypa/installerorotherstandards-basedtools.Seehttps://blog.ganssle.io/articles/2021/10/setup-py-deprecated.htmlfordetails.********************************************************************************!!self.initialize_options()runninginstall_librunningbuild_pycreatingbuildcreatingbuild/libcreatingbuild/lib/myhellocopyingmyhello/hello.py->build/lib/myhellocopyingmyhello/__init__.py->build/lib/myhellocreatingbuild/bdist.macosx-11.0-arm64creatingbuild/bdist.macosx-11.0-arm64/eggcreatingbuild/bdist.macosx-11.0-arm64/egg/myhellocopyingbuild/lib/myhello/hello.py->build/bdist.macosx-11.0-arm64/egg/myhellocopyingbuild/lib/myhello/__init__.py->build/bdist.macosx-11.0-arm64/egg/myhellobyte-compilingbuild/bdist.macosx-11.0-arm64/egg/myhello/hello.pytohello.cpython-38.pycbyte-compilingbuild/bdist.macosx-11.0-arm64/egg/myhello/__init__.pyto__init__.cpython-38.pyccreatingbuild/bdist.macosx-11.0-arm64/egg/EGG-INFOcopyingmyhello.egg-info/PKG-INFO->build/bdist.macosx-11.0-arm64/egg/EGG-INFOcopyingmyhello.egg-info/SOURCES.txt->build/bdist.macosx-11.0-arm64/egg/EGG-INFOcopyingmyhello.egg-info/dependency_links.txt->build/bdist.macosx-11.0-arm64/egg/EGG-INFOcopyingmyhello.egg-info/top_level.txt->build/bdist.macosx-11.0-arm64/egg/EGG-INFOzip_safeflagnotset;analyzingarchivecontents...creatingdistcreating'dist/myhello-1.0-py3.8.egg'andadding'build/bdist.macosx-11.0-arm64/egg'toitremoving'build/bdist.macosx-11.0-arm64/egg'(andeverythingunderit)12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849打wheel包(practice)tangsiqi@tangsiqideMacBook-Prodemo%lsmyhellosetup.py(practice)tangsiqi@tangsiqideMacBook-Prodemo%pythonsetup.pybdist_wheelrunningbdist_wheelrunningbuildrunningbuild_pycreatingbuildcreatingbuild/libcreatingbuild/lib/myhellocopyingmyhello/hello.py->build/lib/myhellocopyingmyhello/__init__.py->build/lib/myhello/Users/tangsiqi/miniconda3/envs/practice/lib/python3.8/site-packages/setuptools/_distutils/cmd.py:66:SetuptoolsDeprecationWarning:setup.pyinstallisdeprecated.!!********************************************************************************Pleaseavoidrunning``setup.py``directly.Instead,usepypa/build,pypa/installerorotherstandards-basedtools.Seehttps://blog.ganssle.io/articles/2021/10/setup-py-deprecated.htmlfordetails.********************************************************************************!!self.initialize_options()installingtobuild/bdist.macosx-11.0-arm64/wheelrunninginstallrunninginstall_libcreatingbuild/bdist.macosx-11.0-arm64creatingbuild/bdist.macosx-11.0-arm64/wheelcreatingbuild/bdist.macosx-11.0-arm64/wheel/myhellocopyingbuild/lib/myhello/hello.py->build/bdist.macosx-11.0-arm64/wheel/myhellocopyingbuild/lib/myhello/__init__.py->build/bdist.macosx-11.0-arm64/wheel/myhellorunninginstall_egg_inforunningegg_infocreatingmyhello.egg-infowritingmyhello.egg-info/PKG-INFOwritingdependency_linkstomyhello.egg-info/dependency_links.txtwritingtop-levelnamestomyhello.egg-info/top_level.txtwritingmanifestfile'myhello.egg-info/SOURCES.txt'readingmanifestfile'myhello.egg-info/SOURCES.txt'writingmanifestfile'myhello.egg-info/SOURCES.txt'Copyingmyhello.egg-infotobuild/bdist.macosx-11.0-arm64/wheel/myhello-1.0-py3.8.egg-inforunninginstall_scriptscreatingbuild/bdist.macosx-11.0-arm64/wheel/myhello-1.0.dist-info/WHEELcreating'dist/myhello-1.0-py3-none-any.whl'andadding'build/bdist.macosx-11.0-arm64/wheel'toitadding'myhello/__init__.py'adding'myhello/hello.py'adding'myhello-1.0.dist-info/METADATA'adding'myhello-1.0.dist-info/WHEEL'adding'myhello-1.0.dist-info/top_level.txt'adding'myhello-1.0.dist-info/RECORD'removingbuild/bdist.macosx-11.0-arm64/wheel(practice)tangsiqi@tangsiqideMacBook-Prodemo%lsbuilddistmyhellomyhello.egg-infosetup.py(practice)tangsiqi@tangsiqideMacBook-Prodemo%cddist(practice)tangsiqi@tangsiqideMacBook-Prodist%piplist|grepmy(practice)tangsiqi@tangsiqideMacBook-Prodist%pipinstallmyhello-1.0-py3-none-any.whlLookinginindexes:https://pypi.tuna.tsinghua.edu.cn/simpleProcessing./myhello-1.0-py3-none-any.whlInstallingcollectedpackages:myhelloSuccessfullyinstalledmyhello-1.0(practice)tangsiqi@tangsiqideMacBook-Prodist%piplist|grepmymyhello1.0(practice)tangsiqi@tangsiqideMacBook-Prodist%pythonPython3.8.17|packagedbyconda-forge|(default,Jun162023,07:11:32)[Clang14.0.6]ondarwinType"help","copyright","credits"or"license"formoreinformation.>>>frommyhelloimporthello>>>hello.hello_func()HelloWorld>>>exit()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071快捷操作–打包|安装pythonsetup.pyinstall1(practice)tangsiqi@tangsiqideMacBook-Prodemo%lsmyhellosetup.py(practice)tangsiqi@tangsiqideMacBook-Prodemo%pythonsetup.pyinstallrunninginstall/Users/tangsiqi/miniconda3/envs/practice/lib/python3.8/site-packages/setuptools/_distutils/cmd.py:66:SetuptoolsDeprecationWarning:setup.pyinstallisdeprecated.!!********************************************************************************Pleaseavoidrunning``setup.py``directly.Instead,usepypa/build,pypa/installerorotherstandards-basedtools.Seehttps://blog.ganssle.io/articles/2021/10/setup-py-deprecated.htmlfordetails.********************************************************************************!!self.initialize_options()/Users/tangsiqi/miniconda3/envs/practice/lib/python3.8/site-packages/setuptools/_distutils/cmd.py:66:EasyInstallDeprecationWarning:easy_installcommandisdeprecated.!!********************************************************************************Pleaseavoidrunning``setup.py``and``easy_install``.Instead,usepypa/build,pypa/installerorotherstandards-basedtools.Seehttps://github.com/pypa/setuptools/issues/917fordetails.********************************************************************************!!self.initialize_options()runningbdist_eggrunningegg_infocreatingmyhello.egg-infowritingmyhello.egg-info/PKG-INFOwritingdependency_linkstomyhello.egg-info/dependency_links.txtwritingtop-levelnamestomyhello.egg-info/top_level.txtwritingmanifestfile'myhello.egg-info/SOURCES.txt'readingmanifestfile'myhello.egg-info/SOURCES.txt'writingmanifestfile'myhello.egg-info/SOURCES.txt'installinglibrarycodetobuild/bdist.macosx-11.0-arm64/eggrunninginstall_librunningbuild_pycreatingbuildcreatingbuild/libcreatingbuild/lib/myhellocopyingmyhello/hello.py->build/lib/myhellocopyingmyhello/__init__.py->build/lib/myhellocreatingbuild/bdist.macosx-11.0-arm64creatingbuild/bdist.macosx-11.0-arm64/eggcreatingbuild/bdist.macosx-11.0-arm64/egg/myhellocopyingbuild/lib/myhello/hello.py->build/bdist.macosx-11.0-arm64/egg/myhellocopyingbuild/lib/myhello/__init__.py->build/bdist.macosx-11.0-arm64/egg/myhellobyte-compilingbuild/bdist.macosx-11.0-arm64/egg/myhello/hello.pytohello.cpython-38.pycbyte-compilingbuild/bdist.macosx-11.0-arm64/egg/myhello/__init__.pyto__init__.cpython-38.pyccreatingbuild/bdist.macosx-11.0-arm64/egg/EGG-INFOcopyingmyhello.egg-info/PKG-INFO->build/bdist.macosx-11.0-arm64/egg/EGG-INFOcopyingmyhello.egg-info/SOURCES.txt->build/bdist.macosx-11.0-arm64/egg/EGG-INFOcopyingmyhello.egg-info/dependency_links.txt->build/bdist.macosx-11.0-arm64/egg/EGG-INFOcopyingmyhello.egg-info/top_level.txt->build/bdist.macosx-11.0-arm64/egg/EGG-INFOzip_safeflagnotset;analyzingarchivecontents...creatingdistcreating'dist/myhello-1.0-py3.8.egg'andadding'build/bdist.macosx-11.0-arm64/egg'toitremoving'build/bdist.macosx-11.0-arm64/egg'(andeverythingunderit)Processingmyhello-1.0-py3.8.eggCopyingmyhello-1.0-py3.8.eggto/Users/tangsiqi/miniconda3/envs/practice/lib/python3.8/site-packagesAddingmyhello1.0toeasy-install.pthfileInstalled/Users/tangsiqi/miniconda3/envs/practice/lib/python3.8/site-packages/myhello-1.0-py3.8.eggProcessingdependenciesformyhello==1.0Finishedprocessingdependenciesformyhello==1.012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970python使用setuptools打包特性文件setup.py、MANIFEST.in、setup.cfg引入非Python文件上例中,我们只会将”myhello”包下的源码打包,如果想引入静态文件,如JS、CSS、图片等,怎么做?答:在项目根目录下添加一个”MANIFEST.in”文件夹。假设我们把所有静态文件都放在”static”子目录下,现在的项目结构如下:demo/├setup.py#安装文件├MANIFEST.in#清单文件└myhello/#源代码├static/#静态文件目录├__init__.py...12345678我们在清单文件”MANIFEST.in”中,列出想要在包内引入的目录路径:recursive-includemyhello/static*recursive-includemyhello/xxx*12recursive-include表明包含子目录在”setup.py”中将include_package_data参数设为True:#coding:utf8fromsetuptoolsimportsetupsetup(name='MyHello',#项目名version='1.0',#版本号packages=['myhello'],#包括在安装包内的Python包include_package_data=True#启用清单文件MANIFEST.in)12345678如果你想排除一部分文件,可以在”setup.py”中使用exclude_package_date参数:setup(...include_package_data=True,#启用清单文件MANIFEST.inexclude_package_date={'':['.gitignore']})12345依赖管理我们的项目会依赖其他Python模块,如何在setup.py中管理这些依赖呢?答:修改”setup.py”文件,加入install_requires参数#coding:utf8fromsetuptoolsimportsetupsetup(name='MyHello',#项目名version='1.0',#版本号packages=['myhello'],#包括在安装包内的Python包include_package_data=True,#启用清单文件MANIFEST.inexclude_package_date={'':['.gitignore']},install_requires=[#依赖列表'Flask>=0.10','Flask-SQLAlchemy>=1.5,=0.10','Flask-SQLAlchemy>=1.5,=0.10','Flask-SQLAlchemy>=1.5,
|
|