Python打包exe程序一行简单的代码为什么就是那么多人不知道?

版权声明:禁止转载至其它平台,转载至博客需带上此文链接。 https://blog.csdn.net/qq_41841569/article/details/89845154

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

img

只是一个简单的demo,一个demo项目中,有一个hello文件,文件中有一个函数hello,函数的作用是读取testdd.txt文件中的数据然后输出。
image
当然在学习Python的道路上肯定会困难,没有好的学习资料,怎么去学习呢?  学习Python中有不明白推荐加入交流群号:984137898 群里有志同道合的小伙伴,互帮互助,  群里有不错的视频学习教程和PDF!
这个项目中还有其他的一些东西,以演示打包。

整个项目结构如下:

singledemo
demo
model
init.py
entity.py
static
test.txt
hello.py
MANIFEST.in
setup.py
hello.py中的代码:

def hello():
f = open('testdd.txt', 'r')
content = f.read()
f.close()
print content
</pre>
setup.py中的代码如下:

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',
)

还有一个文件需要注意,MANIFEST.in:

recursive-include demo *.txt

虽然只有一句话,但是是要通过它来包括你要打包的非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,输出结果。

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

猜你喜欢

转载自blog.csdn.net/qq_41841569/article/details/89845154