The. py file in Python is packaged into an exe executable file,

Source: Internet
Author: User

The. py file in Python is packaged into an exe executable file,

Preface

Recently I made several simple python crawler programs, so I wanted to make a window to see the results.

First of all, if you have never touched the window before, you should first consider using Qt to create a simple ui. Here, we use the crawler script of sinanews as an example to create a window for getting the sina headlines of the day.

After the py file is generated, run the py file. I drag several components in the window here. The main text browser is used to display the obtained sinanews.

First, paste my configuration

Official download:

Python 3.3.3

PyQt5-5.2.1 for Py3.3 (after installing Python3.3, install the corresponding PyQt, it will find the Python installation directory, do not change the installation directory)

Local download:

Python 3.3.3

PyQt5-5.2.1 for Py3.3 (after installing Python3.3, install the corresponding PyQt, it will find the Python installation directory, do not change the installation directory)

Python3.3 by default, pip is not installed, You need to download the get-pip.py after running, prompt that the installation is successful.

Next we need to install some necessary components. To facilitate installation, add pip to the environment variable first.

Now we can use the pip command to install the component.

First paste sina_news.py to observe which components are needed.

import requestsfrom bs4 import BeautifulSoupres = 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)

We found that import requests and import BeautifulSoup. Therefore, we need to install these components first.

pip install requestspip install BeautifulSoup4

After we paste this code into the window code:

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 sysimport requestsfrom PyQt5 import QtCore, QtGui, QtWidgetsfrom bs4 import BeautifulSoupclass 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_())

If the preceding steps are successful, run x. py in python to view the displayed window.

The following is the packaging process. If Pyinstaller is not installed, install it:

pip install pyinstaller

After the installation is complete, run the cmd path cd to the directory where x. py is located.

Packaging command:

Pyinstaller -w x.py

Then, in the x.pygenerated distfolder, the packaged x.exe file is in this folder. Double x.exe:

Of course there are still many improvements, such as selecting a date above to get the headlines of a specified date.

In this blog post, I will introduce the packaging process of py files.

Possible problems:

The packaged program cannot be run:

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

Of course, this error code was not retained at the time. This is caused by version mismatch:

My Pyinstaller is 3.2

To reduce the version of requests, requests2.10 can be packaged successfully, but 2.11 won't. The requests2.10 used to solve this problem is pasted here. I don't know if this problem will be fixed in the future. I dreamed about this bug yesterday. It was solved when I got up this morning. I hope the problems encountered in this process will be helpful to you.

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.