Leahcim Leahcim - 4 months ago 16
Python Question

How to use the root package in an import?

Taking the Ansible project as an example, there is, inside the lib/ansible root directory, many other packages, like this

/...many omitted for brevity

As an example, from
, the imports start at the root ansible/lib directory, such as

from ansible.errors import AnsibleError


from ansible.errors import AnsibleError

rather than

from errors import AnsibleError etc.

I have a demo project with a directory structure like this

__init__.py (this __init__.py file is in the beatles dir)

So, using the Ansible example, I tried (in file
) to do

from beatles.george.harrison import Guitar

but I get an error `No module named beatles.george.harrison

However, the code works fine if I omit the
package (the equivalent of ansible in the ansible project)

from george.harrison import Guitar

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

from beatles.george.harrison import Guitar

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.

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

wim wim

The difference here is that when you install ansible, it puts files into a subdirectory which is accessible from your sys.path. For example, it might go here:


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

Now, if you write a setup.py file, you could also get your app visible locally with:

python setup.py develop

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

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