Differences between `import module` and `from package import module`
<p>I've been learning me some dynamic 'plugin loading' for python and noticed a not really problematic but rather an interesting difference between <code>import module</code> and <code>from package import module</code>.</p>

<p>I've created a test script consisting of four files (resembles my own setup for what I want to achieve)</p>

<p>The file tree looks like this:</p>

<ul>
<li>test (main package)
<ul>
<li>sup (package, plugin folder)
<ul>
<li>__init__.py</li>
<li>uber.py (plugin)</li>
</ul></li>
<li>__init__.py</li>
<li>bar.py ('main'-program)</li>
<li>foo.py (object that needs dynamically added functionality)</li>
<li>poo.py (decorator)</li>
</ul></li>
</ul>

<p>poo.py:</p>

<pre><code>from test import foo

def decorate(method):
    print "before:", method.__name__ in dir(foo.Foo)
    setattr(foo.Foo, method.__name__, method)
    print "after :", method.__name__ in dir(foo.Foo)
    return method
</code></pre>

<p>foo.py:</p>

<pre><code>import os

class Foo(object):

    def __init__(self):
        self.__loadplugins("sup")

    @classmethod
    def __loadplugins(cls, plugindir):
        for f in os.listdir(os.path.join(os.path.dirname(__file__), plugindir)):
            if f.endswith(".py"):
                __import__(("%s.%s" % (plugindir, f))[0:-3])
</code></pre>

<p>uber.py:</p>

<pre><code>from test import poo

@poo.decorate
def aFunction(self, anArg):
    print anArg
</code></pre>

<p>I have two versions of bar.py, this one does not work:</p>

<pre><code>import foo

f = foo.Foo()

f.aFunction("print goes here") # pylint: disable-msg=E1101
</code></pre>

<p>This one does work:</p>

<pre><code>from test import foo

f = foo.Foo()

f.aFunction("print goes here") # pylint: disable-msg=E1101
</code></pre>

<p>The only difference between the two bars is the import. One is relative the other one is not. But the relative one does not work, and the absolute one does work. Is there anyone who can replicate this on his machine and could give some kind of explanation of why it is happening?</p>

<p><strong>update</strong><br>
Thought it would be useful to also note my python version:
Using normal python release 2.7.2 x86</p>

<p><strong>update</strong><br>
Output of the 'wrong' bar.py:</p>

<pre class="lang-none prettyprint-override"><code>before: False
after : True
Traceback (most recent call last):
  File "C:\Users\Daan\workspace\python\mytests\src\test\bar.py", line 6, in &lt;module&gt;
    f.aFunction("print goes here") # pylint: disable-msg=E1101
AttributeError: 'Foo' object has no attribute 'aFunction'
</code></pre>

<p>Output of the 'correct' bar.py:</p>

<pre class="lang-none prettyprint-override"><code>before: False
after : True
print goes here
</code></pre>
