diff --git a/Doc/Manual/Python.html b/Doc/Manual/Python.html index 34870a285..83baa1a47 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,17 @@ import pkg1.pkg2.mod3 write-import pkg2.mod3 ++from sys import version_info +if version_info >= (2, 7, 0): + from . import pkg2 + import pkg1.pkg2.mod3 +else: + import pkg2.mod3 +del version_info
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 diff --git a/Examples/python/import_packages/namespace_pkg/runme.py b/Examples/python/import_packages/namespace_pkg/runme.py index 4d0aa755c..9c22d36fb 100644 --- a/Examples/python/import_packages/namespace_pkg/runme.py +++ b/Examples/python/import_packages/namespace_pkg/runme.py @@ -1,8 +1,17 @@ # These examples rely on namespace packages. Don't # run them for old python interpreters. import sys +import os.path + +testname = os.path.basename(os.path.dirname(os.path.abspath(__file__))) +print "Testing " + testname + " - namespace packages" + if sys.version_info < (3, 3, 0): + print " Not importing nstest as Python version is < 3.3" sys.exit(0) import nstest + +print " Finished importing nstest" + nstest.main() diff --git a/Examples/python/import_packages/split_modules/vanilla/runme.py b/Examples/python/import_packages/split_modules/vanilla/runme.py index 8a6055bf1..a188364f1 100644 --- a/Examples/python/import_packages/split_modules/vanilla/runme.py +++ b/Examples/python/import_packages/split_modules/vanilla/runme.py @@ -1,3 +1,10 @@ +import os.path + +testname = os.path.basename(os.path.dirname(os.path.abspath(__file__))) +print "Testing " + testname + " - split modules" + import pkg1.foo +print " Finished importing pkg1.foo" + assert(pkg1.foo.count() == 3) diff --git a/Examples/python/import_packages/split_modules/vanilla_split/runme.py b/Examples/python/import_packages/split_modules/vanilla_split/runme.py index 8a6055bf1..a188364f1 100644 --- a/Examples/python/import_packages/split_modules/vanilla_split/runme.py +++ b/Examples/python/import_packages/split_modules/vanilla_split/runme.py @@ -1,3 +1,10 @@ +import os.path + +testname = os.path.basename(os.path.dirname(os.path.abspath(__file__))) +print "Testing " + testname + " - split modules" + import pkg1.foo +print " Finished importing pkg1.foo" + assert(pkg1.foo.count() == 3) diff --git a/Source/Modules/python.cxx b/Source/Modules/python.cxx index 22703bb72..a7e76cb0f 100644 --- a/Source/Modules/python.cxx +++ b/Source/Modules/python.cxx @@ -1251,13 +1251,14 @@ public: Printf(out, "import %s%s%s%s\n", apkg, *Char(apkg) ? "." : "", pfx, mod); Delete(apkg); } else { - if (py3) { - if (py3_rlen1) - Printf(out, "from . import %.*s\n", py3_rlen1, rpkg); - Printf(out, "from .%s import %s%s\n", rpkg, pfx, mod); - } else { - Printf(out, "import %s%s%s%s\n", rpkg, *Char(rpkg) ? "." : "", pfx, mod); - } + Printf(out, "from sys import version_info\n"); + Printf(out, "if version_info >= (2, 7, 0):\n"); + if (py3_rlen1) + Printf(out, tab4 "from . import %.*s\n", py3_rlen1, rpkg); + Printf(out, tab4 "from .%s import %s%s\n", rpkg, pfx, mod); + Printf(out, "else:\n"); + Printf(out, tab4 "import %s%s%s%s\n", rpkg, *Char(rpkg) ? "." : "", pfx, mod); + Printf(out, "del version_info\n"); Delete(rpkg); } return out;