利用setuptools打包python程序的方法步骤
一、准备工程文件
1.创建工程leeoo
2.在工程根目录下创建setup.py文件
3.在工程根目录下创建同名package
二、编辑setup.py
1.编辑setup.py文件
fromsetuptoolsimportsetup,find_packages setup( name='leeoo',#包的名称 version='1.0',#版本号 packages=find_packages(),#动态获取packages description="leeoopackage", author='Leo', author_email='leo4774177@gmail.com', url="None", )
2.参数说明
三、编写测试代码
1.在leeoopackage下创建pkg
2.test.py文件内容
deftestfunc(): print("Thisisatestfunction..") classTestClass(object): def__init__(self,name): self.name=name print("ThisisatestClass..") defget_name(self): returnself.name
3.将test.py中的内容全部导入到leeoo的__init__.py中
这样,以后importleeoo后,就可以直接使用leeoo.testfunc()了。
四、打包
1.命令行进入工程根目录
2.运行命令
(venv)D:\pycharm_workspace\leeoo>pythonsetup.pycheck runningcheck
(venv)D:\pycharm_workspace\leeoo>pythonsetup.pybdist_egg runningbdist_egg runningegg_info creatingleeoo.egg-info writingleeoo.egg-info\PKG-INFO writingdependency_linkstoleeoo.egg-info\dependency_links.txt writingtop-levelnamestoleeoo.egg-info\top_level.txt writingmanifestfile'leeoo.egg-info\SOURCES.txt' readingmanifestfile'leeoo.egg-info\SOURCES.txt' writingmanifestfile'leeoo.egg-info\SOURCES.txt' installinglibrarycodetobuild\bdist.win-amd64\egg runninginstall_lib runningbuild_py creatingbuild creatingbuild\lib creatingbuild\lib\leeoo copyingleeoo\__init__.py->build\lib\leeoo creatingbuild\bdist.win-amd64 creatingbuild\bdist.win-amd64\egg creatingbuild\bdist.win-amd64\egg\leeoo copyingbuild\lib\leeoo\__init__.py->build\bdist.win-amd64\egg\leeoo byte-compilingbuild\bdist.win-amd64\egg\leeoo\__init__.pyto__init__.cpython-37.pyc creatingbuild\bdist.win-amd64\egg\EGG-INFO copyingleeoo.egg-info\PKG-INFO->build\bdist.win-amd64\egg\EGG-INFO copyingleeoo.egg-info\SOURCES.txt->build\bdist.win-amd64\egg\EGG-INFO copyingleeoo.egg-info\dependency_links.txt->build\bdist.win-amd64\egg\EGG-INFO copyingleeoo.egg-info\top_level.txt->build\bdist.win-amd64\egg\EGG-INFO zip_safeflagnotset;analyzingarchivecontents... creatingdist creating'dist\leeoo-1.0-py3.7.egg'andadding'build\bdist.win-amd64\egg'toit removing'build\bdist.win-amd64\egg'(andeverythingunderit)
3.查看生成的文件
在工程根目录下,可以看到生成了一系列文件:
五、安装leeoo
1.在工程目录下(setup.py所在目录)运行命令
D:\pycharm_workspace\leeoo>pythonsetup.pyinstall runninginstall runningbdist_egg runningegg_info writingleeoo.egg-info\PKG-INFO writingdependency_linkstoleeoo.egg-info\dependency_links.txt writingtop-levelnamestoleeoo.egg-info\top_level.txt readingmanifestfile'leeoo.egg-info\SOURCES.txt' writingmanifestfile'leeoo.egg-info\SOURCES.txt' installinglibrarycodetobuild\bdist.win-amd64\egg runninginstall_lib runningbuild_py creatingbuild\bdist.win-amd64\egg creatingbuild\bdist.win-amd64\egg\leeoo copyingbuild\lib\leeoo\__init__.py->build\bdist.win-amd64\egg\leeoo byte-compilingbuild\bdist.win-amd64\egg\leeoo\__init__.pyto__init__.cpython-37.pyc creatingbuild\bdist.win-amd64\egg\EGG-INFO copyingleeoo.egg-info\PKG-INFO->build\bdist.win-amd64\egg\EGG-INFO copyingleeoo.egg-info\SOURCES.txt->build\bdist.win-amd64\egg\EGG-INFO copyingleeoo.egg-info\dependency_links.txt->build\bdist.win-amd64\egg\EGG-INFO copyingleeoo.egg-info\top_level.txt->build\bdist.win-amd64\egg\EGG-INFO zip_safeflagnotset;analyzingarchivecontents... creating'dist\leeoo-1.0-py3.7.egg'andadding'build\bdist.win-amd64\egg'toit removing'build\bdist.win-amd64\egg'(andeverythingunderit) Processingleeoo-1.0-py3.7.egg Copyingleeoo-1.0-py3.7.eggtod:\dev_apps\anaconda5.3.0\lib\site-packages Addingleeoo1.0toeasy-install.pthfile Installedd:\dev_apps\anaconda5.3.0\lib\site-packages\leeoo-1.0-py3.7.egg Processingdependenciesforleeoo==1.0 Finishedprocessingdependenciesforleeoo==1.0
2.查看安装好的文件
我们看到上述打印日志中,将leeoo-1.0-py3.7.egg安装到了d:\dev_apps\anaconda5.3.0\lib\site-packages。
六、使用leeoo
新建一个项目,然后导入leeoo:
importleeoo leeoo.testfunc() obj=leeoo.TestClass("demo") print(obj.get_name())
也可以使用:
fromleeoo.pkgimporttest test.testfunc() obj=test.TestClass("demo") print(obj.get_name())
当然也可以直接将testfunc()和TestClass导入,但是容易引起命名冲突。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。