Install Python2.7 w/ Tkinter

Print Friendly, PDF & Email

Rhys asked for us to install Tkinter for Python2.7.  [Remember, that Python2.6 is the default Python on Tempest because it’s what RHEL comes with, but most users are using Python 2.7.]  Here’s the current behavior:

[anderson@tempest ~] python2.6
Python 2.6.6 (r266:84292, Nov 21 2013, 10:50:32) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from Tkinter import *
>>> 
[anderson@tempest ~] python2.7
Python 2.7 (r27:82500, Sep 20 2012, 17:09:01) 
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from Tkinter import *
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 39, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter
>>>

Which is obviously no good.  This seemed pretty straightforward, since the fact that it works for Python2.6 suggests that I have all the necessary .so files and such.  However, I couldn’t find any pre-compiled binaries for Python2.7 for RHEL6/CentOS6.  I tried compiling Python for myself, just like I did last August (https://blogs.wellesley.edu/cssysadmin/2012/08/29/python-2-7/  However, that resulted in exactly the same ImportError.

Here’s the best of the “how to compile Python” info pages that I found.  However, I tried one more time, but this time, I discovered a comment that explained that I have to install tk-devel.  That, however, is a piece of cake.

# yum -y install tk-devel

To avoid any issue with over-writing the existing Python2.7, which is installed in /usr/local/, I installed the new, improved, Python2.7 in /opt:

# wget http://python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz
# tar xf Python-2.7.6.tar.xz
# cd Python-2.7.6
# ./configure --prefix=/opt --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /opt/lib"
# make && make altinstall

The first time I tried that, it failed miserably:

checking build system type... x86_64-unknown-linux-gnu
 checking host system type... x86_64-unknown-linux-gnu
 checking for --enable-universalsdk... no
 checking for --with-universal-archs... 32-bit
 checking MACHDEP... linux2
 checking EXTRAPLATDIR...
 checking for --without-gcc... no
 checking for gcc... gcc
 checking whether the C compiler works... no
 configure: error: in `/var/tmp/install-python2.7/Python-2.7.6':
 configure: error: C compiler cannot create executables
 See `config.log' for more details

It turned out that the trouble was that there wasn’t a /opt/lib directory.  Doing a “mkdir /opt/lib” and re-doing the config command worked perfectly, as did the “make” and “make altinstall”

So, now we have:

Python 2.7.6 (default, Mar 24 2014, 13:29:51)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from Tkinter import *
>>>

But, we also need to make sure that the new Python2.7 has all the libraries that the old Python2.7 had.

I learned that Pip can tell you the libraries it has loaded:

# /usr/local/bin/pip-2.7 freeze
CouchDB==0.9
MySQL-python==1.2.4b5
Orange==2.5a4
anyjson==0.3.3
beautifulsoup4==4.3.2
biopython==1.63
couchdbkit==0.6.5
dumptruck==0.1.5
gensim==0.8.6
numpy=1.6.2
scikit-learn=0.12.1
...

Compare to:

# /opt/bin/pip2.7 freeze
wsgiref==0.1.2

We can dump the former into a file and use it to set up the new Python:

# umask 0002
# /opt/bin/pip2.7 freeze
wsgiref==0.1.2
# rm old-py2.7-modules
# /usr/local/bin/pip-2.7 freeze > old-py2.7-modules
# /opt/bin/pip2.7 install -r old-py2.7-modules
Downloading/unpacking CouchDB==0.9 (from -r old-py2.7-modules (line 1))
Downloading CouchDB-0.9.tar.gz (55kB): 55kB downloaded
Running setup.py (path:/tmp/pip_build_root/CouchDB/setup.py) egg_info for package CouchDB

Downloading/unpacking MySQL-python==1.2.4b5 (from -r old-py2.7-modules (line 2))
Downloading MySQL-python-1.2.4b5.tar.gz (82kB): 82kB downloaded
...

Well, that *mostly* worked.  We ran into trouble after a while:

Downloading/unpacking scikit-learn==0.12.1 (from -r old-py2.7-modules (line 27))
Downloading scikit-learn-0.12.1.tar.gz (3.0MB): 3.0MB downloaded
Running setup.py (path:/tmp/pip_build_root/scikit-learn/setup.py) egg_info for package scikit-learn
Partial import of sklearn during the build process.
Traceback (most recent call last):
File "<string>", line 17, in <module>
File "/tmp/pip_build_root/scikit-learn/setup.py", line 36, in <module>
from numpy.distutils.core import setup
ImportError: No module named numpy.distutils.core
Complete output from command python setup.py egg_info:
Partial import of sklearn during the build process.

Traceback (most recent call last):

File "<string>", line 17, in <module>

File "/tmp/pip_build_root/scikit-learn/setup.py", line 36, in <module>

from numpy.distutils.core import setup

ImportError: No module named numpy.distutils.core

----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /tmp/pip_build_root/scikit-learn
Traceback (most recent call last):
File "/opt/bin/pip2.7", line 9, in <module>
load_entry_point('pip==1.5.4', 'console_scripts', 'pip2.7')()
File "/opt/lib/python2.7/site-packages/pip-1.5.4-py2.7.egg/pip/__init__.py", line 185, in main
return command.main(cmd_args)
File "/opt/lib/python2.7/site-packages/pip-1.5.4-py2.7.egg/pip/basecommand.py", line 161, in main
text = '\n'.join(complete_log)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 70: ordinal not in range(128)

How much got installed?  Let’s see:

# /opt/bin/pip2.7 freeze
wsgiref==0.1.2

Well, that stinks! Is it all or nothing?  The error output suggests that there may be a dependency on numpy, so let’s install that first:

# /opt/bin/pip2.7 install numpy
# /opt/bin/pip2.7 freeze
numpy==1.8.0
wsgiref==0.1.2

Okay, and I notice that the version is newer than the numpy in the old Python2.7. So, this will be annoying and tedious, but I guess I may have to install things by hand. One more try:

# /usr/local/bin/pip-2.7 freeze | cut -d= -f1 | grep -v numpy > old-py2.7-module-names
# /opt/bin/pip2.7 install -r old-py2.7-module-names

Wait a *long* time, and it succeeds!

# diff old-py2.7-module-names new-py2.7-module-names
15a16
> numpy
26a28
> scipy

Woo hoo!

Note that pip can be used to upgrade Python packages.

About CS SysAdmins

The CS Department System Administrators
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *