How to Solve PyInstaller Package Error: ModuleNotFoundError: No module named ‘xxxx‘

In the venv environment, there is no exception in the packaging process when the command line pyinstaler is used to execute the packaging command. However, after the packaging is successful, it is executed. In the venv environment, the packaging process is normal when the command line pyinstaler is used to execute the packaging command. After the packaging is successful, the following similar errors appear when the dist/xxx.exe file is executed:

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    import jcw
  File "PyInstaller\loader\pyimod03_importers.py", line 531, in exec_module
  File "jcw.py", line 3, in <module>
    import pandas as pd
ModuleNotFoundError: No module named 'pandas'

Solution:

Because the running environment is venv virtual environment, the command line packaging may use the installation environment of Python in the computer, that is, the global environment and dependency library, which leads to the failure to include the required modules in the packaging.

Use another packaging method of pyinstaler official website, Python code to run the packaging command, such as:

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import PyInstaller.__main__
import os


if __name__ == '__main__':
    pyi_args = [
        '--upx-dir=F:\\soft\\upx-3.96-win64',
        '--clean',
        '--add-data={0};.'.format(os.path.realpath('cfg.ini')),
        '--add-binary={0};driver'.format(os.path.realpath('driver/chromedriver.exe')),
        '--name=demo',
        'main.py',
        '-y'
    ]
    print("pyinstaller " + " ".join(pyi_args))
    PyInstaller.__main__.run(pyi_args=pyi_args)

Several ways of online search don’t work (you can try your own environment or not)

1.Move the import statement from the file header to the code block

2.Command line use — hidden import = missing module

 

 

Read More: