Detailed python. py files packaged as exe executable file instance code

Source: Internet
Author: User
This article mainly introduces you to the Python. py file package into EXE executable file of the relevant information, the article introduced in very detailed, I believe that we have a certain reference value, the need for friends below to see it together.

Objective

Recently made a few simple reptile Python program, so I want to make a window to see the effect.

First of all, the window, if not how to contact before, first consider using QT to make a simple UI. Here is an example of the previous Sinanews crawler script, making a window to get the Sina headlines for the day.

After generating the py file, run the py file, where the window I just dragged a few components in, the main text browser used to display the obtained sinanews.

First, post my configuration.

Official Download:

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)

This underground download:

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 default is no PIP installed, need to download get-pip.py run, prompt installation success.

The next step is to install some of the necessary components. For ease of installation, add the PIP to the environment variable first.

Below we can install the components with the PIP command.

First put the sina_news.py out and see what 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

Discover import Requests,import BeautifulSoup So install these components first

Pip Install Requestspip Install BEAUTIFULSOUP4

When we put this piece of 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 would be Lost!import sysimport requestsfrom PyQt5 import Qtcore, Qtgui, Qtwidgetsfrom BS4 Imp ORT 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, ten, 141, X)) 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 it goes well, now running x.py in Python should be able to see the displayed window.

The following is the packaging process, where the author of the Pyinstaller, not installed, to install:

Pip Install Pyinstaller

After the installation is complete, CMD path CD to the directory where x.py is located.

Package Command:

Pyinstaller-w x.py

At this point, the Dist folder is generated in x.py, and the packaged X.exe is under this folder. Double-click X.exe to display the effect:

There are, of course, many improvements, such as choosing a date above to get headlines on a given date.

Problems you may encounter:

Opening a packaged program cannot run display:

Importerror:no module named ' Queue ' During handling of the above exception, another exception Occurred:traceback (most RE Cent: File "test.py", line 2, <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 +, 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, 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 one, in &L T;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 one, 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, in <module>importerror: No module named ' urllib3 ' Failed to execute script test

Of course this error code, I did not keep, this is the version mismatch caused by:

My Pyinstaller is 3.2.

The requests version needs to be lowered, requests2.10 can be packaged successfully, and 2.11 will not work. The requests2.10 used to solve this problem is not known to fix the problem later. I dreamed about this bug yesterday. I woke up this morning to solve the excitement of the unbearable. I hope that the problems encountered in this process will be of some help to you.

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.