Python. py file packaged as exe executable file

Source: Internet
Author: User

A few days ago did 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 paste my configuration (click on the 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)

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.

ImportRequests fromBs4ImportBeautifulsoupres= Requests.get ('http://news.sina.com.cn/china/') res.encoding='Utf-8'Soup= BeautifulSoup (Res.text,'Html.parser') forNewsinchSoup.select ('. News-item'):    ifLen (News.select ('H2')) >0:H2= News.select ('H2') [0].text a= News.select ('a') [0]['href'] Time= News.select ('. Time') [0].textPrint(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!ImportSYSImportRequests fromPyQt5ImportQtcore, Qtgui, Qtwidgets fromBs4ImportBeautifulSoupclassui_x (object):defgetnews (): Res= Requests.get ('http://news.sina.com.cn/china/') res.encoding='Utf-8'Soup= BeautifulSoup (Res.text,'Html.parser') Title= []         forNewsinchSoup.select ('. News-item'):            ifLen (News.select ('H2')) >0:H2= News.select ('H2') [0].text title.append (H2) a= News.select ('a') [0]['href'] Time= News.select ('. Time') [0].textreturn '\ n'. Join (title)defsetupui (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)defRetranslateui (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.

In this blog post, I mainly introduce the package process of py file.

problems you may encounter :

Open packaged program fails to run
Show:
Importerror:no module named ' Queue '

During handling of the above exception, another exception occurred:

Traceback (most recent):
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 <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.

                      

Python. py file packaged as exe executable file

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.