diff --git a/Doc/Manual/Contents.html b/Doc/Manual/Contents.html index ffb467c35a7..b75da40e3ce 100644 --- a/Doc/Manual/Contents.html +++ b/Doc/Manual/Contents.html @@ -1595,6 +1595,12 @@
+Python3 adds another option for packages with +PEP 0420 (implicit +namespace packages). Implicit namespace packages no longer use +__init__.py files. SWIG generated Python modules support implicit +namespace packages. See +36.11.5 Implicit Namespace +Packages for more information. +
+ ++If you place a SWIG generated module into a Python package then there +are details concerning the way SWIG +searches for the wrapper module +that you may want to familiarize yourself with. +
+The way Python defines its modules and packages impacts SWIG users. Some users may need to use special features such as the package option in the %module directive or import related command line options. These are @@ -5939,6 +5962,109 @@
+When SWIG creates wrappers from an interface file, say foo.i, two Python modules are +created. There is a pure Python module module (foo.py) and C/C++ code which is +built and linked into a dynamically (or statically) loaded module _foo +(see the Preliminaries section for details). So, the interface +file really defines two Python modules. How these two modules are loaded is +covered next. +
+ ++The pure Python module needs to load the C/C++ module in order to link +to the wrapped C/C++ methods. To do this it must make some assumptions +about what package the C/C++ module may be located in. The approach the +pure Python module uses to find the C/C++ module is as follows: +
+ +The pure Python module, foo.py, tries to load the C/C++ module, _foo, from the same package foo.py is + located in. The package name is determined from the __name__ + attribute given to foo.py by the Python loader that imported + foo.py. If foo.py is not in a package then _foo is loaded + as a global module.
+If the above import of _foo results in an ImportError + being thrown, then foo.py makes a final attempt to load _foo + as a global module.
++As an example suppose foo.i is compiled into foo.py and _foo.so. Assuming +/dir is on PYTHONPATH, then the two modules can be installed and used in the +following ways: +
+ + +Both modules are in one package:
++/dir/package/foo.py +/dir/package/__init__.py +/dir/package/_foo.so ++
And imported with
++from package import foo ++
The pure python module is in a package and the C/C++ module is global:
++/dir/package/foo.py +/dir/package/__init__.py +/dir/_foo.so ++
And imported with
++from package import foo ++
Both modules are global:
++/dir/foo.py +/dir/_foo.so ++
And imported with
++import foo ++
+If _foo is statically linked into an embedded Python interpreter, then it may or +may not be in a Python package. This depends in the exact way the module was +loaded statically. The above search order will still be used for statically +loaded modules. So, one may place the module either globally or in a package +as desired. +
+