From ac7157dfc620471cee3376cfc3f8312dff48d32c Mon Sep 17 00:00:00 2001 From: Mike Romberg Date: Mon, 6 Jun 2016 13:43:58 -0600 Subject: [PATCH] Update -relative import documentation to reflect runtime check. --- Doc/Manual/Python.html | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/Doc/Manual/Python.html b/Doc/Manual/Python.html index 34870a285..e6c23bc8c 100644 --- a/Doc/Manual/Python.html +++ b/Doc/Manual/Python.html @@ -5680,8 +5680,9 @@ class M2(pkg2.mod3.M3): pass

By default, SWIG would generate mod2.py proxy file with import directive as in point 1. This can be changed with the -relativeimport command line option. The -relativeimport instructs -SWIG to organize imports as in point 2 (for Python 2.x) or as in point 4 (for -Python 3, that is when the -py3 command line option is enabled). In short, if you have +SWIG to organize imports as in point 2 (for Python < 2.7.0) or as in point 4 +for Python 2.7.0 and newer. This is a check done at the time the module is +imported. In short, if you have mod2.i and mod3.i as above, then without -relativeimport SWIG will write

@@ -5695,22 +5696,16 @@ import pkg1.pkg2.mod3 write

-
-import pkg2.mod3
+  
+import sys
+if sys.version_info >= (2, 7, 0):
+    from . import pkg2
+    import pkg1.pkg2.mod3
+else:
+    import pkg2.mod3
 
-

if -py3 is not used, or

- -
-
-from . import pkg2
-import pkg1.pkg2.mod3
-
-
- -

when -py3 is used.

-

You should avoid using relative imports and use absolute ones whenever possible. There are some cases, however, when relative imports may be necessary. The first example is, when some (legacy) Python code refers entities