Skip to content

Submodules within a namespace package do not appear #5167

@SpenserHaddad

Description

@SpenserHaddad

Description of the issue

When using PyInstaller to bundle submodules of a namespace package, where each submodule is its own module (see the example at PyPA, none of the submodules appear under the namespace when iterating over the namespace using pkgutil.iter_modules. Using from namespace import submodule does work, however.

Printing the namespace module after importing it in a script and from an exe shows two different results:

import namespace
print(namespace)
# Script output: <module 'subpackages' (namespace)>
# PyInstaller output: <module 'namespace' from 'path_to_namespace/__init__.pyc'>

In addition, for the code example linked below, when I ran PyInstaller with --debug all and looked in dist/pyi_ns_issue/subpackages, only submod_b (which is explicitly imported unlike submod_a) is listed.

Context information (for bug reports)

  • Output of pyinstaller --version: 4.0

  • Version of Python: e.g. 3.7.5 and 3.8.2

  • Platform: Ubuntu 20.04.1 LTS

  • Did you also try this on another platform? Does it work there? Encountered the same issue on Windows 10 Enterprise 1809

  • try the latest development version, using the following command:

pip install https://github.com/pyinstaller/pyinstaller/archive/develop.zip

Make sure everything is packaged correctly

  • start with clean installation
  • use the latest development version
  • Run your frozen program from a command window (shell) — instead of double-clicking on it
  • Package your program in --onedir mode
  • Package without UPX, say: use the option --noupx or set upx=False in your .spec-file
  • Repackage you application in verbose/debug mode. For this, pass the option --debug to pyi-makespec or pyinstaller or use EXE(..., debug=1, ...) in your .spec file.

A minimal example program which shows the error

I've made a repo with a mimimal example at: https://github.com/SpenserHaddad/pyinstaller-namespace-issue-repro

Here's the offending code from the repo:

impl_modules = pkgutil.iter_modules(subpackages.__path__, subpackages.__name__ + ".")
# impl_modules is an "empty" generator, and the nothing will happen in the loop below
for mod in impl_modules:
    loaded_mod = importlib.import_module(mod.name)
    print(loaded_mod.sayhi())

Stacktrace / full error message

(paste text here)

Please also see https://github.com/pyinstaller/pyinstaller/wiki/How-to-Report-Bugs
for more about what would use to solve the issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions