Subject: Re: bug#10227: Python installation fails for Python 3

From: Stefano Lattarini <[email-masked]>
Date: Thu, 22 Nov 2012 14:01:07 +0100

tags 10227 - moreinfo
thanks

On 11/21/2012 06:58 PM, Reuben Thomas wrote:
> On 21 November 2012 13:41, Stefano Lattarini <[email-masked]>wrote:
> 
>> tags 10227 + moreinfo
>> thanks
>>
>> Hi Roumen, Reuben.
>>
>> I'm going through old open bugs, and I've noticed this one.  Is the
>> problem still present, after the recent updates to the python support?
>> (They should be already merged in the maint branch by now)  If yes,
>> could you please provide me with a minimal, clear reproducer, or at
>> least restart this discussion from the basics?
>>
> 
> I've just checked out the maint branch of automake, and I still see exactly
> the same problem: in m4/python.m4 the code used to get the Python package
> directory is:
> 
> am_cv_python_pythondir=`$PYTHON -c "import sys; from distutils import
> sysconfig;
> sys.stdout.write(sysconfig.get_python_lib(0,0,prefix='$am_py_prefix'))"
> 
> Assuming the default am_py_prefix of /usr/local, this gives:
> 
> $ python3
> Python 3.2.3 (default, Oct 19 2012, 19:53:16)
> [GCC 4.7.2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> from distutils import sysconfig; print
> (sysconfig.get_python_lib(0,0,'/usr/local'))
> /usr/local/lib/python3/dist-packages
> 
> i.e. exactly the same incorrect result as before (the correct answer is:
> 
> /usr/local/lib/python3.2/dist-packages
> 
> on my Ubuntu 12.10 system). I'm sorry, I can't tell whether, as Roumen
> suggests, this is an Ubuntu packaging bug, or an automake bug.
>
OK, thanks for explaining it once again.  I can now reproduce the same
issue on Debian.  I think this is something we should try to work around,
since we cannot have our installation rules broken by default on both
Debian and Ubuntu ...

> It seems to me that this is the sort of thing the pyconfigure people
> ought to know about.
> 
By peeking at their repository, I came up with the minimal patch below,
which should do the trick.  Can you verify it works for you as well?

Regards,
  Stefano

---- 8< ---- 8< ---- 8< ---- 8< ---- 8< ---- 8< ---- 8< ---- 8< ---- 8< ----

>From 30463e31e481537462713cb680279aded6a9d449 Mon Sep 17 00:00:00 2001
Message-Id: <30463e31e[email-masked]>
From: Stefano Lattarini <[email-masked]>
Date: Thu, 22 Nov 2012 12:03:07 +0100
Subject: [PATCH] python: make installed modules find by default on Debian and Ubuntu

This change fixes automake bug#10227.

The code used to get the python package directory was wrong for Python 3,
at least on Debian and Ubuntu distributions.  In the case the installation
was using the default prefix "/usr/local", python modules were incorrectly
installed in the directory

    /usr/local/lib/python3/dist-packages

(which is *not* searched by default), rather than in a directory like

    /usr/local/lib/python3.x/dist-packages

which is searched by default.

* m4/python.m4 (AM_PATH_PYTHON): Try to use the 'sysconfig' module if
possible, for better interactions with python 3.x.

Signed-off-by: Stefano Lattarini <[email-masked]>
---
 m4/python.m4 | 37 +++++++++++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)

diff --git a/m4/python.m4 b/m4/python.m4
index 50213a9..ab3dfaa 100644
--- a/m4/python.m4
+++ b/m4/python.m4
@@ -106,6 +106,25 @@ AC_DEFUN([AM_PATH_PYTHON],
     [am_cv_python_platform=`$PYTHON -c "import sys; sys.stdout.write(sys.platform)"`])
   AC_SUBST([PYTHON_PLATFORM], [$am_cv_python_platform])

+  # Just factor out some code duplication.
+  am_python_setup_sysconfig="\
+import sys
+# Prefer sysconfig over distutils.sysconfig, for better compatibility
+# with python 3.x.  See automake bug#10227.
+try:
+    import sysconfig
+except ImportError:
+    can_use_sysconfig = 0
+else:
+    can_use_sysconfig = 1
+# Can't use sysconfig in CPython 2.7, since it's broken in virtualenvs:
+# <https://github.com/pypa/virtualenv/issues/118>
+try:
+    from platform import python_implementation
+    if python_implementation() == 'CPython' and sys.version[[:3]] == '2.7':
+        can_use_sysconfig = 0
+except ImportError:
+    pass"

   dnl Set up 4 directories:

@@ -122,7 +141,14 @@ AC_DEFUN([AM_PATH_PYTHON],
      else
        am_py_prefix=$prefix
      fi
-     am_cv_python_pythondir=`$PYTHON -c "import sys; from distutils import sysconfig; sys.stdout.write(sysconfig.get_python_lib(0,0,prefix='$am_py_prefix'))" 2>/dev/null`
+     am_cv_python_pythondir=`$PYTHON -c "
+$am_python_setup_sysconfig
+if can_use_sysconfig:
+    sitedir = sysconfig.get_path('purelib', vars={'base':'$am_py_prefix'})
+else:
+    from distutils import sysconfig
+    sitedir = sysconfig.get_python_lib(0, 0, prefix='$am_py_prefix')
+sys.stdout.write(sitedir)"`
      case $am_cv_python_pythondir in
      $am_py_prefix*)
        am__strip_prefix=`echo "$am_py_prefix" | sed 's|.|.|g'`
@@ -157,7 +183,14 @@ AC_DEFUN([AM_PATH_PYTHON],
      else
        am_py_exec_prefix=$exec_prefix
      fi
-     am_cv_python_pyexecdir=`$PYTHON -c "import sys; from distutils import sysconfig; sys.stdout.write(sysconfig.get_python_lib(1,0,prefix='$am_py_exec_prefix'))" 2>/dev/null`
+     am_cv_python_pyexecdir=`$PYTHON -c "
+$am_python_setup_sysconfig
+if can_use_sysconfig:
+    sitedir = sysconfig.get_path('platlib', vars={'base':'$am_py_prefix'})
+else:
+    from distutils import sysconfig
+    sitedir = sysconfig.get_python_lib(1, 0, prefix='$am_py_prefix')
+sys.stdout.write(sitedir)"`
      case $am_cv_python_pyexecdir in
      $am_py_exec_prefix*)
        am__strip_prefix=`echo "$am_py_exec_prefix" | sed 's|.|.|g'`
-- 
1.8.0.209.gf3828dc