Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid info_name with shiv and pex #2332

Closed
harkabeeparolus opened this issue Aug 2, 2022 · 2 comments · Fixed by #2333
Closed

Invalid info_name with shiv and pex #2332

harkabeeparolus opened this issue Aug 2, 2022 · 2 comments · Fixed by #2333
Milestone

Comments

@harkabeeparolus
Copy link
Contributor

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
@davidism
Copy link
Member

davidism commented Aug 2, 2022

If you know you're packaging your app with those tools, you can set the program name directly:

main(prog_name="my-command")

@harkabeeparolus
Copy link
Contributor Author

harkabeeparolus commented Aug 2, 2022

main(prog_name="my-command")

Hey, thanks! I didn't realize that parameter existed. That's good to know. 😊

However, this used to work out of the box until #1609, so I would prefer if we could restore it. This bug has prevented me from upgrading from Click 7 for the longest time, which is why I finally found the energy to fix the problem and create a PR. 😉

@davidism davidism added this to the 8.1.4 milestone Jun 30, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants