python 程序打包成exe py2exe

来源:互联网 发布:传智播客 unity3d 编辑:程序博客网 时间:2024/05/21 11:04

python 程序运行需要特定的python环境,如何直接打包成windows直接运行的程序,不依赖特定的python环境呢? 使用py2exe

1.py2exe 的安装

直接使用pip install py2exe,python2.7版本会提示需要高版本的python的错误。解决方法是,下载https://sourceforge.net/projects/py2exe/files/py2exe/0.6.9/ exe程序直接双击运行即可。

2.如何打包自己的python项目

首先,需要在你的Python项目下建立一个setup.py文件,写入打包脚本。脚本的写入方法:点击打开链接

主要注意的是,setup(console=["helloworld.py"])还是setup(windows=["helloworld.py"]),区别在于,console是控制台程序,如果你的程序有图形化界面,使用windows。如果你的项目有多个py文件,只写入口py文件名即可。如果你的程序依赖除py文件之外其他类型的文件,如配置文件config.ini,txt文件,图标等,需要在setup中指定,打包程序会复制到打包的目录下,具体方法链接中都有。

3.我的一个例子

项目文件。

setup.py

from distutils.core import setupimport py2exesetup(windows=["main.py"],      data_files=[(".",                   ["url.txt", "log.txt", "result.txt"]),                  (".",                   ["logo.ico"]),                  (".", ["config.ini"])],)
4.打包与打包之后的运行

打包方法就是命令行下运行setup.py文件,命令行下进入到项目目录运行

python setup.py py2exe

运行没有错误之后,会生成一个dist文件夹,这个文件夹就是打包的结果,独立于

python运行环境,需要整体发布,不能只单独发布exe文件。

双击exe文件,如果没有提示错误,程序运行正常就完事了。如果提示错误,

查看.exe.log查看日志,一般情况下是因为依赖的其他文件不存在导致的。我的

就因为两个js文件不存在出错了。

Traceback (most recent call last):  File "main.py", line 6, in <module>  File "browser.pyc", line 1, in <module>  File "selenium\webdriver\__init__.pyc", line 18, in <module>  File "selenium\webdriver\firefox\webdriver.pyc", line 34, in <module>  File "selenium\webdriver\remote\webdriver.pyc", line 25, in <module>  File "selenium\webdriver\remote\webelement.pyc", line 40, in <module>  File "pkgutil.pyc", line 591, in get_dataIOError: [Errno 2] No such file or directory: 'selenium\\webdriver\\remote\\getAttribute.js'
这是提示的错误,方法就是找到你环境中的该文件,复制到dist目录下的

library.zip相应的目录即可。


原创粉丝点击