利用setuptools打包python程序的方法步骤


Posted in Python onJanuary 18, 2020

一、准备工程文件

1.创建工程leeoo

利用setuptools打包python程序的方法步骤

2.在工程根目录下创建setup.py文件

利用setuptools打包python程序的方法步骤

3.在工程根目录下创建同名package

利用setuptools打包python程序的方法步骤

二、编辑setup.py

1.编辑setup.py文件

from setuptools import setup, find_packages

setup(
  name='leeoo', # 包的名称
  version='1.0', # 版本号
  packages=find_packages(), # 动态获取packages
  description="leeoo package",
  author='Leo',
  author_email='leo4774177@gmail.com',
  url="None",
)

2.参数说明

利用setuptools打包python程序的方法步骤

三、编写测试代码

1.在leeoo package下创建pkg

利用setuptools打包python程序的方法步骤

2.test.py文件内容

def testfunc():
  print("This is a test function..")


class TestClass(object):
  def __init__(self, name):
    self.name = name
    print("This is a test Class..")

  def get_name(self):
    return self.name

3.将test.py中的内容全部导入到leeoo的__init__.py中

利用setuptools打包python程序的方法步骤

这样,以后import leeoo后,就可以直接使用leeoo.testfunc()了。

四、打包

1.命令行进入工程根目录

利用setuptools打包python程序的方法步骤

2.运行命令

(venv) D:\pycharm_workspace\leeoo>python setup.py check
running check
(venv) D:\pycharm_workspace\leeoo>python setup.py bdist_egg
running bdist_egg
running egg_info
creating leeoo.egg-info
writing leeoo.egg-info\PKG-INFO
writing dependency_links to leeoo.egg-info\dependency_links.txt
writing top-level names to leeoo.egg-info\top_level.txt
writing manifest file 'leeoo.egg-info\SOURCES.txt'
reading manifest file 'leeoo.egg-info\SOURCES.txt'
writing manifest file 'leeoo.egg-info\SOURCES.txt'
installing library code to build\bdist.win-amd64\egg
running install_lib
running build_py
creating build
creating build\lib
creating build\lib\leeoo
copying leeoo\__init__.py -> build\lib\leeoo
creating build\bdist.win-amd64
creating build\bdist.win-amd64\egg
creating build\bdist.win-amd64\egg\leeoo
copying build\lib\leeoo\__init__.py -> build\bdist.win-amd64\egg\leeoo
byte-compiling build\bdist.win-amd64\egg\leeoo\__init__.py to __init__.cpython-37.pyc
creating build\bdist.win-amd64\egg\EGG-INFO
copying leeoo.egg-info\PKG-INFO -> build\bdist.win-amd64\egg\EGG-INFO
copying leeoo.egg-info\SOURCES.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying leeoo.egg-info\dependency_links.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying leeoo.egg-info\top_level.txt -> build\bdist.win-amd64\egg\EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating dist
creating 'dist\leeoo-1.0-py3.7.egg' and adding 'build\bdist.win-amd64\egg' to it
removing 'build\bdist.win-amd64\egg' (and everything under it)

3.查看生成的文件

在工程根目录下,可以看到生成了一系列文件:

利用setuptools打包python程序的方法步骤

五、安装leeoo

1.在工程目录下(setup.py所在目录)运行命令

D:\pycharm_workspace\leeoo>python setup.py install
running install
running bdist_egg
running egg_info
writing leeoo.egg-info\PKG-INFO
writing dependency_links to leeoo.egg-info\dependency_links.txt
writing top-level names to leeoo.egg-info\top_level.txt
reading manifest file 'leeoo.egg-info\SOURCES.txt'
writing manifest file 'leeoo.egg-info\SOURCES.txt'
installing library code to build\bdist.win-amd64\egg
running install_lib
running build_py
creating build\bdist.win-amd64\egg
creating build\bdist.win-amd64\egg\leeoo
copying build\lib\leeoo\__init__.py -> build\bdist.win-amd64\egg\leeoo
byte-compiling build\bdist.win-amd64\egg\leeoo\__init__.py to __init__.cpython-37.pyc
creating build\bdist.win-amd64\egg\EGG-INFO
copying leeoo.egg-info\PKG-INFO -> build\bdist.win-amd64\egg\EGG-INFO
copying leeoo.egg-info\SOURCES.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying leeoo.egg-info\dependency_links.txt -> build\bdist.win-amd64\egg\EGG-INFO
copying leeoo.egg-info\top_level.txt -> build\bdist.win-amd64\egg\EGG-INFO
zip_safe flag not set; analyzing archive contents...
creating 'dist\leeoo-1.0-py3.7.egg' and adding 'build\bdist.win-amd64\egg' to it
removing 'build\bdist.win-amd64\egg' (and everything under it)
Processing leeoo-1.0-py3.7.egg
Copying leeoo-1.0-py3.7.egg to d:\dev_apps\anaconda5.3.0\lib\site-packages
Adding leeoo 1.0 to easy-install.pth file

Installed d:\dev_apps\anaconda5.3.0\lib\site-packages\leeoo-1.0-py3.7.egg
Processing dependencies for leeoo==1.0
Finished processing dependencies for leeoo==1.0

2.查看安装好的文件

我们看到上述打印日志中,将leeoo-1.0-py3.7.egg安装到了d:\dev_apps\anaconda5.3.0\lib\site-packages。

利用setuptools打包python程序的方法步骤

六、使用leeoo

新建一个项目,然后导入leeoo:

import leeoo

leeoo.testfunc()
obj = leeoo.TestClass("demo")
print(obj.get_name())

也可以使用:

from leeoo.pkg import test

test.testfunc()
obj = test.TestClass("demo")
print(obj.get_name())

当然也可以直接将testfunc()和TestClass导入,但是容易引起命名冲突。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python实现搜索本地文件信息写入文件的方法
Feb 22 Python
Python数据可视化库seaborn的使用总结
Jan 15 Python
Python实现的登录验证系统完整案例【基于搭建的MVC框架】
Apr 12 Python
python基础教程之while循环
Aug 14 Python
Python数据库小程序源代码
Sep 15 Python
Python破解BiliBili滑块验证码的思路详解(完美避开人机识别)
Feb 17 Python
python mysql 字段与关键字冲突的解决方式
Mar 02 Python
python3 实现口罩抽签的功能
Mar 11 Python
python输出结果刷新及进度条的实现操作
Jul 13 Python
Windows 平台做 Python 开发的最佳组合(推荐)
Jul 27 Python
总结python多进程multiprocessing的相关知识
Jun 29 Python
Python List remove()实例用法详解
Aug 02 Python
python计算二维矩形IOU实例
Jan 18 #Python
解决python replace函数替换无效问题
Jan 18 #Python
使用Python来做一个屏幕录制工具的操作代码
Jan 18 #Python
pytorch 状态字典:state_dict使用详解
Jan 17 #Python
Python标准库itertools的使用方法
Jan 17 #Python
Python实现投影法分割图像示例(二)
Jan 17 #Python
Python常用库大全及简要说明
Jan 17 #Python
You might like
桌面中心(二)数据库写入
2006/10/09 PHP
php将数据库中的电话号码读取出来并生成图片
2008/08/31 PHP
php解析html类库simple_html_dom(详细介绍)
2013/07/05 PHP
PHP中SQL查询语句的id=%d解释(推荐)
2016/12/10 PHP
多个Laravel项目如何共用migrations详解
2018/09/25 PHP
如何在PHP中使用数组
2020/06/09 PHP
jquery 学习之二 属性(类)
2010/11/25 Javascript
js 静态动态成员 and 信息的封装和隐藏
2011/05/29 Javascript
JavaScript操纵窗口的方法小结
2013/06/28 Javascript
巧用局部变量提升javascript性能
2014/02/24 Javascript
localResizeIMG先压缩后使用ajax无刷新上传(移动端)
2015/08/11 Javascript
Jquery+ajax+JAVA(servlet)实现下拉菜单异步取值
2016/03/23 Javascript
jQuery实现点击按钮文字变成input框点击保存变成文字
2016/05/09 Javascript
JS中关于事件处理函数名后面是否带括号的问题
2016/11/16 Javascript
Centos6.8下Node.js安装教程
2017/05/12 Javascript
详解html-webpack-plugin插件(用法总结)
2018/09/12 Javascript
jQuery子选择器与可见性选择器实例分析
2019/06/28 jQuery
antd中table展开行默认展示,且不需要前边的加号操作
2020/11/02 Javascript
在Python中操作字典之setdefault()方法的使用
2015/05/21 Python
Python实现字符串与数组相互转换功能示例
2017/09/22 Python
matplotlib在python上绘制3D散点图实例详解
2017/12/09 Python
python 多线程将大文件分开下载后在合并的实例
2018/11/09 Python
Python实现操纵控制windows注册表的方法分析
2019/05/24 Python
解决pycharm remote deployment 配置的问题
2019/06/27 Python
如何通过雪花算法用Python实现一个简单的发号器
2019/07/03 Python
python默认参数调用方法解析
2020/02/09 Python
基于python纯函数实现井字棋游戏
2020/05/27 Python
详解Python 中的容器 collections
2020/08/17 Python
纯CSS3实现漂亮的input输入框动画样式库(Text input love)
2018/12/29 HTML / CSS
纯CSS3实现的井字棋游戏
2020/11/25 HTML / CSS
自我鉴定三原则
2014/01/13 职场文书
党的群众路线教育实践活动对照检查材料(教师)
2014/09/24 职场文书
2014感恩节演讲稿大全
2014/10/11 职场文书
2014年优质护理服务工作总结
2014/11/14 职场文书
Go缓冲channel和非缓冲channel的区别说明
2021/04/25 Golang
MySQL 存储过程的优缺点分析
2021/05/20 MySQL