'cx_Freeze can't find local package

I'm using dash to create a standalone desktop app. I want to use cx_Freeze to create an executable for my app.

Here's the cx_setup.py file:

import sys

from setuptools import find_packages
from cx_Freeze import setup, Executable

options = {
    'build_exe': {
        'includes': [
            'cx_Logging', 'idna', 'CustomModule'
        ],
        'packages': [
            'asyncio', 'flask', 'jinja2', 'dash', 'plotly'
        ],
        'excludes': [
            'tkinter'
        ],
        'include_files': [
            'database.ini'
        ]
    }
}

base = 'console'
if sys.platform == 'win32':
    base = 'Win32GUI'

executables = [
    Executable('server.py',
               base=base,
               target_name='App.exe')
]

setup(
    name='App',
    packages=find_packages(),
    version='0.5.0',
    description='',
    executables=executables,
    options=options
)

Here's what the dir looks like:

project
│  venv
│
└──src
   │
   └───myPackage
       │   cx_setup.py
       │   Module1.py
       │   Module2.py

Module1.py has the following import statement:

from src.myPackage import Module2Class as mc

cx_Freeze has no problem building the exe but when it tries to run it throws the error:

ModuleNotFoundError: No module named 'src.myPackage'

I've tried putting myPackage in cx_setup.py script but it says that the package doesn't exist. I also used a setup.py to install the package using pip install . to my venv.



Solution 1:[1]

Try to add an empty __init__.py file in the src directory.

The import statement:

from src.myPackage import Module2Class as mc

implies that the src directory is treated as a Python module, however cx_Freeze will not recognize it as a module if the __init__.py file is missing (even if some IDEs will) and thus will fail to include files from there into the frozen executable.

You can also try to explicitly add src.myPackage to the packages list of the build_exe options:

options = {
    'build_exe': {
        ...
        'packages': [
            'src.myPackage', ...
        ],
        ...
    }
}

but this might not work either, for the same reason.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jpeg