[New-bugs-announce] [issue38457] __package__ is None in __init__.py until an import is used

Yuval S report at bugs.python.org
Sat Oct 12 17:48:12 EDT 2019


New submission from Yuval S <sadan.yuval at gmail.com>:

In Python 2.7, the __package__ variable isn't set when __init__.py is run, until an import is done. In Python 3.5, it is set correctly.

e.g.

# mkdir x && echo "print(__package__)" | tee x/__init__.py x/test.py
# python2 x.test
None
x
# python3 x.test
x
x
# echo -e "import os\n$(cat x/__init__.py)" > x/__init__.py
# python2 x.test
x
x

This is very old, as it's there at least since 2010: https://stackoverflow.com/questions/4437394/package-is-none-when-importing-a-python-module 

I would expect Python 2 and 3 to behave the same.

----------
components: Library (Lib)
messages: 354555
nosy: Yuval S
priority: normal
severity: normal
status: open
title: __package__ is None in __init__.py until an import is used
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38457>
_______________________________________


More information about the New-bugs-announce mailing list