Entry points on Windows do not set __package__ correctly
See original GitHub issueWerkzeug provides a reloader for WSGI applications like Flask. Because Python behaves slightly differently when run as python file
vs python -m module
, the reloader needs to figure out how Python was invoked in order to invoke it the same way on reload. To determine if -m module
was used, it looks at __main__.__package__
. If it’s set to None
, it was run as a file, otherwise it was run as a package. See explanation here and in PEP 366:
When the main module is specified by its filename, then the
__package__
attribute will be set toNone
.
However, entry points on Windows are not behaving this way. Instead, __package__
is the empty string when running an entry point. This caused an issue with the Werkzeug 0.15.5 release, see pallets/werkzeug#1614.
Additionally, entry points on Windows are behaving correctly when the package is installed in editable mode (pip install -e .
), and they behave consistently and correctly on other platforms. So there seems to be an inconsistency in how setuptools creates entry points on Windows.
The following two files reproduce this. Use pip install -e .
then run ep-example
, observe the output. Then use pip install .
and ex-example
, and observe that the output is different. You can confirm that the output is consistent by doing the same on Linux.
ep_example.py
:
import sys
def main():
__main__ = sys.modules["__main__"]
print("__main__.__package__:", repr(__main__.__package__))
setup.py
:
from setuptools import setup
setup(
name="ep-example",
version="0.1.0",
py_modules=["ep_example"],
entry_points={"console_scripts": ["ep-example=ep_example:main"]},
)
Correct behavior for editable install on Windows, and editable and dist installs on Linux or Mac:
> pip install -e .
> ep-example
__main__.__package__: None
Incorrect behavior for dist install on Windows:
> pip install .
> ep-example
__main__.__package__: ''
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:14 (9 by maintainers)
Top GitHub Comments
I’m not clear that it’s the script, since the pip and distlib scripts behave correctly on Linux. Maybe it’s some quirk of the exe.
The actual change may end up being in pypa/distlib, which pip vendors.