Search code examples
pythonpyqt5cx-freeze

Create exe file from Python file using cx_Freeze


Using cx_Freeze with PyQt5, I get the following error:

ImportError: No module named 'PyQt5.Qt'

My setup.py file is as follows:

from cx_Freeze import setup, Executable

base = None

executables = [Executable("Chemistry.py", base=base)]

packages = ["idna", "sys", "pandas", "PyQt5"]
options = {
    'build_exe': {
        'packages':packages,
    },
}

setup(
    name = "<any name>",
    options = options,
    version = "<any number>",
    description = '<any description>',
    executables = executables
)

How do I fix this error? I am using Windows OS.


Solution

  • Try this solution to a similar question:

    1. Remove "PyQt5" from the packages list
    2. Let cx_Freeze copy the whole PyQt5 directory into the lib subdirectory of the build directory. You can do that by passing a (source, destination) tuple to the include_files list, which tells cx_Freeze to copy the source (a file or a whole directory) to a destination relative to the build directory (see the cx_Freeze documentation). Set the source to os.path.dirname(PyQt5.__file__), which gives the directory of the PyQt5 package (through its __init__.py file) of your Python installation, and the destination to "lib".
    3. Furthermore, if your application really uses pandas, you also need to add "numpy" to the packages list, see cx_Freeze not able to build msi with pandas and Creating cx_Freeze exe with Numpy for Python

    Altogether, try modify your setup.py script as follows:

    import os
    import PyQt5
    include_files = [(os.path.dirname(PyQt5.__file__), "lib")]
    packages = ["idna", "sys", "numpy", "pandas"]
    options = {
        'build_exe': {
            'include_files':include_files,
            'packages':packages,
        },
    }