pyinstaller / pyinstaller/pyinstaller

Handle relative imports in a package's __main__.py

Open
#2,560 16 comments 30 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Python
Stars
13.1k
Forks
2k
Avg merge
1d 3h
Merged PRs (30d)
11

Description

I have a runnable package with __main__.py:

foo/
    __init__.py
    __main__.py

I can execute this package by running python -m foo. However you cannot directly create a PyInstaller application from this package:

  • If you try pyinstaller -F foo, you'll get IOError: [Errno 21] Is a directory.
  • If you try pyinstaller -F foo/__main__.py it will build, but then you'll get ValueError: Attempted relative import in non-package when you try to run it, presumably because it took __main__.py out of the context of its package.

The work around is to create a small "stub" application that lives outside of the package and calls into it:

from foo import main
main()

...and then point PyInstaller at the stub.

It seems that PyInstaller should be able to create an application by being pointed at the package directory.

I'm sorry if this has already been addressed, but I've been unable to find anything in the documentation or anywhere else:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the three cases in the issue: python -m foo, pyinstaller -F foo, and pyinstaller -F foo/__main__.py, using the shown package layout. Then trace PyInstaller's package entry-point handling. Done means a package directory can produce a runnable application while preserving relative imports, without requiring an external stub.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
build-system
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.