{"id":303,"date":"2018-05-17T21:54:05","date_gmt":"2018-05-17T21:54:05","guid":{"rendered":"http:\/\/pjakey.com\/wp\/?p=303"},"modified":"2020-04-22T14:34:10","modified_gmt":"2020-04-22T14:34:10","slug":"python-3-on-centos-7","status":"publish","type":"post","link":"https:\/\/pjakey.com\/wp\/2018\/05\/17\/python-3-on-centos-7\/","title":{"rendered":"Python 3 on CentOS 7"},"content":{"rendered":"<p>Without impacting your native Python installation, you can install Python 3 along side of it using the following method.<\/p>\n<h4>Installing Python 3<\/h4>\n<p><strong>Step 1:<\/strong> If you can, take a snapshot of your VM before proceeding.<\/p>\n<p><strong>Step 2:<\/strong> Run the following commands:<\/p>\n<pre>$ sudo yum update\r\n$ sudo yum install @development\r\n$ sudo yum install -y zlib-devel openssl-devel sqlite-devel bzip2-devel xz-libs\r\n$ VER=3.6.5\r\n$ PYVER=\"Python-${VER}\"\r\n$ wget http:\/\/www.python.org\/ftp\/python\/${VER}\/${PYVER}.tar.xz\r\n$ xz -d .\/${PYVER}.tar.xz\r\n$ tar -xvf .\/${PYVER}.tar\r\n$ cd ${PYVER}\r\n$ .\/configure\r\n$ make\r\n$ sudo make altinstall<\/pre>\n<p>To see what you&#8217;ve done:<\/p>\n<pre>$ which python3\r\n\/usr\/local\/bin\/python3\r\n\r\n$ which python\r\n\/usr\/bin\/python\r\n\r\n$ which python3.6\r\n\/usr\/local\/bin\/python3.6<\/pre>\n<p>You can create symlinks to safely reference items of interest:<\/p>\n<pre>$ ls -al \/usr\/local\/bin\/\r\ntotal 24836\r\ndrwxr-xr-x.  2 root root      196 May 17 17:38 .\r\ndrwxr-xr-x. 12 root root      131 Apr 11 00:59 ..\r\n-rwxr-xr-x.  1 root root      101 May 17 16:38 2to3-3.6\r\n-rwxr-xr-x.  1 root root      242 May 17 16:38 easy_install-3.6\r\n-rwxr-xr-x.  1 root root       99 May 17 16:38 idle3.6\r\nlrwxrwxrwx.  1 root root       21 May 17 17:38 pip3 -&gt; \/usr\/local\/bin\/pip3.6\r\n-rwxr-xr-x.  1 root root      214 May 17 16:38 pip3.6\r\n-rwxr-xr-x.  1 root root       84 May 17 16:38 pydoc3.6\r\nlrwxrwxrwx.  1 root root       24 May 17 17:31 python3 -&gt; \/usr\/local\/bin\/python3.6\r\n-rwxr-xr-x.  2 root root 12699000 May 17 16:38 python3.6\r\n-rwxr-xr-x.  2 root root 12699000 May 17 16:38 python3.6m\r\n-rwxr-xr-x.  1 root root     3117 May 17 16:38 python3.6m-config\r\n-rwxr-xr-x.  1 root root      441 May 17 16:38 pyvenv-3.6<\/pre>\n<hr \/>\n<h4>Python 3 and pip<\/h4>\n<p>Issues like missing modules may arise.\u00a0 They&#8217;ll look something like the following:<\/p>\n<pre>$ python3 .\/main.py \r\nTraceback (most recent call last):\r\nFile \".\/main.py\", line 8, in &lt;module&gt;\r\nfrom lxml.etree import ET\r\nModuleNotFoundError: No module named 'lxml'<\/pre>\n<p>Can <code>pip<\/code> help us?<\/p>\n<pre>$ which pip3\r\n\/usr\/local\/bin\/pip3<\/pre>\n<p>Looks like I have a <code>pip3<\/code>.\u00a0 Let&#8217;s try&#8230;<\/p>\n<pre>$ pip3 install lxml\r\n-bash: \/usr\/local\/bin\/pip3: \/usr\/local\/bin\/python3.6: bad interpreter: No such file or directory<\/pre>\n<p>Hmmm&#8230;that does NOT look good.\u00a0 Do I have <code>python3.6<\/code>?<\/p>\n<pre>$ which python3.6\r\n\/usr\/bin\/python3.6<\/pre>\n<p>Sure do, but it&#8217;s in a location that <code>pip3<\/code> is paying no attention to.\u00a0 As you can see from the output, <code>pip3<\/code> is unusable.\u00a0 Just running <code>pip3<\/code> results in bad interpreter: No such file or directory.\u00a0 We need to fix <code>pip<\/code> or fix the part where it&#8217;s expecting <code>python3.6<\/code> to be in <code>\/usr\/local\/bin\/python3.6<code><\/code><\/code><\/p>\n<p>Easiest thing to try is to update the operating system&#8217;s hash table where it stores its mappings of programs to their locations so it doesn&#8217;t have to search every time.\u00a0 Sometimes those mappings can get out of sync, especially if a location of a program changes.<\/p>\n<pre>$ hash\r\nhits command\r\n1 \/usr\/bin\/sudo\r\n2 \/usr\/local\/bin\/pip3\r\n2 \/usr\/bin\/python3\r\n2 \/usr\/bin\/ls\r\n$ hash -r\r\n$ hash\r\nhash: hash table empty<\/pre>\n<p>Maybe that will help <code>pip3<\/code> find what it needs?\u00a0 Um, no.\u00a0 Not one bit.<\/p>\n<pre>$ pip3 install lxml\r\n-bash: \/usr\/local\/bin\/pip3: \/usr\/local\/bin\/python3.6: bad interpreter: No such file or directory<\/pre>\n<p>Why is <code>pip3<\/code> expecting python3.6 to be in that location when it clearly isn&#8217;t?<\/p>\n<figure id=\"attachment_642\" aria-describedby=\"caption-attachment-642\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/pjakey.com\/wp\/2018\/05\/17\/python-3-on-centos-7\/missing_symlinks\/\" rel=\"attachment wp-att-642\"><img decoding=\"async\" class=\"lazyload wp-image-642 size-full\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%27http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%27%20width%3D%27600%27%20height%3D%27400%27%20viewBox%3D%270%200%20600%20400%27%3E%3Crect%20width%3D%27600%27%20height%3D%27400%27%20fill-opacity%3D%220%22%2F%3E%3C%2Fsvg%3E\" data-orig-src=\"http:\/\/pjakey.com\/wp\/wp-content\/uploads\/2018\/05\/missing_symlinks.gif\" alt=\"directory listing showing broken symlink\" width=\"600\" height=\"400\" \/><\/a><figcaption id=\"caption-attachment-642\" class=\"wp-caption-text\">When symlinks are broken they blink.<\/figcaption><\/figure>\n<p>After fixing my symlink (not because it would help with my current issue but because it&#8217;s good to fix things), I returned my focus to <code>pip3<\/code>.\u00a0 I would like to uninstall and reinstall it.\u00a0 If it is being managed by <code>yum<\/code>, it can easily be uninstalled by calling <code>yum remove<\/code>.<\/p>\n<pre>$ sudo yum remove python3-pip\r\nLoaded plugins: fastestmirror\r\nNo Match for argument: python3-pip\r\nNo Packages marked for removal<\/pre>\n<p>But, it isn&#8217;t.\u00a0 How did <code>pip3<\/code> get on this machine?\u00a0 Can <code>python3<\/code> run <code>pip<\/code> as a module?<\/p>\n<pre>$ python3 -m pip\r\n\/usr\/local\/bin\/python3: No module named pip<\/pre>\n<pre> -m module-name\r\n       Searches sys.path for the named module and runs the corresponding .py file as a script.<\/pre>\n<p>Nope.\u00a0 Okay, enough spinning my wheels.\u00a0 The setup I have isn&#8217;t making much sense.\u00a0 How is this supposed to happen?\u00a0 How is <code>pip<\/code> supposed to be installed?\u00a0 Maybe I can do what&#8217;s supposed to be done (<a href=\"https:\/\/cjsawer.whitewillow.co.uk\/2018\/08\/29\/epel-python-3-6-with-pip-on-centos-7\/\">or by trying something else somebody else claims to have worked for them<\/a>) and things will just work?<\/p>\n<pre>$ python3 -m ensurepip --user\r\nLooking in links: \/tmp\/tmp41dea8fo\r\nRequirement already satisfied: setuptools in \/usr\/lib\/python3.6\/site-packages (39.2.0)\r\nCollecting pip\r\nInstalling collected packages: pip\r\nSuccessfully installed pip-18.1\r\n\r\n$ python3 -m pip\r\n\r\nUsage: \r\n \/usr\/local\/bin\/python3 -m pip &lt;command&gt; [options]\r\n\r\nCommands:\r\n install                     Install packages.\r\n download                    Download packages.\r\n uninstall                   Uninstall packages.\r\n freeze                      Output installed packages in requirements format.\r\n list                        List installed packages.\r\n show                        Show information about installed packages.\r\n check                       Verify installed packages have compatible dependencies.\r\n config                      Manage local and global configuration.\r\n search                      Search PyPI for packages.\r\n wheel                       Build wheels from your requirements.\r\n hash                        Compute hashes of package archives.\r\n completion                  A helper command used for command completion.\r\n help                        Show help for commands.\r\n\r\nGeneral Options:\r\n -h, --help                  Show help.\r\n --isolated                  Run pip in an isolated mode, ignoring environment variables and user configuration.\r\n -v, --verbose               Give more output. Option is additive, and can be used up to 3 times.\r\n -V, --version               Show version and exit.\r\n -q, --quiet                 Give less output. Option is additive, and can be used up to 3 times (corresponding to\r\n                             WARNING, ERROR, and CRITICAL logging levels).\r\n--log &lt;path&gt;                 Path to a verbose appending log.\r\n--proxy &lt;proxy&gt;              Specify a proxy in the form [user:passwd@]proxy.server:port.\r\n--retries &lt;retries&gt;          Maximum number of retries each connection should attempt (default 5 times).\r\n--timeout &lt;sec&gt;              Set the socket timeout (default 15 seconds).\r\n--exists-action &lt;action&gt;     Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort).\r\n--trusted-host &lt;hostname&gt;    Mark this host as trusted, even though it does not have valid or any HTTPS.\r\n--cert &lt;path&gt;                Path to alternate CA bundle.\r\n--client-cert &lt;path&gt;         Path to SSL client certificate, a single file containing the private key and the\r\n                             certificate in PEM format.\r\n--cache-dir &lt;dir&gt;            Store the cache data in &lt;dir&gt;.\r\n--no-cache-dir               Disable the cache.\r\n--disable-pip-version-check\r\n                             Don't periodically check PyPI to determine whether a new version of pip is available for\r\n                             download. Implied with --no-index.\r\n--no-color                   Suppress colored output<\/pre>\n<p>Yep!<\/p>\n<p>Can I do what I had originally set out to do?<\/p>\n<pre>$ python3 -m pip install --user lxml\r\nCollecting lxml\r\nUsing cached https:\/\/files.pythonhosted.org\/packages\/dd\/ba\/a0e6866057fc0bbd17192925c1d63a3b85cf522965de9bc02364d08e5b84\/lxml-4.5.0-cp36-cp36m-manylinux1_x86_64.whl\r\nInstalling collected packages: lxml\r\nSuccessfully installed lxml-4.5.0<\/pre>\n<p>Yep!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Without impacting your native Python installation, you can install Python 3 along side of it using the following method. Installing Python 3 Step 1: If you can, take a snapshot of your VM before proceeding. Step 2: Run the following commands: $ sudo yum update $ sudo yum install @development $ sudo yum install -y<span class=\"excerpt-ellipsis\">&#8230;<\/span><\/p>\n<p><a class=\"more-link\" href=\"https:\/\/pjakey.com\/wp\/2018\/05\/17\/python-3-on-centos-7\/\" itemprop=\"url\">Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25,19,10],"tags":[],"class_list":["post-303","post","type-post","status-publish","format-standard","hentry","category-python","category-snippets","category-sysadmin"],"_links":{"self":[{"href":"https:\/\/pjakey.com\/wp\/wp-json\/wp\/v2\/posts\/303","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pjakey.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pjakey.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pjakey.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pjakey.com\/wp\/wp-json\/wp\/v2\/comments?post=303"}],"version-history":[{"count":2,"href":"https:\/\/pjakey.com\/wp\/wp-json\/wp\/v2\/posts\/303\/revisions"}],"predecessor-version":[{"id":712,"href":"https:\/\/pjakey.com\/wp\/wp-json\/wp\/v2\/posts\/303\/revisions\/712"}],"wp:attachment":[{"href":"https:\/\/pjakey.com\/wp\/wp-json\/wp\/v2\/media?parent=303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pjakey.com\/wp\/wp-json\/wp\/v2\/categories?post=303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pjakey.com\/wp\/wp-json\/wp\/v2\/tags?post=303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}