[New-bugs-announce] [issue35073] 'from app import __init__' behaves differently with native import and importlib

Quentin Agren report at bugs.python.org
Fri Oct 26 01:31:45 EDT 2018


New submission from Quentin Agren <quentin.agren at gmail.com>:

I'm running Python 3.6 on Ubuntu 16.04

I don't know if this should qualify as a bug, but I noticed the following behavior difference in the (contrived?) scenario of directly importing '__init__' from a package:

## Setup ##

mkdir app
echo 'print(f"Executing app/__init__.py as {__name__}")' > app/__init__.py

## Native: executes __init__ *once* ##

python -c 'from app import __init__'
# Output:
# Executing app/__init__.py as app

## Importlib: executes __init__ *twice* ##

python -c "import importlib; importlib.import_module('.__init__', 'app')"
# Output:
# Executing app/__init__.py as app
# Executing app/__init__.py as app.__init__

Note in addition that absolute import (either with importlib or native) executes '__init__' twice.

----------
components: Library (Lib)
messages: 328512
nosy: qagren
priority: normal
severity: normal
status: open
title: 'from app import __init__' behaves differently with native import and importlib
type: behavior
versions: Python 3.6

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


More information about the New-bugs-announce mailing list