Relative import python
<p>I am loosing my mind over this. It has passed half of an year and i am still trying to figure python relative library import. </p>

<p>I am running python2.7 and python3.5 interpreter and notepad++ with NPP_EXEC plugin , and i am trying not to use external libraries before i understand each an every library inside python/lib folder, ofcourse my own libraries don't count.</p>

<p>I googled and researched the hell out of this topic and most of the responses are "use -m / -c switch " which is fine, but I can not use those switches within the script, only from console. </p>

<p>After several months i realized i needed <strong>init</strong> and <strong>main</strong> files inside folder, i have done that but still lots and lots of ImportErrors keep poping out and there is not understandable solution to this errors. </p>

<p>I have opened up pkgutil.py file and seen that there in iter_importers function next code:</p>

<p>python27</p>

<pre><code>def iter_importers(fullname=""):
"""Yield PEP 302 importers for the given module name

If fullname contains a '.', the importers will be for the package
containing fullname, otherwise they will be importers for sys.meta_path,
sys.path, and Python's "classic" import machinery, in that order.  If
the named module is in a package, that package is imported as a side
effect of invoking this function.

Non PEP 302 mechanisms (e.g. the Windows registry) used by the
standard import machinery to find files in alternative locations
are partially supported, but are searched AFTER sys.path. Normally,
these locations are searched BEFORE sys.path, preventing sys.path
entries from shadowing them.

For this to cause a visible difference in behaviour, there must
be a module or package name that is accessible via both sys.path
and one of the non PEP 302 file system mechanisms. In this case,
the emulation will find the former version, while the builtin
import mechanism will find the latter.

Items of the following types can be affected by this discrepancy:
    imp.C_EXTENSION, imp.PY_SOURCE, imp.PY_COMPILED, imp.PKG_DIRECTORY
"""
if fullname.startswith('.'):
    raise ImportError("Relative module names not supported")
if '.' in fullname: ## this is what got me to depression stage
    # Get the containing package's __path__
    pkg = '.'.join(fullname.split('.')[:-1])
    if pkg not in sys.modules:
        __import__(pkg)
    path = getattr(sys.modules[pkg], '__path__', None) or []
else:
    for importer in sys.meta_path:
        yield importer
    path = sys.path
for item in path:
    yield get_importer(item)
if '.' not in fullname:
    yield ImpImporter()
</code></pre>

<p>python35</p>

<pre><code>def iter_importers(fullname=""):
"""Yield PEP 302 importers for the given module name

If fullname contains a '.', the importers will be for the package
containing fullname, otherwise they will be all registered top level
importers (i.e. those on both sys.meta_path and sys.path_hooks).

If the named module is in a package, that package is imported as a side
effect of invoking this function.

If no module name is specified, all top level importers are produced.
"""
if fullname.startswith('.'):
    msg = "Relative module name {!r} not supported".format(fullname)
    raise ImportError(msg)
if '.' in fullname:
    # Get the containing package's __path__
    pkg_name = fullname.rpartition(".")[0]
    pkg = importlib.import_module(pkg_name)
    path = getattr(pkg, '__path__', None)
    if path is None:
        return
else:
    yield from sys.meta_path
    path = sys.path
for item in path:
    yield get_importer(item)
</code></pre>

<p>Since in pkgutil.py file in iter_imports stats that if fullname starts with '.' it will raise error without computing anything it seams to me that relative import does not exist in python language ( which is a shame), but then i am confused because python official page states that relative imports are possible  <a href="https://docs.python.org/2.5/whatsnew/pep-328.html" rel="nofollow noreferrer">PEP 328</a>, but how it is done escapes the official page, it just describes the procces without any structural about files.</p>

<p>If anyone can use relative import with python i would be very thankfull if sed person can do next:</p>

<p>describe minimal requirements for relative import:</p>

<ul>
<li>structure of folder</li>
<li>structure of <strong>init</strong>.py and <strong>main</strong>.py file ( if relevant )
  since i know that structure of <strong>init</strong> file can vary from code to code, i think that best syntax would be:
      some examples
          <strong>all</strong> = list of strings for all function names
          <strong>loader</strong> = if overrided SourceFileLoader object ( you can find it in %path% )
         etc etc</li>
<li>path managment (if required) minimal use and how it is done</li>
<li>and since i dont know how to relative import , please, describe your procces of doing it.</li>
</ul>

<p>I am doing this because i hope if anyone can answear this question it would help any newb like me and anyother person that is just starting with python to use this awesome function of python ( if it exists ) and therefore stop this question from being repeated.</p>