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>The difference here is that when you install ansible, it puts files into a subdirectory which is accessible from your <code>sys.path</code>.  For example, it might go here:</p>

<pre><code>/home/leahcim/.local/lib/python2.7/site-packages'
</code></pre>

<p>That's just a guess at the location.  <code>import ansible</code> and check on <code>ansible.__file__</code> for the actual location it ended up in (the exact location is dependent on several things).  </p>

<p>Now, if you write a <code>setup.py</code> file, you could also get your app visible locally with:</p>

<pre><code>python setup.py develop
</code></pre>

<p>If you don't want to write a working <code>setup.py</code> for your app just yet, then you will need to find some other way to make it visible in <code>sys.path</code>.  </p>

<p>Specifically, the <strong>parent directory of your "beatles" directory</strong> should be contained in <code>sys.path</code>.  One easy way to do that is to export it in the environment variable <code>PYTHONPATH</code>.  </p>

python