How to use the root package in an import?
<p>Taking the <a href="https://github.com/ansible/ansible/tree/devel/lib/ansible" rel="nofollow">Ansible</a> project as an example, there is, inside the <a href="https://github.com/ansible/ansible/tree/devel/lib/ansible" rel="nofollow">lib/ansible root directory</a>, many other packages, like this</p>

<pre><code>ansible/cli
       /errors
       /...many omitted for brevity
       /utils
       /vars
       __init__.py
</code></pre>

<p>As an example, from <code>ansible/utils/vars.py</code>, the imports start at the root ansible/lib directory, such as</p>

<pre><code>   from ansible.errors import AnsibleError
</code></pre>

<p>and</p>

<pre><code>from ansible.errors import AnsibleError
</code></pre>

<p>rather than</p>

<pre><code>from errors import AnsibleError etc.
</code></pre>

<p>I have a demo project with a directory structure like this</p>

<pre><code>beatles/george/harrison.py
       /george/__init__.py
       /john/lennon.py
       /john/__init__.py
       /ringo/starr.py
       /ringo/__init__.py
       /paul/mccartney.py
       /paul/__init__.py
       __init__.py (this __init__.py file is in the beatles dir)
       helpers.py
</code></pre>

<p>So, using the Ansible example, I tried (in file <code>john/lennon.py</code>) to do</p>

<p>from beatles.george.harrison import Guitar</p>

<p>but I get an error `No module named beatles.george.harrison</p>

<p>However, the code works fine if I omit the <code>beatles</code> package (the equivalent of ansible in the ansible project)</p>

<p>from george.harrison import Guitar</p>

<p>How come, with my directory structure, I am not able to code the import as</p>

<pre><code>from beatles.george.harrison import Guitar
</code></pre>

<p>from the beatles/john/lennon.py file? If possible, please explain with reference to the ansible project so it's clear why it works in one situation but not the other.</p>

<pre><code>&gt;&gt;&gt; from beatles.john.lennon import Guitar Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; ImportError: No module named beatles.john.lennon 
&gt;&gt;&gt; from john.lennon import Guitar &gt;&gt;&gt; Guitar &lt;class 'john.lennon.Guitar'&gt;
</code></pre>

<p>Works for me. Structure:</p>

<pre><code>.
âââ beatles
    âââ george
    âÂ Â  âââ harrison.py
    âÂ Â  âââ __init__.py
    âââ __init__.py
    âââ john
    âÂ Â  âââ __init__.py
    âÂ Â  âââ lennon.py
    âââ paul
    âÂ Â  âââ __init__.py
    âÂ Â  âââ mccartney.py
    âââ ringo
        âââ __init__.py
        âââ starr.py
</code></pre>

<p>and:</p>

<pre><code>$ cat beatles/john/lennon.py

from beatles.george.harrison import Guitar

def Guitar(x):
    return "John's guitar"
</code></pre>

<p>and:</p>

<pre><code>$ cat beatles/george/harrison.py

def Guitar(x):
    return "George's guitar"
</code></pre>

<p>and I can do, <strong>with my working directory being the one with the <code>beatles</code> folder</strong>:</p>

<pre><code>&gt;&gt;&gt; from beatles.john.lennon import Guitar
&gt;&gt;&gt; Guitar(1)
"John's guitar"
</code></pre>

<p>Which seems to be exactly your setup as described but does not give an error message when <code>lennon.py</code> imports <code>Guitar</code> from <code>harrison.py</code>.</p>

<pre><code>Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
</code></pre>

<p>and</p>

<pre><code>&gt;&gt;&gt; sys.path
['', '/usr/lib/py...[etc]
</code></pre>

<p>so that my working directory is the first item in the path. Have you tried to install your package? Or is it in your working directory?</p>

python