Bypassing __init__.py on Import
How can you import a python module and bypass __init__.py
?
# nothing_changed/__init__.py
raise Exception('bad news from __init__')
# nothing_changed/isolated.py
def isolated():
pass
By default, Python will see __init__.py
defining the module and run it.
def test_import_with_init():
with pytest.raises(Exception, match='bad news from __init__'):
import nothing_changed.isolated
To get around this, add the path to the module and import the isolated part of it directly.
def test_import_without_init():
import sys
sys.path.append('./nothing_changed/')
import isolated
There may be a more elegant approach using custom import features, but this works.