Environment:

  • On a server that can run the algorithm properly (Otherwise, the package won’t work on a normal computer (which may not have CUDA or NVIDIA drivers, etc.))
  • Run an Algo docker container and mount the algorithm code (must use Python source code, not use cython’s dynamic library) (see command: Docker run – it – the name one_file – v/home/imsight/one_file ct_chest_lung_nodule / : / app registry.xxx.com/softwaredev… / bin/bash)

Tools:

Install pyinstaller (PIP install pyinstaller -i mirrors.aliyun.com/pypi/simple)

Packaging:

Pyinstaller -f main.py

–add-binary ‘/usr/lib/x86_64-linux-gnu/libxcb.so.1:.’ — binary ‘/usr/lib/x86_64-linux-gnu/libxcb.so.

If there is a problem of missing libraries during runtime, based on experience, the recommended solution is as follows:

If the missing library definition is in the.py file, it is recommended to hook it instead of Hiddenimports.

For example, if the elasticapm library is missing at runtime, the hidden Library will need to be added one by one (run each time to find one missing) :

“elasticapm.transport”, “elasticapm.transport.http”, “elasticapm.processors”, “elasticapm.instrumentation.packages”, “Elasticapm. Instrumentation. Packages. Django” and so on

It’s a waste of time.

Create a folder called hooks for example and put hook-Elasticapm.py in it with only 2 sentences

from PyInstaller.utils.hooks import collect_submoduleshiddenimports = collect_submodules('elasticapm')
Copy the code

Add — Addition-resting-dir =./hooks to fix missing libraries in the Elasticapm family

When packing flask-limiter, the following error occurs:

pkg_resources.DistributionNotFound: The ‘werkzeug’ distribution was not found and is required by the application

Hook add the following two sentences can be solved:

From PyInstaller.utils. Hooks import Collect_subModuleshiddenimports = Collect_submodules ('flask_limiter')# Solve pkg_resources. DistributionNotFound: The 'werkzeug' distribution was not found and is required by the applicationfrom PyInstaller.utils.hooks import copy_metadatadatas = copy_metadata('werkzeug')Copy the code

Note: Flask_limiter: flask_limiter: flask_limiter: flask_limiter: flask_limiter: flask_limiter: flask_limiter: flask_limiter: flask_limiter: flask_limiter Otherwise it is not successful (verified by multiple tests)

If the library definition is in a Cython-generated dynamic library, use –hidden-import

For example, the model’s encryption password is too important to appear in clear text in a project that multiple people can access.

One option is to decrypt the model in a repository that only a few people have access to.

After cython, the binary dynamic library is output, which is called directly by the algorithm code of the project.

Since pyInstaller cannot directly search for import information in this Cython-like file, hiddle-import must be used to add libraries such as Crypto.Cipher.

When pyInstaller is packaged, the Crypto.Cipher library is added.

Products:

By default, executables such as main(with the suffix removed from main.py) are generated in the dist folder

Testing:

Be sure to test. If a missing library is packaged and no apparent failure is visible, it must be tested by running

Run:

Dist /main

Note:

Currently, ubuntu18.04 packaged executable files cannot run on ubuntu16.04 (because 18.04 requires a version of GLIBC >=2.25)

Therefore, if based on python3.6 or higher executables, it is recommended to package it in ubuntu18.04 and run it on ubuntu18.04’s docker.

Here are the commands to boot from the public Ubuntu :18.04 image:

docker run -it --rm --name sentinel_docker --privileged --device /dev/bus/usb:/dev/bus/usb -v /home/imsight/one_file/ct_chest_lung_nodule:/app -v /data:/data -p 6314:6314 Ubuntu :18.04 bash -c export LANG= c.utf-8; cd /app; ./dist/main"Copy the code