002 萝卜头学python:python 转成EXE

萝卜头社区:luobotou.net

方法有两种:

方法一:用pyinstaller

使用pyinstaller可以将你的.py文件直接转换成.exe格式,在没有部署python环境的windows机器上直接运行你的程序!!

废话不多说,直接上正文。

1. 在命令行用pip安装 pyinstaller包

     pip install pyinstaller

2.下载安装pyinstaler运行时所需要的windows扩展pywin32

        mhammond/pywin32

        上面如没法下载,可用这个:https://www.jb51.net/softs/695840.html

        选择最新版的下载,注意要选择对应的python版本(version)和python位数(bittedness)

        通过在命令行输入python查看python版本和位数:python -V

        如下所示为python3.6的32位,需要下载[pywin32-223.win32-py3.6.exe]

        Python 3.6.3 ... [MSC v.1900 32 bit (Intel)] on win32

        如下所示为python3.6的64位,需要下载[pywin32-223.win-amd64-py3.6.exe]

        Python 3.6.3 ... [MSC v.1900 64 bit (AMD64)] on win32

3.在命令行中直接输入下面的指令即可

        pyinstaller [opts] yourprogram.py

        参数含义

            -F 指定打包后只生成一个exe格式的文件(建议写上这个参数)

            -D –onedir 创建一个目录,包含exe文件,但会依赖很多文件(默认选项)

            -c –console, –nowindowed 使用控制台,无界面(默认)

            -w –windowed, –noconsole 使用窗口,无控制台

            -p 添加搜索路径,让其找到对应的库。

            -i 改变生成程序的icon图标(比如给女朋友写的程序,换个好看的图标,默认的很丑)

实例说明

            比如你有个python程序叫test.py,绝对路径在[D:\project],打包成一个exe格式的文件

            pyinstaller -F D:\project\test.py

            条件同上,如果还希望没有控制台的黑框框,在进程中偷偷运行

            pyinstaller -F -w D:\project\test.py

            条件同上,如果还希望更换程序图标

            pyinstaller -F -w -i D:\project\test.ico D:\project\test.py

            在你的py文件所在的目录下,生成build和dist文件夹,如果是选择了-F参数,那么dist文件夹下就是你要的程序,build文件夹可以删除

注意,pyinstaller只能在windows电脑环境下进行转换。同时建议路径使用英文,不要包含中文,低版本的pyinstaller可能会出错。

4、pyinstaller的正确打包有两种方法:

第一种方法:将需要打包的程序和其所有依赖的包,统一放在pyinstaller的根目录下,直接用-F打包即可成功,少一个包都不行!

第二种方法:安装一个纯净的python环境,然后一步步测试pip,少哪个包就直接安装哪个。

两种方法各有千秋,但是推荐使用第二种方法,因为这种方法相对简便,特别是在不清楚包之间的依赖关系的情况下


5、Pyinstaller Maximum Recursion Depth Exceded

I am trying to create an executable from python 3.6.4 using pyinstaller 3.3.1. The packages I am using are Pandas and openpyxl. When I try to create the bundle I receive this error.

$ RecursionError: maximum recursion depth exceeded in comparison

I have tried increasing my recursion limit and most of the steps described in How to Report Bugs and the error is still the same. I also got the same error when I tried bundling

import openpyxl

print("Hello World")

so I think the problem has to do with openpyxl but pyinstaller is supposed to be compatible with this. Any help would be greatly appreciated!

pandas pyinstaller openpyxl

2 Answers

Install the development version, it should have been resolved in #2919:

pip install https://github.com/pyinstaller/pyinstaller/tarball/develop

I'm using python3.6.4, and I installed pyinstaller using the development version (see above) for PyInstaller-3.4.dev0+bb5d04ef8, and I receive the recursion error. Should I use an earlier version of Python 3? Python 3.5? – Benjamin Levy Mar 24 '18 at 18:33

To me, only python 3.5.2 worked. 3.5.3 or 3.6 didn't work. – TheTufik Mar 25 '18 at 17:45

Remove the build & dist folder, and try run your pyinstaller yourscript.py again. For my case, python 3.6.3 version still be able to run it.

方法二:用PY2EXE

    1)先建立一个mysetup.py

        mysetup.py示例如下:

        # mysetup.py

        from distutils.core import setup

        import py2exe

        setup(console=["helloworld.py"]) #helloworld.py换为你要打包的程序名

2)然后按下面的方法运行mysetup.py:

        python mysetup.py py2exe

3)注意:python 版本为2,版本3不行

你可能感兴趣的:(002 萝卜头学python:python 转成EXE)