Python基础实例教程(微课版)
上QQ阅读APP看书,第一时间看更新

1.3 在Linux下搭建Python开发环境

Linux下安装Python

一般,Linux系统中默认有Python开发环境,但是目前Linux中自带的Python一般为Python 2.X版本。

如果想在Linux中使用Python 3.X版本,一般不建议卸载自带的Python 2.X版本,因为卸载后会遇到很多麻烦。如果目前要在Linux系统中使用Python 3.X的环境,一般建议在保留自带的Python 2.X版本的基础上,同时安装Python 3.X版本,也就是要保证两个版本共存于该Linux系统中。

这中间会涉及相应的技巧,具体以CentOS 7系统为例进行讲解。

首先,在终端中输入“python”,便可以查到当前自带的Python版本,本例中自带的版本为Python 2.7.5,如下所示:

[root@localhost weisuen]# python
Python 2.7.5 (default, Nov 20 2015, 02:00:19)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

接下来,要安装3.X版本的Python可以这样做:先从Python的官网下载Python 3.X,在此选择的版本是Python-3.4.2.tgz,所以可以按如下代码进行下载:

# wget https://www.python.org/ftp/python/3.4.2/Python-3.4.2.tgz

下载之后进行相应的解压操作:

# tar -zxvf Python-3.4.2.tgz

随后对其进行配置:

[root@localhost weisuen]# ls 
Python-3.4.2   Python-3.4.2.tgz  公共  模板   视频   图片  文档  下载   音乐   桌面 
[root@localhost weisuen]# cd Python-3.4.2/ 
[root@localhost Python-3.4.2]# ./configure --prefix=/usr/local/ python3

配置完成之后,可以进行make(编译)和make install(安装):

[root@localhost Python-3.4.2]# make 
[root@localhost Python-3.4.2]# make install

安装完成之后,为了能通过直接输入“python”调用刚刚安装的Python 3.4.2,需要建立软链接。在建立软链接之前,一般首先需要备份原来的Python,具体过程如下:

[root@localhost bin]# mv /usr/bin/python /usr/bin/python2bac 
[root@localhost bin]# ln -fs /usr/local/python3/bin/python3 /us r/bin/python

此时,输入“python”即可调用刚刚安装的Python 3.4.2,而输入“python2.7”,则可以调用系统原来的Python 2.X版本,两种Python版本都在Linux中,如下所示:

[root@localhost bin]# python
Python 3.4.2 (default, Sep 3 2016, 20:04:41)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[root@localhost bin]# python2.7
Python 2.7.5 (default, Nov 20 2015, 02:00:19)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

接下来需要配置好Python 3.X对应的pip工具,其实在Python 3.4中会默认带有pip3,所以此时,为了能在终端中输入“pip3”可以直接调用Python 3.4自带的pip3,需要为pip3建立软链接,如下所示:

[root@localhost bin]# ln -fs /usr/local/python3/bin/pip3 /usr/bin/pip3

建立好软链接之后,在终端中输入“pip3”,即可出现如下信息,说明此时在终端中输入“pip3”已经能成功调用pip3。

[root@localhost bin]# pip3

Usage:
  pip <command> [options]

Commands:
  install           Install packages.
  uninstall          Uninstall packages.
  freeze           Output installed packages in requirements format.
  list            List installed packages.
  show            Show information about installed packages.
  search           Search PyPI for packages.
  wheel            Build wheels from your requirements.
  zip             DEPRECATED. Zip individual packages.
  unzip            DEPRECATED. Unzip individual packages.
  bundle           DEPRECATED. Create pybundles.
  help            Show help for commands.
General Options:
  -h, --help         Show help.
  -v, --verbose        Give more output. Option is additive, and can be used up to 3 times.
  -V, --version        Show version and exit.
  -q, --quiet         Give less output.
  --log-file <path>      Path to a verbose non-appending log, that only logs failures. This log is active by default at /root/.pip/pip.log.
  --log <path>        Path to a verbose appending log. This log is inactive by default.
  --proxy <proxy>       Specify a proxy in the form [user:passwd@]proxy.server:port.
  --timeout <sec>       Set the socket timeout (default 15 seconds).
  --exists-action <action>  Default action when a path already exists: (s)witch, (i)gnore, (w)ipe,(b)ackup.
  --cert <path>        Path to alternate CA bundle.

通过以上的步骤,已经在Linux系统中搭建好了Python 2.X与Python 3.X共存的开发环境。但是,由于升级之后会影响某些系统的功能,所以还需要了解一下经常出现的问题及解决方案。

常见问题1:升级Python 3.X后,yum无法使用。

问题描述:

升级Python 3.X后,可能会导致yum无法使用,出现如下信息:

  File "/usr/bin/yum", line 30
     except KeyboardInterrupt, e:
                             ^
SyntaxError: invalid syntax

解决办法:

因为/usr/bin/yum文件中会调用Python,而此时调用的Python为升级后的Python 3.X,由于Python 3.X与Python 2.X有一些差异,所以可以让系统调用Python 2.X,修改一下相应的代码。

编辑文件/usr/bin/yum:

[root@localhost Python-3.4.2]# vim /usr/bin/yum 
#!/usr/bin/python 
import sys 
try: 
    import yum 
except ImportError: 
     print >> sys.stderr, """\ 
There was a problem importing one of the Python modules 
required to run yum. The error leading to this problem was: 
 
   %s 
 
Please install a package which provides this module, or 
verify that the module is installed correctly. 
 
It's possible that the above module doesn't match the 
current version of Python, which is: 
%s 
 
If you cannot solve this problem yourself, please go to 
the yum faq at: 
  http://yum.baseurl.org/wiki/Faq 
""" % (sys.exc_value, sys.version) 
    sys.exit(1) 
 
sys.path.insert(0, '/usr/share/yum-cli') 
try: 
    import yummain 
        yummain.user_main(sys.argv[1:], exit_code=True) 
except KeyboardInterrupt, e: 
     print >> sys.stderr, "\n\nExiting on user cancel." 
    sys.exit(1)

可以发现,第一行代码默认会调用Python 3.X,所以需要将第一行代码改为:

#!/usr/bin/python2.7

修改之后,保存并退出。

随后,使用yum时就不会再出现该问题。

常见问题2:升级Python后,/usr/libexec/urlgrabber-ext-down出现问题。

问题描述:

升级Python后,有时程序在用到/usr/libexec/urlgrabber-ext-down文件的时候(比如有时用yum之时),可能会出现如下所示的问题:

Downloading packages: 
Delta RPMs reduced 2.7 M of updates to 731 k (73% saved) 
  File "/usr/libexec/urlgrabber-ext-down", line 28 
    except OSError, e: 
                  ^ 
SyntaxError: invalid syntax 
  File "/usr/libexec/urlgrabber-ext-down", line 28 
    except OSError, e: 
                  ^ 
SyntaxError: invalid syntax 
  File "/usr/libexec/urlgrabber-ext-down", line 28 
    except OSError, e: 
                  ^ 
SyntaxError: invalid syntax

由于用户取消而退出。

解决办法:

这个问题的原因跟问题1的原因类似,即程序用到Python的时候无法调用Python 2.X。

所以可以修改/usr/libexec/urlgrabber-ext-down文件里面的代码,具体操作如下:

[root@localhost Python-3.4.2]# vim /usr/libexec//urlgrabber-ext-down 
#! /usr/bin/python 
#   A very simple external downloader 
#   Copyright 2011-2012 Zdenek Pavlas 
 
#    This library is free software; you can redistribute it and/or 
#    modify it under the terms of the GNU Lesser General Public 
#    License as published by the Free Software Foundation; either
#    version 2.1 of the License, or (at your option) any later version. 
…

同样,需要将第一行改为:

#! /usr/bin/python2.7

修改并保存之后,该问题即可解决。