一、Ubuntu上python打包为可执行文件(包含pyecharts)
- python文件打包有很多方法,本文介绍重点介绍的是pyinstaller方法,本文也是记录自己在ubuntu上打包踩的坑!
1、首先先安装pyinstaller
#安装pyinstaller
1 pip install pyinstaller
#检查pyinstaller版本
2 pyinstaller --version
2、安装好pyinstaller后,开始按照网上教程直接打包py文件,进入要打包的py文件路径下,打开终端输入以下命令
1 pyinstaller -F xxx.py
- 运行后报以下错误
On Debian / Ubuntu,You would need to install Python development packages
apt-get install python3-dev
apt-get install python-dev
If you‘re building Python by yourself,please rebuild your Python with --enable-shared`
- 这个时候,就需要去安装以下python3-dev,但是由于ubuntu自带python2.7以及python3.5,自己又安装了python3.7,所以直接安装python3.7-dev就可以了
#打开终端输入
1 apt-get install python3.7-dev
- 安装好python3.7-dev后,在将用pip将development安装
1 pip install development
- 再用pyinstaller重新打包,这次可以打包成功,在py文件夹同路径下,生成build、disk两个文件夹加上一个spec文件,可执行文件就在disk文件夹内,打开终端进入到disk文件夹,./xxx运行打包好的文件
二、运行打包后的可执行文件时遇到问题(坑坑坑)
1、运行时,发现提示pyecharts模块没有打包到可执行文件里,后来百度之后才发现,pyinstaller模块没有对pyecharts模块提供响应的支持,所以百度了一些方法,需要在
/home/y/PycharmProjects/pythonProject2/venv/lib/python3.7/site-packages/PyInstaller/hooks 路径下增加如下文件:
文件名:hook-pyecharts.py
文件内容:
#-----------------------------------------------------------------------------
# Copyright (c) 2017-2020, PyInstaller Development Team.
#
# Distributed under the terms of the GNU General Public License (version 2
# or later) with exception for distributing the bootloader.
#
# The full license is in the file COPYING.txt, distributed with this software.
#
# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
#-----------------------------------------------------------------------------
# Hook for nanite: https://pypi.python.org/pypi/nanite
from PyInstaller.utils.hooks import collect_data_files
datas = collect_data_files('pyecharts')
- 然后删除刚刚打包的文件,用下列命令重新打包
pyinstaller -F WinPerformCollect.py --hidden-import cmath
- 再运行.py程序就可以正常运行了
2、由于代码运行结果包含在现有目录创建result文件夹,就报了os.makedirs创建文件夹没有权限错误
百度原因后:解释是根据官方Python documentation的os.makedirs功能模式参数可能在某些系统上被忽略,在哪里则不能忽略系统当前的umask值被屏蔽掉。
#将代码行里os.makedirs强制模式为0o777
os.makedirs(res_dir_path, 0o777)
又重新打包该.py文件后,可正常运行
本文为自己使用时遇到的一些问题,记录一下!
版权声明:本文为ayusimiao原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。