使用 py2exe 为 python 代码构建可执行文件 [英] Building an executable file for python code using py2exe

查看:35
本文介绍了使用 py2exe 为 python 代码构建可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Python2.7"安装了py2exe",制作了一个Hello World"程序,使用py2exe构建了它的exe,并且运行良好.

I installed "py2exe" for "Python2.7" , made a "Hello World" program, used py2exe to build its exe and it worked perfectly.

现在,当我尝试构建实际代码的 exe 时,会创建文件夹,也可以毫无问题地创建 exe,但是当我启动 exe 时,控制台出现不到一秒钟并关闭.

Now when I tried building the exe of my actual code, the folders get created and exe is also created without any problem but when I launch the exe the console appears for less than a second and closes.

在这种情况下和测试程序的唯一区别是在这种情况下我有多个代码文件,其中一些我还导入了一些文件/库以下是分布在不同文件中的导入命令:

Only difference in this case and test program is that I have multiple code files in this case and in some of them I am importing a few files/libraries as well Following are import commands spread across different files:

from bs4 import BeautifulSoup
import time
import requests
from RdWrtCls import *
import os
import openpyxl 

最初我的源代码是:

from distutils.core import setup
import py2exe
from glob import glob
setup (console =['Crawler.py'])

当这不起作用时,我做了一些研究,发现有时会出现缺少 dll 的问题,因此我将代码修改为:

when this didn't work I did some research and found that sometimes there is a missing dll problem, so I modified my code to this:

from distutils.core import setup
import py2exe
from glob import glob
data_files = [("Microsoft.VC100.CRT", glob(r'C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\redist\\x86\\Microsoft.VC100.CRT\\*.*'))]

setup(data_files =data_files,console =['Crawler.py'])

我被这个问题困住了,所以任何和各种帮助都将不胜感激.

I am stuck in this problem so any and all sorts of help would be highly appreciated.

其他信息:我在用:Windows 7的,Python 2.7-32 位

Other Information: I am using: Windows7, Python 2.7-32 bit

更新:我在一个关于堆栈溢出的问题中找到了一个建议,以解决类似的问题,建议通过命令提示符运行 EXE.我通过命令提示符运行程序并显示错误

Update : I found a suggestion in a question on stack overflow for a similar problem suggesting to run the EXE through command prompt. I ran the program through command prompt and showed me the error

Import error : No module named bs4

推荐答案

我能够成功构建一个可运行的 exe.以下是我采取的步骤,希望它们对将来面临这些问题的其他人有所帮助.

I was able to successfully build a working exe. Following are the steps I took I hope they prove helpful for others facing these issues in future.

1) 使用命令提示符启动程序,以便我可以查看错误.

1) Launch the program Using Command Prompt so I can view errors.

2) 将我所有的代码(函数、类)放在主文件中.

2) Put all my code(functions,classes) in main file.

3) 重新安装最新版本的 pip

3) reinstalled latest version of pip

4) 使用 pip 卸载并重新安装 BeautifulSoup4

4) Uninstalled and reinstalled BeautifulSoup4 using pip

pip uninstall BeautifulSoup4
pip install  BeautifulSoup4

5) 现在jdcal"开始出现问题(安装openpyxl时安装)

5) Now "jdcal" started giving problems (it is installed when installing openpyxl)

6) 使用 pip 卸载 openpyxl

6) Uninstalled openpyxl using pip

7) 使用 pip 卸载 jdcal

7) Uninstalled jdcal using pip

8) 使用 pip 重新安装 openpyxl(它会自动安装 jdcal)

8) reinstalled openpyxl using pip (it automatically installed jdcal )

9) 现在 exe 完美运行了.

9) Now exe runs perfectly.

这篇关于使用 py2exe 为 python 代码构建可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆