Record the problems and solutions encountered during the packaging process of pyinstaller and using Cpython

Since the project needs to package the python script into a dos window and run it directly, here I chose to use pyinstaller for packaging.

Question 1: Add input statement under if __name__ == '__main__': and then package and report error

Generally for packaging I will use:

pyinstaller -D -w ***.py

It should be noted here that the -w option cannot be added after using the input statement , which will make the input statement unable to be enabled.

Question 2: In order to encrypt the source code, use Cpython to convert it into a pyd file, and then package the pyd file.

The .py file used here is as follows:

from distutils.core import setup
from Cython.Build import cythonize

setup(ext_modules=cythonize("find_ball.py"))

The conversion statement here is as follows:

python setup.py build_ext --inplace

There is a big hole here! ! !

As a custom package, the name must not be "test.py". If so, the custom function will not be imported after packaging. My personal guess is that there should be a test-related library, which should be selected first when packaging. That library is not a custom package, thus making it impossible to use the functions in the custom library "test.py". Although it can be compiled in VSCDOE, it does not work after packaging!

Things to note when converting to pyd files for packaging

1. Before converting the py file into a pyd file, import the packages used by each .py file (not all, but try to add them such as open3d and cv2) in the entry .py file to avoid using them. Library has not been added.

2. After converting to a .pyd file, import the following into the entry .py file (for example, after the original find_ball.py was converted and imported, even if you only use a few functions in it, it is best to add statements similar to the following to ensure that there is no will go wrong):

import find_ball

Because packaging will only add libraries\packages imported by the entry file, keep this in mind.

3. When converting a py file to a pyd file, be careful not to make the function name the same as the file name. Since the library name needs to be imported again in step 2 (that is, the file name without the suffix), the following error will occur:

TypeError: 'module' object is not callable

Question 3: Package and load additional resources

Because every time you package the software, there will be some other resources such as icons, model files, etc. that need to be added to the packaged folder, and the statement is as follows:

pyinstaller -D -w entrance.py --add-data="./template_cnt_.npy;."  --add-data="./data_bk;./data_bk" --add-data="./result;./result" --add-data="./data_pcd;./data_pcd" --add-data="./logo.ico;."

The above code adds two files and two folders to the corresponding folder of the software. (Be careful to use double quotes under the English path)

Guess you like

Origin blog.csdn.net/zhaodongdz/article/details/124501944