Python3 on Debian and Raspbian repository is version 3.5. I need the latest python to run the Leon AI project.
The following is tested only on Debian 9 Stretch and Raspbian 9 Stretch.

Remove –enable-optimizations parameter is optional, it is for optimizing the python3 execution but will take a lot of time when configuring.
Change -j4 to match you CPU cores, ex. 4 cores = j4

Alternatively, you could use checkinstall to compile the program and generate a debian APT package. So, you will be able to uninstall the package.

If you use make install, you may not be able to uninstall the program files with make uninstall. Unless you will read through the makefile and remove all the files yourself.


$ sudo apt install build-essential checkinstall libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev zlib1g

$ wget -c "https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tar.xz"

$ tar xvf Python-3.7.2.tar.xz
$ cd Python-3.7.2/
$ ./configure  --enable-optimizations

$ sudo checkinstall

or
$ make -j4
$ sudo make install

Some output:


...
rm -f /usr/local/bin/idle3                                                                        
(cd /usr/local/bin; ln -s idle3.7 idle3)                                                          
rm -f /usr/local/bin/pydoc3                                                                       
(cd /usr/local/bin; ln -s pydoc3.7 pydoc3)                                                        
rm -f /usr/local/bin/2to3                                                                         
(cd /usr/local/bin; ln -s 2to3-3.7 2to3)                                                          
rm -f /usr/local/bin/pyvenv                                                                       
(cd /usr/local/bin; ln -s pyvenv-3.7 pyvenv)                                                      
if test "x" != "x" ; then \                                                                       
        rm -f /usr/local/bin/python3-32; \                                                        
        (cd /usr/local/bin; ln -s python3.7-32 python3-32) \                                      
fi                                                                                                
rm -f /usr/local/share/man/man1/python3.1                                                         
(cd /usr/local/share/man/man1; ln -s python3.7.1 python3.1)                                       
if test "xupgrade" != "xno"  ; then \                                                             
        case upgrade in \                                                                         
                upgrade) ensurepip="--upgrade" ;; \                                               
                install|*) ensurepip="" ;; \                                                      
        esac; \                                                                                   
         ./python -E -m ensurepip \                                                               
                $ensurepip --root=/ ; \                                                           
fi                                                                                                
Looking in links: /tmp/tmplxnagd_p                                                                
Collecting setuptools                                                                             
Collecting pip                                                                                    
Installing collected packages: setuptools, pip                                                    
Successfully installed pip-18.1 setuptools-40.6.2 

Testing the new python


$ python3 --version
Python 3.7.2

$ python3
Python 3.7.2 (default, Feb 16 2019, 19:43:13) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Python is cool")
Python is cool

Except where otherwise noted, this work is licensed under Creative Commons Attribution-ShareAlike 4.0 International License (http://creativecommons.org/licenses/by-sa/4.0/).
I hope that this post is useful to you, if you liked this post you may support me via liberapay or patreon. Thank you for your support.

References:
https://dzone.com/articles/install-python-370-on-ubuntu-1804debian-95
https://gist.github.com/SeppPenner/6a5a30ebc8f79936fa136c524417761d