利用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读写文本文件及编写简单的文本编辑器
Mar 11 Python
Python编程修改MP3文件名称的方法
Apr 19 Python
python微信跳一跳游戏辅助代码解析
Jan 29 Python
Python实现对一个函数应用多个装饰器的方法示例
Feb 09 Python
Python实现的爬取百度贴吧图片功能完整示例
May 10 Python
详解利用Python scipy.signal.filtfilt() 实现信号滤波
Jun 05 Python
深入解析神经网络从原理到实现
Jul 26 Python
Atom Python 配置Python3 解释器的方法
Aug 28 Python
解决Jupyter-notebook不弹出默认浏览器的问题
Mar 30 Python
Python pygame实现中国象棋单机版源码
Jun 20 Python
详解MindSpore自定义模型损失函数
Jun 30 Python
对象析构函数__del__在Python中何时使用
Mar 22 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
php中使用gd库实现下载网页中所有图片
2015/05/12 PHP
PHP页面跳转操作实例分析(header方法)
2016/09/28 PHP
PHP基于DOMDocument解析和生成xml的方法分析
2017/07/17 PHP
Laravel 实现密码重置功能
2018/02/23 PHP
解决laravel上传图片之后,目录有图片,但是访问不到(404)的问题
2019/10/14 PHP
PHP如何开启Opcache功能提升程序处理效率
2020/04/27 PHP
自适应图片大小的弹出窗口
2006/07/27 Javascript
验证用户是否修改过页面的数据的实现方法
2008/09/26 Javascript
Dom与浏览器兼容性说明
2010/10/25 Javascript
基于jquery的15款幻灯片插件
2011/04/10 Javascript
Javascript Memoizer浅析
2014/10/16 Javascript
jQuery form 表单验证插件(fieldValue)校验表单
2016/01/24 Javascript
jQuery中$.each()函数的用法引申实例
2016/05/12 Javascript
JavaScript鼠标事件,点击鼠标右键,弹出div的简单实例
2016/08/03 Javascript
JS中的hasOwnProperty()、propertyIsEnumerable()和isPrototypeOf()
2016/08/11 Javascript
浅谈Angular.js中使用$watch监听模型变化
2017/01/10 Javascript
vue2滚动条加载更多数据实现代码
2017/01/10 Javascript
js仿QQ邮箱收件人选择与搜索功能
2017/02/10 Javascript
微信小程序实现自上而下字幕滚动
2018/07/14 Javascript
通过图带你深入了解vue的响应式原理
2019/06/21 Javascript
[05:34]2014DOTA2国际邀请赛中国区预选赛精彩TOPPLAY第二弹
2014/06/25 DOTA
Python程序中设置HTTP代理
2016/11/06 Python
Python中常见的异常总结
2018/02/20 Python
python 读入多行数据的实例
2018/04/19 Python
用Python将mysql数据导出成json的方法
2018/08/21 Python
利用arcgis的python读取要素的X,Y方法
2018/12/22 Python
Python多线程原理与用法实例剖析
2019/01/22 Python
Python中的list与tuple集合区别解析
2019/10/12 Python
python实现字符串和数字拼接
2020/03/02 Python
Python打印不合法的文件名
2020/07/31 Python
Python自动巡检H3C交换机实现过程解析
2020/08/14 Python
编译 pycaffe时报错:fatal error: numpy/arrayobject.h没有那个文件或目录
2020/11/29 Python
英国文具、办公用品和科技商店:Ryman
2018/09/27 全球购物
生日派对邀请函
2014/01/13 职场文书
支部书记四风对照材料
2014/08/28 职场文书
Java多条件判断场景中规则执行器的设计
2021/06/26 Java/Android