Saturday, March 26, 2016

How-to: Installing Python 3.5.1 in Linux

When installing a new software in Linux, you could experience the following:
  • Expected
    • For example, if you have read this companion article (or watch this video), you would know that:
      • You can install multiple versions of Python on the same Linux Server (but in different PATHONHOME).[1]
      • There are differences between Python 2 and 3. So, be careful of reading articles that refer to different versions of Python installation (2 vs 3).
  • Unexpected
    • Surprises always happen even with careful planning. For example, we have run into at least two issues:
      • /usr/bin/install: cannot change permissions of `/usr/local/lib': No such file or directory
      • The directory '/home/<usrname>/.cache/pip' or its parent directory is not owned by the current user

In this article, we will cover the installation of Python 3.5.1 in Linux and how to resolve the issues encountered.

Downloads


You can click on this link to download Python 3.5.1 in Gzipped source tarball format. Go to that page and scroll down to Files section.

VersionOperating SystemDescriptionMD5 SumFile SizeGPG
Gzipped source tarballSource releasebe78e48cdfc1a7ad90efff146dce6cfe20143759SIG

If you want to download it using command lines, you can do:

$ export http_proxy=http://www-proxy.us.xxx.com:80 $ export https_proxy=http://www-proxy.us.xxx.com:80 $ wget https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz $ tar -xvf Python-3.5.1.tgz $ cd Python-3.5.1

Configure and Install (Overview)


In the Python-3.5.1 folder, there is a README file. Read the information inside and follow the steps as follows:

On Unix, Linux, BSD, OSX, and Cygwin:

$ ./configure
$ make
$ make test
$ sudo make install

This will install Python as python3. Note that only make install need to be run as a root user.

Considerations of Configuration


Before you execute the configure command, do plan in advance for your new PYTHONHOME.[1] This is especially important if you have:
  • Multiple versions of Python installed on the system, or
  • Some parts of file system are read-only

Enter ./configure --help to learn how to customize installation directories in the configuration step.

$./configure --help Installation directories: --prefix=PREFIX install architecture-independent files in PREFIX [/usr/local] --exec-prefix=EPREFIX install architecture-dependent files in EPREFIX [PREFIX] By default, `make install' will install all the files in `/usr/local/bin', `/usr/local/lib' etc. You can specify an installation prefix other than `/usr/local' using `--prefix', for instance `--prefix=$HOME'. For better control, use the options below. Fine tuning of the installation directories: --bindir=DIR user executables [EPREFIX/bin] --sbindir=DIR system admin executables [EPREFIX/sbin] --libexecdir=DIR program executables [EPREFIX/libexec] --sysconfdir=DIR read-only single-machine data [PREFIX/etc] --sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com] --localstatedir=DIR modifiable single-machine data [PREFIX/var] --libdir=DIR object code libraries [EPREFIX/lib] --includedir=DIR C header files [PREFIX/include] --oldincludedir=DIR C header files for non-gcc [/usr/include] --datarootdir=DIR read-only arch.-independent data root [PREFIX/share] --datadir=DIR read-only architecture-independent data [DATAROOTDIR] --infodir=DIR info documentation [DATAROOTDIR/info] --localedir=DIR locale-dependent data [DATAROOTDIR/locale] --mandir=DIR man documentation [DATAROOTDIR/man] --docdir=DIR documentation root [DATAROOTDIR/doc/python] --htmldir=DIR html documentation [DOCDIR] --dvidir=DIR dvi documentation [DOCDIR] --pdfdir=DIR pdf documentation [DOCDIR] --psdir=DIR ps documentation [DOCDIR]

In our system, both default installation directories /usr/local/bin and /usr/local/lib  are read-only. So, we need to configure it with different PREFIX and EPREFIX as follows:

$ ./configure --prefix=/usr --exec-prefix=/usr
creating Modules/Setup creating Modules/Setup.local creating Makefile

After resolving all installation issues, you could find out where the final installation directories are by entering:[2]

$ python3 -c 'import sys; print("\n".join(sys.path))'
/usr/lib/python35.zip /usr/lib/python3.5 /usr/lib/python3.5/plat-linux /usr/lib/python3.5/lib-dynload /home/<usrname>/.local/lib/python3.5/site-packages /usr/lib/python3.5/site-packages

Potential Issues


Without specifying PREFIX and/or EPREFIX, you might run into the following issues (read [2] for further help):

$ python3 Could not find platform independent libraries Could not find platform dependent libraries Consider setting $PYTHONHOME to [:] Fatal Python error: Py_Initialize: Unable to get the locale encoding ImportError: No module named 'encodings'


To resolve another issue as shown below, try "sudo -H make install" as suggested (note that you may want to clean up first—see next section):

$ sudo make install The directory '/home/<usrname>/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. The directory '/home/<usrname>/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.

Cleanup and Retry


In case you have run into any issues, you can cleanup your environment and retry with fixes. In your Python-3.5.1 folder, type the following:

# make clean
find . -depth -name '__pycache__' -exec rm -rf {} ';' find . -name '*.py[co]' -exec rm -f {} ';' find . -name '*.[oa]' -exec rm -f {} ';' find . -name '*.s[ol]' -exec rm -f {} ';' find . -name '*.so.[0-9]*.[0-9]*' -exec rm -f {} ';' find build -name 'fficonfig.h' -exec rm -f {} ';' || true find: build: No such file or directory find build -name '*.py' -exec rm -f {} ';' || true find: build: No such file or directory find build -name '*.py[co]' -exec rm -f {} ';' || true find: build: No such file or directory rm -f pybuilddir.txt rm -f Lib/lib2to3/*Grammar*.pickle rm -f Programs/_testembed Programs/_freeze_importlib rm -rf build

References

  1. Environment Variables (Python)
    • sys.path
      • A list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default.
  2. How-to: When a Missing Python Module Error Was Thrown
  3. What does “SyntaxError: Missing parentheses in call to 'print'” mean in Python?
    • This error message means that you are attempting to use Python 3 to follow an example or run a program that uses the Python 2 print statement:
  4. Install / Update Python 3.5.0 at Linux machine. (Youtube)
  5. Python 3.5.1
  6. Python Module
  7. upgrade Python to 2.7.2
  8. How can I troubleshoot Python “Could not find platform independent libraries
  9. Py_Initialize: Unable to get the locale encoding in OpenSuse 12.3
  10. Python script header
  11. Standard modules (Python)
  12. How do I find the location of Python module sources?
  13. sys module — System-specific parameters and functions
  14. What do the python file extensions, .pyc .pyd .pyo stand for?
  15. How do I unload (reload) a Python module?
  16. Python Packaging User Guide
  17. Purpose of #!/usr/bin/python3 (important)

2 comments:

Blogger said...

Do you need free Twitter Followers?
Did you know that you can get these AUTOMATICALLY AND ABSOLUTELY FOR FREE by using Like 4 Like?

Blogger said...

BlueHost is ultimately the best web-hosting provider with plans for all of your hosting needs.