python-55 - packaged exe execution

Preface

Do you have this trouble?

  • Others may need to install various dependency packages when using your project?
  • If someone else is using your project, maybe they don’t know how to install the environment at all?
  • Shared service machine, occasionally someone else changes the version of a dependency package, causing it to fail to run?

I would find that it takes a lot of time to do this every time, so I thought that I had packaged it before and executed it directly as an exe file.

1. pyinstaller

PyInstaller is a tool for converting Python programs into standalone executable files. Using PyInstaller, you can package a Python program into a single executable file that can be run on computers without a Python interpreter.
Here are the basic steps for packaging a Python program into an executable file using PyInstaller:

1. Make sure that PyInstaller has been installed. If it is not installed, you can install it using the following command:

pip install pyinstaller

2. Open a terminal or command prompt and navigate to the directory containing your Python program.

3. Run the following command to package the Python program into an executable file:

pyinstaller your_script.py

where your_script.py is the file name of your Python program.

4. PyInstaller will create a folder named dist in the current directory, which contains the generated executable file. On Windows, the executable file will have an .exe extension, while on Linux and Mac there will be no extension.
screenshot
5. Command view

pyinstaller --help
  • -F: Pack all files into a single executable file.
  • -D: Pack all files into a directory, including the executable file and all dependent files.
  • -c: Combine the program with the command prompt so that it can be run from the command prompt.
  • -d: Pack debugging information into the executable file.
  • –onefile: Pack all files into a single executable file.
  • -o: Specifies the location of the output file.
  • -w: Pack as a window file.
  • -i: Specified ico.

The ones I use most often are:

pyinstaller -F -i xxx.ico xxx.py

2. Practice packaging exe

pyinstaller -F -i ./desc/build.ico ppl.py

1、遇坑1:Plugin already registered

Dependencies are generally entered, but there are also pitfalls, such as pytest

import pytest
from allure_pytest import plugin as allure_plugin
# pytest.main(argv)
pytest.main(argv, plugins=[allure_plugin])  # todo use package

When I packaged, I needed to add the allure_plugin dependency, but when I ran it in the IDE, I got an error again, so I had to change it to:

import pytest
from allure_pytest import plugin as allure_plugin
pytest.main(argv)
# pytest.main(argv, plugins=[allure_plugin])  # todo use package

pytest.main(argv) was solved, and the error was reported as follows:
ValueError: Plugin already registered: allure_pytest=<module 'allure_pytest.plugin'
Error screenshot


2. Encounter 2: OSError handle is invalid

1. Executing the exe after packaging found the following errors:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
OSError: [WinError 6] 句柄无效。

Later, I saw that the loguru log module reported an error:
I also saw this issue but did not actually solve it.

2. So add a parameter during runtime to indicate whether loguru is used for logging.
The default is to use loguru. If you add the parameter --colour=0, it means loguru is not used.
Part of the code changes are as follows: the PplLog printing class uses the same method of loguru, so the code changes are minimal.
Error screenshot
3. Package test and
found that it is normal. This should be the reason for the issues , but it seems that I have not seen a better solution, so I can only do this for now.
Run screenshot


3. Summary

  • After packaging, it is so convenient that various machines can run it with one click, but some of your changes may have to be repackaged, or you may encounter various packaging problems and have to spend time solving them.

  • If it is under a Linux, Mac, etc. machine, you may have to have a host machine before you can package it. Currently, I have not seen any way to package other operating systems under Windows.

  • There is no problem when running under the code interpreter, but there may not be any problem after packaging. For example, if there are missing dependencies, it is more difficult to troubleshoot.

My code address:
github
gitee

Insert image description here

Guess you like

Origin blog.csdn.net/qq_42675140/article/details/132776385