Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
Hardware and software Environment
- Windows 10 64bit
- PyQt5
- Anaconda with python 3.6.5
- pyinstaller
- apscheduler
- sqlalchemy
preface
Py2exe is the installer that can be used to package a python3 program. Py2exe is the installer that can be used to package a python3 program. Py2exe is the installer that can be used to package a python3 program.
Pyinstaller installation
Command line
pip install pyinstaller
Copy the code
Packaging exe
For example, decompress to the project directory and run the package command
pyinstaller -F --distpath release main.py
Copy the code
Release is the target folder, main.py is the project entry file, and -f means to generate an executable file.
Pyinstaller has a number of packaging parameters, which can be viewed with help if needed
pyinstaller -h
Copy the code
Subprocess packaging problem
Popen package contains subprocess.Popen. Exe file with parameter –noconsole is not running properly. For example, if you want to create a process using subprocess.Popen to perform a command line operation,
mProcess = subprocess.Popen(cmd,stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,universal_newlines=True)
Copy the code
The following commands are used to package pyInstaller
pyinstaller -F --noconsole --clean --distpath release main.py
Copy the code
The packed, generated EXE works, but the viewer process is not working correctly as expected.
The solution is to create the process with the startupinfo parameter as follows
si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
mProcess = subprocess.Popen(cmd,stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,universal_newlines=True,startupinfo=si)
Copy the code
The problem is solved perfectly. Please refer to the information at the end of this article for details
The apScheduler packaging problem
The apscheduler library is used in the project. It is very good and easy to use for scheduled tasks. However, when pyInstaller is packaged, the following error occurs
The solution is to edit the file/usr/local/lib/python3.5 / dist – packages/apscheduler/set py
# These will be removed in APScheduler 4.0.
# release = __import__('pkg_resources').get_distribution('APScheduler').version.split('-')[0]
# version_info = tuple(int(x) if x.isdigit() else x for x in release.split('.'))
# version = __version__ = '.'.join(str(x) for x in version_info[:3])
release = (3.3.1)
version_info = '3.3.1'
version = '3.3.1'
Copy the code
3.3.1 is the version number of the APScheduler I installed, which can be modified according to the actual situation.
Next, create one called hook-ctypes.macholib.py and read as follows
# -*- coding: utf-8 -*-
from PyInstaller.utils.hooks import copy_metadata
datas = copy_metadata('apscheduler')
Copy the code
Finally, execute the package command
pyinstaller -F --clean --additional-hooks-dir hooks --distpath release main.py
Copy the code
Sqlalchemy package problems
While using PyInstaller to package a Python3 project that uses the SQLAlchemy library, I encountered an error in the packaged executable as shown in the figure below
The solution is to import mysql from SQLAlchemy. cn and then use mysql+ Pymysql to open the database as follows
import pymysql
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.dialects import mysql
username = 'xugaoxiang'
passwd = '123456'
server = '192.168.0.100'
port = '3306'
dbname = 'djdb'
cmd_connect = 'mysql+pymysql://{}:{}@{}:{}/{}'.format(username, passwd, server, port, dbname)
engine = create_engine(cmd_connect)
Copy the code
Run the PyInstaller package command
pyinstaller -F --clean --distpath shark main.py
Copy the code
note
To facilitate record keeping, a project has been created on Github at github.com/xugaoxiang/… All the code, documentation, and blog links will be posted on it later.
The resources
- www.pyinstaller.org/
- Github.com/pyinstaller…
- Blog.csdn.net/Xiong_Big/a…