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

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

off999 2024-09-16 00:53 41 浏览 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-

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

相关推荐

无线ap图片(无线ap图标)

WiFi热点中的ap标识即AccessPoint,也就是无线接入点。简单来说就是wifi共享上网中的无线交换机,它是移动终端用户进入有线网络的接入点,主要用于家庭宽带、企业内部网络部署等,可以使无线...

路由器初始密码忘了怎么办(路由器忘记原始密码怎么办)

路由器密码忘了可以通过恢复出厂设置重新设置密码1、把所有网线都从路由器上拔掉,只保留电源线既可;    2、然后用稍尖的笔尖刺紧路由器背面的“RESET”小孔不放;    3、有的是“RESET”...

hotmail邮箱还能用吗(hotmail邮箱登录有手机客户端的吗)

这个是可以重新申请的呢除了谷歌国内受限,其他基本都可以正常使用。看个人使用习惯可自主申请相应邮箱:微软outlook、hotmail邮箱;网易邮箱、网易126邮箱;新浪邮箱、阿里邮箱;QQ邮箱、搜狐...

怎么建立局域网(怎么建立局域网内其他电脑文件夹的快捷方式)
  • 怎么建立局域网(怎么建立局域网内其他电脑文件夹的快捷方式)
  • 怎么建立局域网(怎么建立局域网内其他电脑文件夹的快捷方式)
  • 怎么建立局域网(怎么建立局域网内其他电脑文件夹的快捷方式)
  • 怎么建立局域网(怎么建立局域网内其他电脑文件夹的快捷方式)
diskdigger官网入口(diskinfo官网)

打开LaunchCenterPro,创建一个叫Omnifocus的操作组,然后再往这个操作组添加新的操作。如果你要在Omnifocus创建新收件箱项,添加URL到LaunchCenter...

最新英特尔处理器排名(最新英特尔处理器排名第几)

一、英特尔酷睿i7670。这款英特尔CPU采用的是超频新芯,最大程度的提升处理器的超频能力。二、英特尔酷睿i74790kCPU:这款CPU采用22纳米制程工艺的框架,它的默认频率是4.0到4.4Ghz...

电脑怎样激活win10系统(电脑怎么激活window10)
  • 电脑怎样激活win10系统(电脑怎么激活window10)
  • 电脑怎样激活win10系统(电脑怎么激活window10)
  • 电脑怎样激活win10系统(电脑怎么激活window10)
  • 电脑怎样激活win10系统(电脑怎么激活window10)
nvidia旧版本驱动下载(nvidia新版本驱动)
nvidia旧版本驱动下载(nvidia新版本驱动)

没法装,n卡本身不具备装旧版驱动的功能一、首先在本机电脑内鼠标左键双击打开“驱动人生”(若电脑上无此软件,可以在各大软件市场内下载安装)。二、打开驱动人生软件后,点击“立即体检”进行驱动扫描。三、驱动扫描完成后,点击显卡右边的“箭头”打开驱...

2025-12-18 20:51 off999

怎么解开别人的wifi密码(如何解开别人的wifi密码)

别人的无线网络密码是很不容易破解的,如果人家是愿意分享的,可以在手机上下载"Wifi万能钥匙"注册登陆成功后连接其无线wifi1、以现有的技术手段,是没有办法破解WPA的加密方式(现在...

电脑突然关机(电脑突然关机像断电了一样 再也打不开)
电脑突然关机(电脑突然关机像断电了一样 再也打不开)

如果是插电源的电脑开着突然就关机了,可能是线路接触不良或者是没电了,导致的开着就关机了,如果是你的电脑是充电的那一种可能是你的电池的电量用完了或者是电池的线路接触不良导致的开着突然就关机了,你可以排查一下线路。1、如果你使用的是笔记本电脑,...

2025-12-18 19:51 off999

win7重装系统一直反复重启(win7重装系统无限重启)

WIN7的系统装重复了,可以将原安装的系统删除,方法如下:1、如果以前的windows是安装在C盘上的话,点击桌面上的计算机,选中C盘,鼠标右键选择属性;2、点磁盘清理;3、点清理系统文件,点确定;4...

电脑如何格式化sd卡(电脑格式化sd卡,提示写有保护)

要在电脑上格式化SD卡,可以按照以下步骤:1.将SD卡插入计算机的SD卡读卡器中。2.打开“我的电脑”或“此电脑”,找到SD卡在计算机上的驱动器号(比如E盘)。3.右键单击SD卡驱动器,选择“格...

系统检测不到机械硬盘(系统检测不到机械硬盘怎么办)

第一,我们需要确认一下机械硬盘是否连接正常。可以检查一下硬盘的电源线和数据线是否插紧,是否松动或者断开。如果发现有松动或者断开的情况,可以重新插上并确保插紧。如果硬盘连接正常,但电脑仍然无法读取,那么...

路由器管理平台登录(路由器管理平台登录网址)

路由器的用户登录入口地址是:tplogin.cn电信运营商定制款登录地址是:192.168.2.1或者192.168.8.12、华为(容易)路由器华为路由器跟荣耀路由器只有IP地址,没有域名,它是...

directx修复(DirectX修复工具官网下载)

使用DirectX修复工具很简单。首先需要下载并安装工具,然后打开工具并按照界面提示进行操作即可。工具的作用是自动检测系统中可能存在的DirectX问题,并尝试修复它们,从而保证计算机游戏等应用程序的...

取消回复欢迎 发表评论: