Skip to content

Invalid info_name with shiv and pex #2332

@harkabeeparolus

Description

@harkabeeparolus

Summary

When Click is used inside zipapps produced by Shiv or PEX, it displays an invalid "Usage" string. Instead of "Usage: program" it says "Usage: python -m program".

I did some digging and found that this is due to info_name being guessed incorrectly in the code introduced by #1609. There is a test for __package__ being set to None; however, in the case when the script is run inside Shiv or PEX, __package__ is instead set to the empty string (""). This is allowed by PEP 366:

Note that setting package to the empty string explicitly is permitted, and has the effect of disabling all relative imports from that module (since the import machinery will consider it to be a top level module in that case).

Reproducing

Follow the basic Shiv hello world example, but add Click:

hello.py:

import click

@click.command()
def main():
    print("Hello world")

if __name__ == "__main__":
    main()

setup.py:

from setuptools import setup

setup(
    name="hello-world",
    version="0.0.1",
    description="Greet the world.",
    py_modules=["hello"],
    entry_points={
        "console_scripts": ["hello=hello:main"],
    },
    install_requires=['Click==8.1.3'],
)

Build the executable zipapps and run them:

$ shiv . -c hello -o hello1.pyz
$ pex . -c hello -o hello2.pyz

$ ./hello1.pyz --help
Usage: python -m hello1 [OPTIONS]

$ ./hello2.pyz foo
Usage: python -m hello2 [OPTIONS]
Try 'python -m hello2 --help' for help.

Expected behavior

Click should not print the Usage string as "python -m hello"; it should just be "hello".

Environment:

  • Python version: 3.10.5
  • Click version: 8.1.3
  • Shiv version: 1.0.1
  • Pex version: 2.1.102

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions