Tuesday, August 12, 2014

Python 3 on Centos 70


Try running the code with Python 2.7
[root@centos7 Python-3.4.1]# python
Python 2.7.5 (default, Jun 17 2014, 18:11:42)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> f=open("/etc/yum.conf")
>>> for l in f.readlines():
...   print (l,end="")
  File "<stdin>", line 2
    print (l,end="")
                ^
SyntaxError: invalid syntax

In Centos Linux, yum  (Yellowdog Updater Modifier) is a program used to install and update programs.  It's called a package manager in Linux Speak.

yum is a written in the interpreted language Python and so I though I should at least learn to write a simple Python program.

Imagine my annoyance when my first 2 line program failed using Python 2.7 which is what ships with Centos 70





Oh so Python 3?
So it seems that Python 3 dates back to 2009 but that language syntax changes mean that many vendors like Centos (aka Red Hat Enterprise) have remained with Python 2.x

I would prefer to learn the latest Python so that at the very least I can pretend to talk knowledgeably about Python 3 to UNIX noobs and berate them for /only learning/ Python 2.

So here I will endeavour to install Python 3 in a different place to the system default, because if I replaced /usr/bin/python directly then the whole of Centos 70 Linux would probably become very sick indeed.

Make a Backup
Before attempting to install Python 3 I made a backup.  If my installation went terribly wrong then I'd revert back.  If you follow this tutorial, then of course I expect you to do the same.

The Procedure:


Install C compiler
yum group install "Development Tools"

Get the Source Code
# as root
cd /root
wget https://www.python.org/ftp/python/3.4.1/Python-3.4.1.tar.xz

# uncompress
xz -d Python-3.4.1.tar.xz

#unpack

tar -xvpf Python-3.4.1.tar

cd Python-3.4.1
./configure
checking for broken mbstowcs... no
checking for --with-computed-gotos... no value specified
checking whether gcc -pthread supports computed gotos... yes
checking for build directories... done
checking for -O2... yes
checking for glibc _FORTIFY_SOURCE/memmove bug... no
checking for gcc ipa-pure-const bug... no
checking for ensurepip... upgrade
configure: creating ./config.status
config.status: creating Makefile.pre
config.status: creating Modules/Setup.config
config.status: creating Misc/python.pc
config.status: creating Misc/python-config.sh
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
creating Modules/Setup
creating Modules/Setup.local
creating Makefile

make
changing mode of build/scripts-3.4/pyvenv from 644 to 755
renaming build/scripts-3.4/pydoc3 to build/scripts-3.4/pydoc3.4
renaming build/scripts-3.4/idle3 to build/scripts-3.4/idle3.4
renaming build/scripts-3.4/2to3 to build/scripts-3.4/2to3-3.4
renaming build/scripts-3.4/pyvenv to build/scripts-3.4/pyvenv-3.4
/usr/bin/install -c -m 644 ./Tools/gdb/libpython.py python-gdb.py
gcc -pthread -c -Wno-unused-result -Werror=declaration-after-statement -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes     -I. -IInclude -I./Include    -DPy_BUILD_CORE -o Modules/_testembed.o ./Modules/_testembed.c
gcc -pthread   -Xlinker -export-dynamic -o Modules/_testembed Modules/_testembed.o libpython3.4m.a -lpthread -ldl  -lutil   -lm
# Substitution happens here, as the completely-expanded BINDIR
# is not available in configure
sed -e "s,@EXENAME@,/usr/local/bin/python3.4m," < ./Misc/python-config.in >python-config.py
# Replace makefile compat. variable references with shell script compat. ones;  ->
sed -e 's,\$(\([A-Za-z0-9_]*\)),\$\{\1\},g' < Misc/python-config.sh >python-config
# On Darwin, always use the python version of the script, the shell
# version doesn't use the compiler customizations that are provided
# in python (_osx_support.py).
if test `uname -s` = Darwin; then \
        cp python-config.py python-config; \
fi

make altinstall
changing mode of /usr/local/lib/python3.4/lib-dynload/ossaudiodev.cpython-34m.so to 755
changing mode of /usr/local/lib/python3.4/lib-dynload/xxlimited.cpython-34m.so to 755
changing mode of /usr/local/lib/python3.4/lib-dynload/_ctypes.cpython-34m.so to 755
changing mode of /usr/local/lib/python3.4/lib-dynload/ to 755
changing mode of /usr/local/lib/python3.4/lib-dynload/__pycache__ to 755
running install_scripts
copying build/scripts-3.4/pydoc3.4 -> /usr/local/bin
copying build/scripts-3.4/idle3.4 -> /usr/local/bin
copying build/scripts-3.4/2to3-3.4 -> /usr/local/bin
copying build/scripts-3.4/pyvenv-3.4 -> /usr/local/bin
changing mode of /usr/local/bin/pydoc3.4 to 755
changing mode of /usr/local/bin/idle3.4 to 755
changing mode of /usr/local/bin/2to3-3.4 to 755
changing mode of /usr/local/bin/pyvenv-3.4 to 755
rm /usr/local/lib/python3.4/lib-dynload/_sysconfigdata.py
rm -r /usr/local/lib/python3.4/lib-dynload/__pycache__
/usr/bin/install -c -m 644 ./Misc/python.man \
        /usr/local/share/man/man1/python3.4.1
if test "xupgrade" != "xno"  ; then \
        case upgrade in \
                upgrade) ensurepip="--altinstall --upgrade" ;; \
                install|*) ensurepip="--altinstall" ;; \
        esac; \
         ./python -E -m ensurepip \
                $ensurepip --root=/ ; \
fi
Ignoring ensurepip failure: pip 1.5.6 requires SSL/TLS


Where is it
[root@centos7 Python-3.4.1]# ls -l /usr/local/bin/py*
-rwxr-xr-x. 1 root root       84 Aug 11 14:24 /usr/local/bin/pydoc3.4
-rwxr-xr-x. 2 root root 11420638 Aug 11 14:23 /usr/local/bin/python3.4
-rwxr-xr-x. 2 root root 11420638 Aug 11 14:23 /usr/local/bin/python3.4m
-rwxr-xr-x. 1 root root     3068 Aug 11 14:24 /usr/local/bin/python3.4m-config
-rwxr-xr-x. 1 root root      236 Aug 11 14:24 /usr/local/bin/pyvenv-3.4






Try Running with Python 3
[root@centos7 Python-3.4.1]# /usr/local/bin/python3.4
Python 3.4.1 (default, Aug 11 2014, 14:21:23)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> f=open("/etc/yum.conf")
>>> for l in f.readlines():
...   print (l,end="")
...
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release


#  This is the default, if you make this bigger yum won't see if the metadata
# is newer on the remote and so you'll "gain" the bandwidth of not having to
# download the new metadata and "pay" for it by yum not having correct
# information.
#  It is esp. important, to have correct metadata, for distributions like
# Fedora which don't keep old packages around. If you don't like this checking
# interupting your command line usage, it's much better to have something
# manually check the metadata once an hour (yum-updatesd will do this).
# metadata_expire=90m

# PUT YOUR REPOS HERE OR IN separate files named file.repo
# in /etc/yum.repos.d


Python 3 differences


https://docs.python.org/3.0/whatsnew/3.0.html

http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/tutorials/key_differences_between_python_2_and_3.ipynb


Summary
We downloaded the Python 3 source and installed it into /usr/local/bin  (not /usr/bin).  We are now able to write and run Python 3 programs.