around this issue Indeed, as already noted, it's usually best to follow the PEP 8 recommendation and do your imports at the top. There are some exceptions though. The key to understanding them lies in your embedded question in your second paragraph: "at what stage does the import ... happen?" Import is actually an executable statement. When you import a module, all the executable statements in the module run. "def" is also an executable statement; its execution causes the defined name to be associated with the (already-compiled) code. So if you have:
'Expressions are not allowed at the top level' if the module is not main.swift
fixed the issue. Will look into that further In Python 2, import utils is ambiguous because it can be a relative or an absolute import. If you enable the Python 3 behaviour by adding the following import to the top of your module,
from __future__ import absolute_import
How to import from sibling module in a package, no changes in module files allowed?
Hope this helps Given a folder structure below. Files a.py and b.py generated and can not be changed. File b.py imports file a.py. Init file is empty (can be changed). Using my_package getting an error in b.py: , __init__.py :