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

Script block in __init__.py #1401

Closed
gribok opened this issue Feb 27, 2020 · 5 comments
Closed

Script block in __init__.py #1401

gribok opened this issue Feb 27, 2020 · 5 comments

Comments

@gribok
Copy link
Contributor

gribok commented Feb 27, 2020

Hi all,

I am trying to understand the following statement in __init__.py

if __name__ == '__main__': # pragma: no cover
from pygments.cmdline import main
sys.exit(main(sys.argv))

As I understood, the mentioned lines should never be executable. An __init__.py declares directory as a package und could provide functions on top-level.
But is it possible to execute __init__.py as script, like per __main__.py?

Thanks in advance.

@Anteru
Copy link
Collaborator

Anteru commented Feb 27, 2020

This is invoked when you run it like so: python -m pygments.

@gribok
Copy link
Contributor Author

gribok commented Feb 27, 2020

This is invoked when you run it like so: python -m pygments.

With python -m pygments will be __main__.py invoked.

If I am delete __main__.py, I will get following error msg:

$ python -m pygments -h
/usr/bin/python3.6: No module named pygments.__main__; 'pygments' is a package and cannot be directly executed

Maybe do you have an example to execute __init__.py as script?

@Anteru
Copy link
Collaborator

Anteru commented Feb 28, 2020

Oh ... I see, I somehow thought that calls __init__.py, my bad. I have no idea why it's in __init__.py then, maybe @birkenfeld can shed some light on this :) It was there from the very beginning, 14 years ago -- do you happen to know when __main__.py was introduced?

@gribok
Copy link
Contributor Author

gribok commented Mar 4, 2020

I think, we have a clear understanding, that the mentioned part doesn't work in current python version. So I am closing the issue.

@gribok gribok closed this as completed Mar 4, 2020
@birkenfeld
Copy link
Member

It's probably there by accident and can be removed.

@birkenfeld birkenfeld reopened this Mar 7, 2020
@Anteru Anteru closed this as completed in a990205 Mar 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants