current position:Home>Pyinstaller package Python project

Pyinstaller package Python project

2022-01-29 22:56:03 Notes of the lost schoolboy

Little knowledge , Great challenge ! This article is participating in “ A programmer must have a little knowledge ” Creative activities .

Hardware and software environment

  • Windows 10 64bit
  • PyQt5
  • Anaconda with python 3.6.5
  • pyinstaller
  • apscheduler
  • sqlalchemy

Preface

above PyQt5 Series of tutorials ( Four ) use py2exe Package the program How to use py2exe Here it is python3 Project packaging , This article introduces another packaging tool pyinstaller, Personal feeling is better than py2exe It's simpler 、 convenient , But also cross platform .

pyinstaller install

Execute... On the command line

pip install pyinstaller
 Copy code 

pack exe

Take the previous example , Unzip into the project directory , The command line executes the packaging command

pyinstaller -F --distpath release main.py
 Copy code 

among release It's the destination folder ,main.py It is the project entry file ,-F Means to generate an executable file .

pyinstaller_01

pyinstaller_02

pyinstaller_03

pyinstaller There are many packaging parameters , If necessary, through help Check it out.

pyinstaller -h
 Copy code 

subprocess Packing problem

Include in package subprocess.Popen They found , Add parameters --noconsole When the exe When the file is running , The process is not running correctly . For example, we need to use subprocess.Popen Create a process to perform a command line operation ,

mProcess = subprocess.Popen(cmd,stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,universal_newlines=True)
 Copy code 

pyinstaller The packaging operation command is as follows

pyinstaller -F --noconsole --clean --distpath release main.py
 Copy code 

It is generated after packing exe, Can run , However, the viewing process did not work correctly as expected .

The solution is to create the process , add startupinfo Parameters , 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 code 

Problem solving , For details, please refer to the information at the end of the text

apscheduler Packing problem

The project uses apscheduler This library , It's very good for timed tasks , It's also easy to use , But in the pyinstaller When packing , The following error occurred

pyinstaller_apscheduler_error

The solution is , Edit the file /usr/local/lib/python3.5/dist-packages/apscheduler/__init__.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 code 

3.3.1 I installed it apscheduler Version number of , Modify according to the actual situation .

Next, create a file named hook-ctypes.macholib.py, The contents are as follows

# -*- coding: utf-8 -*-

from PyInstaller.utils.hooks import copy_metadata

datas = copy_metadata('apscheduler')
 Copy code 

Finally, execute the packing command

pyinstaller -F --clean --additional-hooks-dir hooks --distpath release main.py
 Copy code 

sqlalchemy Packing problem

In use pyinstaller Pack one and use sqlalchemy Library python3 In Engineering , Encountered the problem that the packaged executable file reported an error , As shown in the figure below

pyinstaller_sqlalchemy_error

The solution is , take sqlalchemy.dialects Medium mysql introduce , Then use mysql+pymysql To open the database , The code is 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 code 

perform pyinstaller Packing command

pyinstaller -F --clean --distpath shark main.py
 Copy code 

remarks

In order to keep records , stay Github Created a project , The address is : github.com/xugaoxiang/…, All the code will be later 、 Documents and blog links are on it .

Reference material

copyright notice
author[Notes of the lost schoolboy],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201292255596547.html

Random recommended