The solution of no such file or directory and cannot load native module running error of python3 pyinstaller after packaging

 

use  Pyinstaller is often used to package python3 programs   No such file or directory   Or cannot load native module error is because the required file is not entered into the final execution file. In this case, the parameter is needed when using pyinstaller  — Add binary and add code in the entry file function to solve the problem

Example 1. No such file or directory error   — Add binary parameter solution

Package the portal Python file:

pyinstaller -F -w test.py

Run the packaged executable file:

[[email protected] src]# ./dist/test
Traceback (most recent call last):
  File "test.py", line 19, in <module>
    from salt.client.ssh import ssh_py_shim
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller/test/pyimod03_importers.py", line 540, in exec_module
  File "salt/client/ssh/__init__.py", line 205, in <module>
  File "salt/utils/files.py", line 396, in fopen
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/_MEIudOUhL/salt/client/ssh/ssh_py_shim.pyc'
[5034] Failed to execute script test

By reporting an error, you can see that the file is missing, and it will be increased when pyinstall  — Add binary parameter, as follows:

pyinstaller -F -w test.py \
--add-binary="/opt/python3/lib/python*/site-packages/salt/client/ssh/ssh_py_shim.py:salt/client/ssh"

Parameter explanation:

–The syntax of add binary is: – add binary  & lt; SRC; DEST or SRC:DEST>
SRC is the packaged executable file. The missing file is in the local location,

Dest is the location of the directory that needs to be dynamically copied to when running the executable file. Dest is the relative directory,

The absolute path of the packaged execution file is from  / tmp/_ MEIudOUhL/   It started here_ Meiudouhl is generated automatically, different every time

Dest just needs to write the relative directory. For example, here is   salt/client/ssh

The final absolute path is  / tmp/_ MEIudOUhL/salt/client/ssh
The separator of SRC and dest is colon in Linux and semicolon in windows;

Adding this parameter means that the   ssh_ py_ The shim.py file is packaged into an executable file and dynamically released to a relative directory at runtime   Salt/client/SSH, so as to solve the problem that the error prompt can not find the file

Note:

The error in the error prompt cannot be found   ssh_ py_ Shim. PyC, but the   ssh_ py_ shim.py

This is because salt/client/SSH is the penultimate layer in the whole function call stack/__ init__. Py has the following code:

if not is_windows():
    shim_file = os.path.join(os.path.dirname(__file__), "ssh_py_shim.py")
    if not os.path.exists(shim_file):
        # On esky builds we only have the .pyc file
        shim_file += "c"
    with salt.utils.files.fopen(shim_file) as ssh_py_shim:
        SSH_PY_SHIM = ssh_py_shim.read()

You can see that   ssh_ py_ The shim. Py file is also available, so just find it   ssh_ py_ Shim.py or   ssh_ py_ Shim.pyc in  ” Salt/utils/files. Py “, line 396   No file not found error occurs in fopen function

Example of official website:

https://pyinstaller.readthedocs.io/en/stable/usage.html#shortening -the-command

There are a number of them  — Examples of using Add binary and other related parameters

Example 2. Cannot load native module error forced introduction solution

The error of pyinstall after packing and running is as follows:

  File "test/test.py", line 41, in init_test
  File "salt/transport/client.py", line 27, in factory
  File "salt/utils/asynchronous.py", line 70, in __init__
  File "salt/transport/client.py", line 131, in factory
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller/test/pyimod03_importers.py", line 540, in exec_module
  File "salt/transport/zeromq.py", line 23, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller/test/pyimod03_importers.py", line 540, in exec_module
  File "salt/crypt.py", line 65, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller/test/pyimod03_importers.py", line 540, in exec_module
  File "Cryptodome/Cipher/__init__.py", line 27, in <module>
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "PyInstaller/test/pyimod03_importers.py", line 540, in exec_module
  File "Cryptodome/Cipher/_mode_ecb.py", line 35, in <module>
  File "Cryptodome/Util/_raw_api.py", line 297, in load_pycryptodome_raw_lib
OSError: Cannot load native module 'Cryptodome.Cipher._raw_ecb': Trying '_raw_ecb.cpython-39-x86_64-linux-gnu.so': cannot load library '/tmp/_MEIPh0VXD/Cryptodome/Util/../Cipher/_raw_ecb.cpython-39-x86_64-linux-gnu.so': /tmp/_MEIPh0VXD/Cryptodome/Util/../Cipher/_raw_ecb.cpython-39-x86_64-linux-gnu.so: cannot open shared object file: No such file or directory.  Additionally, ctypes.util.find_library() did not manage to locate a library called '/tmp/_MEIPh0VXD/Cryptodome/Util/../Cipher/_raw_ecb.cpython-39-x86_64-linux-gnu.so', Trying '_raw_ecb.abi3.so': cannot load library '/tmp/_MEIPh0VXD/Cryptodome/Util/../Cipher/_raw_ecb.abi3.so': /tmp/_MEIPh0VXD/Cryptodome/Util/../Cipher/_raw_ecb.abi3.so: cannot open shared object file: No such file or directory.  Additionally, ctypes.util.find_library() did not manage to locate a library called '/tmp/_MEIPh0VXD/Cryptodome/Util/../Cipher/_raw_ecb.abi3.so', Trying '_raw_ecb.so': cannot load library '/tmp/_MEIPh0VXD/Cryptodome/Util/../Cipher/_raw_ecb.so': /tmp/_MEIPh0VXD/Cryptodome/Util/../Cipher/_raw_ecb.so: cannot open shared object file: No such file or directory.  Additionally, ctypes.util.find_library() did not manage to locate a library called '/tmp/_MEIPh0VXD/Cryptodome/Util/../Cipher/_raw_ecb.so'

This error also started missing files, missing  _raw_ecb.cpython-39-x86_ 64 Linux gnu.so or  _raw_ecb.so   First, add binary

pyinstaller -F -w test.py \
--add-binary="/opt/python3/lib/python*/site-packages/Crypto/Cipher/_raw_ecb.cpython-39-x86_64-linux-gnu.so:Cryptodome/Util/../Cipher"

When running in package or reporting this error, repeatedly check that the source file destination address is correct,

At this point   Copy references in later projects _raw_ECB error code to the entry function

Let pyinstaller know that I can use it  _raw_ecb.cpython-39-x86_64-linux-gnu.so to force the package of this so file

The code is as follows:

if __name__ == "__main__":
    try:
        ip = "127.0.0.1"
        port = 1234

        config = {
            'id': 'root',
            'log_level': 'debug',
            'master_ip': ip ,
            'master_port': port,
            'auth_timeout': 5,
            'auth_tries': 1,
            'master_uri': f'tcp://{ip}:{port}'
        }

        salt.transport.client.ReqChannel.factory(config, crypt='clear')
    except Exception as e:
        pass

Package with pyinstall

pyinstaller -F -w test.py \
--add-binary="/opt/python3/lib/python*/site-packages/Crypto/Cipher/_raw_ecb.cpython-39-x86_64-linux-gnu.so:Cryptodome/Util/../Cipher"

Not at this time  — Add binary should be OK, not tested

Summary

In case of errors, the general idea is to use the function call stack to push backward,

If the file cannot be found, it can be used
1. — add binary parameter

2. Compulsory introduction

To solve the problem


Read More: