百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

一行简单的代码就能实现Python打包exe,为什么那么多人不知道?

off999 2024-09-16 00:53 26 浏览 0 评论

关于python程序打包的介绍就不多说了,大部分的python基础书上都有介绍。这里就直接演练。



只是一个简单的demo,一个demo项目中,有一个hello文件,文件中有一个函数hello,函数的作用是读取testdd.txt文件中的数据然后输出。

这个项目中还有其他的一些东西,以演示打包。

整个项目结构如下:


<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
singledemo
demo
model
init.py
entity.py
static
test.txt
hello.py
MANIFEST.in
setup.py
</pre>
hello.py中的代码:
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
def hello():
f = open('testdd.txt', 'r')
content = f.read()
f.close()
print content
</pre>
setup.py中的代码如下:
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
coding:utf-8
'''
'''
import os
import sys
from setuptools import setup, find_packages
setup(
name = "demo",
version = "0.0.1",
packages = find_packages(),
include_package_data = True,
entry_points = {
'console_scripts' : [
'demo = demo.hello:hello'
],
},
package_data = {
'demo':['*.txt']
},
author = "the5fire",
author_email = '嘻嘻嘻嘻嘻嘻嘻@email.com',
url = "http://www.the5fire.com",
description = 'a demo for setuptools',
)
</pre>
还有一个文件需要注意,MANIFEST.in:
<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
recursive-include demo *.txt
</pre>

虽然只有一句话,但是是要通过它来包括你要打包的非py文件。

打包时候的命令有两个,

一个是打包成egg文件:python setup.py bdist_egg 。执行完成后,会在同目录下多了两个文件夹:demo.egg-info和dist,egg文件就在dist中,这个文件可以上传到pypi.python.com上,供大家下载。或者上传到某网盘,通过pip install --no--index find-links=[url]来下载。

另外一种是打包成压缩文件形式:python setup.py sdist 。执行结果同上,不过文件格式不同。

打包完成之后,当然要安装了,上一篇介绍了virtualenv,创建一个虚拟环境以供测试。然后执行python setup.py install 就会在你的虚拟环境的bin下创建一个demo的可执行文件,你在虚拟环境中运行:demo,输出结果。

很简单的东西,但是需要参考。

-END-

更多资讯请关注猿来如此呀账号,或点击下方链接了解更多资讯

相关推荐

实战:用 Python+Flask+Echarts 构建电商实时数据大屏

在电商运营中,实时掌握销售趋势、用户行为等核心数据是决策的关键。本文将从实战角度,详解如何用Python+Flask+Echarts技术栈,快速搭建一个支持实时更新、多维度可视化的电商数据大屏,帮...

DeepSeek完全使用手册:从新手到高手的2000字实操指南

一、工具定位与核心功能矩阵(200字)DeepSeek是一款专注于深度推理的强大AI助手,其功能丰富多样,可归纳为4大能力象限:plaintext差异化优势:DeepSeek支持最长达16Ktok...

Python绘制可爱的图表 cutecharts

一个很酷的python手绘样式可视化包——可爱的图表cutecharts。Cutecharts非常适合为图表提供更个性化的触感。Cutecharts与常规的Matplotlib和Seabo...

第十二章:Python与数据处理和可视化

12.1使用pandas进行数据处理12.1.1理论知识pandas是Python中最常用的数据处理库之一,它提供了高效的数据结构和数据分析工具。pandas的核心数据结构是Serie...

5分钟就能做一个Excel动态图表,你确定不学学?(纯gif教学)

本文说明下图是一个比较酷炫的Excel动态图表,最难的部分就是用到了一个复选框控件。其实这个控件我很早就见过,但是不会用呀!望洋兴叹。这次呢,我也是借着这个文章为大家讲述一下这个控件的使用。本文没有...

Python数据可视化:从Pandas基础到Seaborn高级应用

数据可视化是数据分析中不可或缺的一环,它能帮助我们直观理解数据模式和趋势。本文将全面介绍Python中最常用的三种可视化方法。Pandas内置绘图功能Pandas基于Matplotlib提供了简洁的绘...

如何使用 Python 将图表写入 Excel

将Python生成的图表写入Excel文件是数据分析和可视化中常见的需求。Python提供了多种库(如matplotlib、openpyxl和xlsxwriter)来实现这一功能。本文...

Excel 图表制作太痛苦?用 Python 生成动态交互图表

做个动态图表花了3小时?你该换方法了!上周帮销售部做季度汇报图表,Excel操作把我整崩溃了——插入折线图后发现数据源选错,重新选择又得调格式想做动态筛选图表,捣鼓"开发工具"...

Python Matplotlib 入门教程:可视化数据的基石

一、简介Matplotlib是Python中最流行的数据可视化库,提供从简单折线图到复杂3D图形的完整解决方案。其核心优势在于:o灵活性强:支持像素级样式控制o兼容性好:与NumPy、Pa...

20种Python数据可视化绘图 直接复制可用

本文介绍20种python数据绘图方法,可直接用于科研绘图或汇报用图。1.折线图(LinePlot)-描述数据随时间或其他变量的变化。importmatplotlib.pyplotasp...

Python os模块完全指南:轻松玩转文件管理与系统操作

Pythonos模块完全指南:轻松玩转文件管理与系统操作os模块是Python与操作系统对话的"瑞士军刀",学会它能让你轻松管理文件、操控路径、获取系统信息。本教程通过场景化案例+...

Python中h5py与netCDF4模块在Anaconda环境的下载与安装

本文介绍基于Anaconda环境,下载并安装Python中h5py与netCDF4这两个模块的方法。h5py与netCDF4这两个模块是与遥感图像处理、地学分析等GIS操作息息相关的模块,应用...

python中的模块、库、包有什么区别?

一文带你分清Python模块、包和库。一、模块Python模块(Module),是一个Python文件,以.py结尾,包含了Python对象定义和Python语句。模块能定义函数,类和变...

centos7 下面使用源码编译的方式安装python3.11

centos7下面使用源码编译的方式安装python3.11,步骤如下:cd/root#只是将python3.11的安装包下载到/root目录下wgethttps://www.python.o...

Python其实很简单 第十四章 模块

模块是一组程序代码,可以是别人已经写好的,也可以是自己编写的,但都是已经存在的,在编程时直接使用就可以了。模块机制的最大好处就是程序员不再编写重复的代码,而直接利用已有的成果,这样就能将更多的精力投入...

取消回复欢迎 发表评论: