Ufuk A
Ufuk A

Reputation: 121

Pyinstaller ImportError on own module

i have two modules next to each other within a package like this:

main/
|--> __init__.py
|--> somePackageName/
    |--> __init__.py
    |--> module1.py
    |--> module2.py

In module module1.py, I am importing module2.py with this statement:

from .module1 import *

When I run python -m somePackageName.module1, the script works just fine (Command executed from outside the package)

I now want to build an .exe with pyinstaller. After executing pyinstaller module1.spec I get the error:

ImportError: attempted relative import with no known parent package
[15800] Failed to execute script module1

Sidenote: I get the same error, when I try to run python module1.py from inside the package.
Sidenote2: Do I need to work with hidden-imports and/or additional-hooks-dir ?

Thank you in advance.

Upvotes: 6

Views: 2638

Answers (1)

Philippe F
Philippe F

Reputation: 12175

Relative imports work only inside packages. From what you are describing, I think that you are converting module1.py into a .exe so it is no longer inside a package.

You need to do the following:

  • have a bootstrap module outside of your package, which uses your package facilities
  • call pyinstaller on your bootstrap module

This would yield something like:

main/
|--> runme.py
|--> __init__.py
|--> somePackageName/
    |--> __init__.py
    |--> module1.py
    |--> module2.py

Your runme.py would be like:

import module1.main
module1.main()

Then your module1 import will work correctly.

The next step is to package runme.py into an executable with pyinstaller. I just tested it, it works fine.

Upvotes: 7

Related Questions