python程序封装成exe可执行文件
1. 所需环境
需要用到python环境
(本人测试环境 python2.7 python3.8)
需要先安装 pyinstaller 指令: pip install pyinstaller
(注意是pyinstaller 而不是 pyinstall)
然后cmd 打开py文件所在 使用 pyinstaller -F ***.py 就会打包成exe文件可执行了
2. 打包结果
打包完成后,会多出build和dist两个文件夹
封住好的exe文件在dist中
将exe放到想使用的目录文件夹,点击使用即可
3. 可能会遇到打包后乱码问题
1、在每个含有中文的py文件中的开头加上#-- encoding:UTF-8 --
2、将需要打包的源文件内的中文输出和输入全部改写为以下的格式:
输出
print unicode(‘成功:’,‘utf-8’)
print unicode(strName,‘utf-8’) #strName 是字符串
输入
n=raw_input(unicode(‘请输入文字’,‘utf-8’).encode(‘gbk’))
print n
4. 附赠两个python脚本:
① 读取当前文件夹下每个文件并显示
② 将当前文件夹下所有csv文件编码统一更改为utf-8编码
① python读取文件夹下每个文件并显示的代码
# coding=utf-8
import os.path
import time
# path = input('Enter files path:\n')
# speed = input('请输入显示速度(最大为1):\n', 'utf-8')
# 上面是显示utf-8编码
speed = input(unicode('请输入显示速度(最大为1):\n', 'utf-8').encode('gbk'))
# 上面是显示gbk编码
path = './src'
files = os.listdir(path)
while True:
for single_file in files:
file_path = path + '/' + single_file
with open(file_path, 'r') as f:
for line in f:
time.sleep(speed)
print unicode(line, 'utf-8'),
② 将当前文件夹下所有csv文件编码统一更改为utf-8编码
# 批量更改csv文件编码为utf-8
# i!/usr/bin/env python3
# -*- coding:utf-8 -*-
import os
import sys
import codecs
import chardet
def convert(filename, out_enc="UTF-8"):
try:
content = codecs.open(filename, 'rb').read()
source_encoding = chardet.detect(content)['encoding']
print("file encoding:%s" % source_encoding)
if source_encoding != None:
content = content.decode(source_encoding).encode(out_enc)
codecs.open(filename, 'wb').write(content)
print(filename, "更改为utf-8")
# content.close()
else:
print("can not recgonize file encoding %s" % filename)
except IOError as err:
print("I/O error:{0}".format(err))
def explore(dir):
for root, dirs, files in os.walk(dir):
for file in files:
if os.path.splitext(file)[1] == '.csv':
print("filename:%s" % file)
path = os.path.join(root, file)
convert(path)
def main():
# explore(os.getcwd())
filepath = r"../.././import"
explore(filepath)
if __name__ == "__main__":
main()