源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Python中.py文件打包成exe可执行文件详解

  • 时间:2020-04-27 15:32 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Python中.py文件打包成exe可执行文件详解
[b]前言[/b] 最近做了几个简单的爬虫python程序,于是就想做个窗口看看效果。 首先是,窗口的话,以前没怎么接触过,就先考虑用Qt制作简单的ui。这里用前面sinanews的爬虫脚本为例,制作一个获取当天sina头条新闻的窗口。 生成py文件后,运行该py文件,这里窗口我只是随便拖了几个组件进去,主要的text browser用于显示获取到的sinanews。 [b]首先贴一下我的配置[/b] [b]官方下载:[/b]        [url=https://www.python.org/ftp/python/3.3.3/python-3.3.3.amd64.msi]Python 3.3.3[/url]   [url=https://jaist.dl.sourceforge.net/project/pyqt/PyQt5/PyQt-5.2.1/PyQt5-5.2.1-gpl-Py3.3-Qt5.2.1-x64.exe]PyQt5-5.2.1 for Py3.3[/url](当安装完Python3.3后,安装对应PyQt,其会找到Python安装目录,不用更改安装目录) [b]本地下载:[/b]         [url=http://www.1sucai.cn/softs/543087.html]Python 3.3.3[/url]         [url=http://www.1sucai.cn/softs/543096.html]PyQt5-5.2.1 for Py3.3[/url](当安装完Python3.3后,安装对应PyQt,其会找到Python安装目录,不用更改安装目录) Python3.3默认是没有安装pip的,需要[url=http://xiazai.jb51.net/201703/yuanma/get-pip(jb51.net).rar]下载get-pip.py[/url]运行之后,提示安装成功。 接下来就要安装一些必要的组件了。为了安装方便,先把pip添加进环境变量。 [b]下面我们就可以用pip命令安装组件了。[/b] 先把sina_news.py贴出来,观察需要哪些组件。
import requests
from bs4 import BeautifulSoup
res = requests.get('http://news.sina.com.cn/china/')
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text,'html.parser')

for news in soup.select('.news-item'):
 if len(news.select('h2')) > 0:
 h2 = news.select('h2')[0].text
 a = news.select('a')[0]['href']
 time = news.select('.time')[0].text
 print(time,h2,a)
发现import requests,import BeautifulSoup 所以先来安装这些组件
pip install requests

pip install BeautifulSoup4
当我们把这段代码贴进窗口代码后: x.py
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'x.ui'
#
# Created by: PyQt5 UI code generator 5.8.1
#
# WARNING! All changes made in this file will be lost!
import sys
import requests
from PyQt5 import QtCore, QtGui, QtWidgets
from bs4 import BeautifulSoup

class Ui_x(object):
 def getNews():
 res = requests.get('http://news.sina.com.cn/china/')
 res.encoding = 'utf-8'
 soup = BeautifulSoup(res.text,'html.parser')
 title = []
 for news in soup.select('.news-item'):
 if len(news.select('h2')) > 0:
 h2 = news.select('h2')[0].text
 title.append(h2)
 a = news.select('a')[0]['href']
 time = news.select('.time')[0].text
 return '\n'.join(title)

 
 def setupUi(self, x):
 x.setObjectName("x")
 x.resize(841, 749)
 self.timeEdit = QtWidgets.QTimeEdit(x)
 self.timeEdit.setGeometry(QtCore.QRect(310, 10, 141, 31))
 self.timeEdit.setObjectName("timeEdit")
 self.dateEdit = QtWidgets.QDateEdit(x)
 self.dateEdit.setGeometry(QtCore.QRect(100, 10, 191, 31))
 self.dateEdit.setObjectName("dateEdit")
 self.textBrowser = QtWidgets.QTextBrowser(x)
 self.textBrowser.setGeometry(QtCore.QRect(60, 80, 701, 641))
 self.textBrowser.setObjectName("textBrowser")
 self.retranslateUi(x)
 QtCore.QMetaObject.connectSlotsByName(x)

 def retranslateUi(self, x):
 _translate = QtCore.QCoreApplication.translate
 x.setWindowTitle(_translate("x", "x"))

if __name__ == '__main__': 
 app = QtWidgets.QApplication(sys.argv)
 Form = QtWidgets.QWidget()
 ui = Ui_x()
 ui.setupUi(Form)
 Form.show()
 ui.textBrowser.setText(Ui_x.getNews())
 sys.exit(app.exec_())
如果前面顺利的话,现在用python运行x.py应该能看到显示的窗口。 下面就是打包的过程了,这里笔者用的Pyinstaller,没有安装的话,要安装一下:
pip install pyinstaller
安装完成后,cmd路径cd到x.py所在目录。 [b]打包命令:[/b]
Pyinstaller -w x.py
此时,在x.py便生成dist文件夹,打包的x.exe就在此文件夹下。双击x.exe显示效果: [img]http://files.jb51.net/file_images/article/201703/2017322153651008.jpg?2017222153758[/img] 当然还有许多改进的地方,比如在上面选择日期,获得指定日期的头条新闻。 笔者在这片博文主要介绍py文件的打包过程。 [b]可能遇到的问题:[/b] 打开打包后的程序无法运行显示:
ImportError: No module named 'queue'

During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
 File "test.py", line 2, in <module>
 File "c:\users\hasee\appdata\local\programs\python\python35-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 389, in load_module
 exec(bytecode, module.__dict__)
 File "site-packages\requests\__init__.py", line 63, in <module>
 File "c:\users\hasee\appdata\local\programs\python\python35-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 389, in load_module
 exec(bytecode, module.__dict__)
 File "site-packages\requests\utils.py", line 24, in <module>
 File "c:\users\hasee\appdata\local\programs\python\python35-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 389, in load_module
 exec(bytecode, module.__dict__)
 File "site-packages\requests\_internal_utils.py", line 11, in <module>
 File "c:\users\hasee\appdata\local\programs\python\python35-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 389, in load_module
 exec(bytecode, module.__dict__)
 File "site-packages\requests\compat.py", line 11, in <module>
 File "c:\users\hasee\appdata\local\programs\python\python35-32\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 389, in load_module
 exec(bytecode, module.__dict__)
 File "site-packages\requests\packages\__init__.py", line 29, in <module>
ImportError: No module named 'urllib3'
Failed to execute script test
当然这个错误代码,当时我没有保留,这是版本不匹配造成的: [b]我的Pyinstaller为3.2[/b] 需要降低requests的版本,requests2.10可以成功打包,而2.11就不行。这里贴上解决此问题用到的[url=http://xiazai.jb51.net/201703/yuanma/requests-2.10.0(jb51.net).rar]requests2.10[/url]不知道以后会不会修复这个问题。这个bug昨天做梦我还梦到呢。今天早上起来就解决了,兴奋的受不了。希望在此过程中遇到的问题对你会有所帮助。 [b]总结[/b] 以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对编程素材网的支持。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部