利用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采用requests库模拟登录和抓取数据的简单示例
Jul 05 Python
Python中属性和描述符的正确使用
Aug 23 Python
玩转python selenium鼠标键盘操作(ActionChains)
Apr 12 Python
Python2和Python3中urllib库中urlencode的使用注意事项
Nov 26 Python
python3使用flask编写注册post接口的方法
Dec 28 Python
解决pycharm运行程序出现卡住scanning files to index索引的问题
Jun 27 Python
python导入不同目录下的自定义模块过程解析
Nov 18 Python
python判断无向图环是否存在的示例
Nov 22 Python
解决pytorch多GPU训练保存的模型,在单GPU环境下加载出错问题
Jun 23 Python
Python系统公网私网流量监控实现流程
Nov 23 Python
Python 按比例获取样本数据或执行任务的实现代码
Dec 03 Python
python软件测试Jmeter性能测试JDBC Request(结合数据库)的使用详解
Jan 26 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
如何提高MYSQL数据库的查询统计速度 select 索引应用
2007/04/11 PHP
php常量详细解析
2015/10/27 PHP
laravel框架中表单请求类型和CSRF防护实例分析
2019/11/23 PHP
对象特征检测法判断浏览器对javascript对象的支持
2009/07/25 Javascript
多浏览器兼容性比较好的复制到剪贴板的js代码
2011/10/09 Javascript
向当前style sheet中插入一个新的style实现方法
2013/04/01 Javascript
jQuery的deferred对象详解
2014/11/12 Javascript
JS实现自动定时切换的简洁网页选项卡效果
2015/10/13 Javascript
微信小程序 常用工具类详解及实例
2017/02/15 Javascript
jquery+css实现侧边导航栏效果
2017/06/12 jQuery
详解react-router如何实现按需加载
2017/06/15 Javascript
利用vue开发一个所谓的数独方法实例
2017/12/21 Javascript
React中的render何时执行过程
2018/04/13 Javascript
小程序实现带年月选取效果的日历
2018/06/27 Javascript
[45:06]完美世界DOTA2联赛PWL S2 Magma vs InkIce 第二场 11.28
2020/12/02 DOTA
pycharm 使用心得(七)一些实用功能介绍
2014/06/06 Python
Flask Web开发入门之文件上传(八)
2018/08/17 Python
Python 占位符的使用方法详解
2019/07/10 Python
django创建最简单HTML页面跳转方法
2019/08/16 Python
Tensorflow矩阵运算实例(矩阵相乘,点乘,行/列累加)
2020/02/05 Python
构建高效的python requests长连接池详解
2020/05/02 Python
浅谈matplotlib默认字体设置探索
2021/02/03 Python
python 利用panda 实现列联表(交叉表)
2021/02/06 Python
HTML5本地数据库基础操作详解
2016/04/26 HTML / CSS
HTML5不支持frameset的两种解决方法
2016/11/14 HTML / CSS
美国电子产品折扣网站:Daily Steals
2017/05/20 全球购物
彪马加拿大官网:PUMA加拿大
2018/10/04 全球购物
中式餐厅创业计划书范文
2014/01/23 职场文书
主题实践活动总结
2014/05/08 职场文书
党的群众路线教育实践活动领导班子整改措施
2014/10/28 职场文书
创卫工作总结2015
2015/04/22 职场文书
大学毕业晚会开场白
2015/05/29 职场文书
初中英语教师个人工作总结2015
2015/07/21 职场文书
创业计划书之个人工作室
2019/08/22 职场文书
原生CSS实现文字无限轮播的通用方法
2021/03/30 HTML / CSS
vue配置型表格基于el-table拓展之table-plus组件
2022/04/12 Vue.js