This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 8: Python packages

Namespace packages

So far, we have created what is known as namespace packages.

Namespace packages are useful as they also allow you to spread the files in your package across different locations.

For example, say you have your modules distributed across two different locations on your hard drive. But both are still part of the same package.

main.py
site1/
    sound/
        formats/
            wavread.py
site2/
    sound/
        formats/
            wavwrite.py

Let’s say you want to import your package from main.py. So you have to append the paths to your package to sys.path.

Then you can import the package from both places as if they are part of the same package.

import sys
sys.path.append("site1")
sys.path.append("site2")

from sound.formats import wavread, wavwrite

This feature is really only useful if you have a very very large collection of packages and cannot have all of them in the same directory.