This is usually used with pkgutil so that the package is placed on disk. For example, zope.interface and zope.schema are separate distributions ( zope is a "namespace package"). You can have zope.interface installed in /usr/lib/python2.6/site-packages/zope/interface/ , while you use zope.schema locally in /home/me/src/myproject/lib/python2.6/site-packages/zope/schema .
If you put pkgutil.extend_path(__path__, __name__) in /usr/lib/python2.6/site-packages/zope/__init__.py , then both zope.interface and zope.schema will be imported because pkgutil will have a __path__ change - ['/usr/lib/python2.6/site-packages/zope', '/home/me/src/myproject/lib/python2.6/site-packages/zope'] .
pkg_resources.declare_namespace (part of Setuptools) is similar to pkgutil.extend_path , but knows more about zips on the way.
Manually modifying __path__ is unusual and probably not necessary, although it is useful to consider the variable when debugging import problems with namespace packages.
You can also use __path__ for monkeypatching, for example, I have a monkey passed through distutils, creating a distutils/__init__.py , which is in the early stage of sys.path :
import os stdlib_dir = os.path.dirname(os.__file__) real_distutils_path = os.path.join(stdlib_dir, 'distutils') __path__.append(real_distutils_path) execfile(os.path.join(real_distutils_path, '__init__.py')) # and then apply some monkeypatching here...
Ian Bicking Apr 23 '10 at 18:12 2010-04-23 18:12
source share