


Detailed explanation of the example code for packaging .py files into exe executable files in Python
This article mainly introduces you to the relevant information on packaging .py files into exe executable files in Python. The introduction in the article is very detailed. I believe it has certain reference value for everyone. Friends who need it can read it together. Take a look.
Preface
I recently made a few simple crawler python programs, so I wanted to make a window to see the effect.
First of all, as for windows, I haven’t had much contact with them before, so I will first consider using Qt to make a simple UI. Here we use the previous sinanews crawler script as an example to create a window to obtain the sina headlines of the day.
After generating the py file, run the py file. I just dragged a few components into the window here. The main text browser is 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, which will find the Python installation directory without changing the installation directory)
Local download:
PyQt5-5.2.1 for Py3.3 (After installing Python3.3, install the corresponding PyQt, which will find the Python installation directory, no need to change the installation directory)
Python3.3 does not have pip installed by default. You need to download get-pip.py and run it. It will prompt that the installation is successful.
The next step is to install some necessary components. For ease of installation, first add pip to the environment variables.
Now we can use the pip command to install the component.
First post sina_news.py and observe what components are needed.
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)
I found import requests, import BeautifulSoup, so let’s install these components first
pip install requests pip 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 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_())
If everything goes well, you should be able to see the displayed window when running x.py in python.
The following is the packaging process. The author uses Pyinstaller here. If it is not installed, you need to install it:
pip install pyinstaller
After the installation is completed, cd the cmd path to the directory where x.py is located.
Packaging command:
Pyinstaller -w x.py
At this time, 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:
Possible problems:
The packaged program cannot be opened and it displays: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</module></module></module></module></module></module>
My Pyinstaller is 3.2
The version of requests needs to be lowered, and requests2.10 can be packaged successfully. But 2.11 does not work. Posted here are the requests2.10 used to solve this problem. I don’t know if this problem will be fixed in the future. I was dreaming about this bug yesterday. I solved it when I woke up this morning. I was so excited that I couldn't stand it. I hope the questions you encounter during this process will be helpful to you.The above is the detailed content of Detailed explanation of the example code for packaging .py files into exe executable files in Python. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.
