文章目录

xx@ada:~$ python —version
Python 2.7.8
xx@ada:~$ python3.4 —version
Python 3.4.2
xx@ada:~$ python2.7 —version
Python 2.7.8
xx@ada:~$ which python
/usr/bin/python
xx@ada:~$ which python3.4
/usr/bin/python3.4
xx@ada:~$

现在python官网出了python2.7系列的python2.7.9, 好了, 现在我们尝试着安装python2.7.9

python在ubuntu下有几种安装方法:

通过ubuntu官方的apt工具包安装
通过PPA(Personal Package Archive) 的apt工具包安装
通过编译python源代码安装

通过ubuntu官方的apt工具包安装

sudo apt-get install python2.7
sudo apt-get install python3.4

安装完成后, 可以用下面的命令进行确认

xx@ada:~$ python2.7 —version
Python 2.7.8
xx@ada:~$ python3.4 —version
Python 3.4.2
xx@ada:~$

从PPA(Personal Package Archives) 安装apt工具包

$ sudo apt-get install python-software-properties
$ sudo add-apt-repository ppa:fkrull/deadsnakes
$ sudo apt-get update
$ sudo apt-get install python2.7

类似使用apt工具包安装python的工具虽然简单, 但有时不一定能够安装到最新版本。因此, 在python出现重要更新时, 我们最好学会以从源代码直接编译安装python2.7.

从源代码编译安装python

$ wget -c https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
$ tar -xzvf Python-2.7.9.tgz
$ cd Python-2.7.9/
$ LDFLAGS=”-L/usr/lib/x86_64-linux-gnu” ./configure
$ make
$ sudo make install

其中, 上面的wget -c (url)是下载命令,参数-c表示支持断点下载, url是目标文件下载的绝对路径 

“-L/usr/lib/x86_64-linux-gnu”中的x86_64-linux-gnu在/usr/lib/下可以找到, 这是x86_64可以看出我的系统是64的, 这里根据自己的系统进行键入。

好了, 安装完后我们检测下, 终端键入python —version, 回车, 再键入which python

xx@ada:~$ python —version
Python 2.7.9
xx@ada:~$ which python
/usr/local/bin/python
xx@ada:~$

可见, python2.7.9安装成功, 并且发现我们默认的python版本变成了python2.7.9。这是因为操作系统在搜索命令时, 是按照PATH环境变量的顺序依次进行搜索的, 

/usr/local/bin/下的python会比/usr/bin/下的python优先搜索到, 并作为默认的python版本。

那么我ubuntu14.10下就有三个版本的python,分别是python2.7.8, python2.7.9, python3.4.2, 如下:

xx@ada:~$ python —version
Python 2.7.9
xx@ada:~$ python2.7 —version
Python 2.7.9
xx@ada:~$ python3.4 —version
Python 3.4.2
xx@ada:~$ python2.7
Python 2.7.9 (default, Jan 3 2015, 03:27:08)
[GCC 4.9.1] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.

exit()
xx@ada:~$

当然, 我们也可以指定python的路径, 为查看python的版本, 如下:

xx@ada:~$ /usr/bin/python —version
Python 2.7.8
xx@ada:~$ /usr/bin/python2.7 —version
Python 2.7.8
xx@ada:~$ /usr/bin/python3.4 —version
Python 3.4.2
xx@ada:~$ /usr/local/bin/python —version
Python 2.7.9
xx@ada:~$ /usr/local/bin/python2.7 —version
Python 2.7.9
xx@ada:~$

至此, 我们就已经介绍完了python在ubuntu下的三种安装方法。

OK, Enjoy it!!!

继上, 由于我在安装python2.7.9之前安装过了虚拟运行环境virtualenv和virtualenvwrapper, 当安装完了python2.7.9后, 默认的python版本发生了变化, 所以当我再次打开终端时出现以下信息:

/usr/local/bin/python: No module named virtualenvwrapper
virtualenvwrapper.sh: There was a problem running the initialization hooks.

If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python and that PATH is
set properly.
xx@ada:~$

由上面的信息我们可以知道新安装的python2.7.9安装目录下找不到模块virtualenvwrapper, 但她们依然可用, 不妨我们在终端下试下以下几个命令

$ virtualenv —help

$ mkivrtulaenv —help

$ workon

/usr/local/bin/python: No module named virtualenvwrapper
virtualenvwrapper.sh: There was a problem running the initialization hooks.

If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python and that PATH is
set properly.
xx@ada:~$ workon
env
xx@ada:~$ workon env
/usr/local/bin/python: No module named virtualenvwrapper
/usr/local/bin/python: No module named virtualenvwrapper
(env)xx@ada:~$ deactivate
/usr/local/bin/python: No module named virtualenvwrapper
/usr/local/bin/python: No module named virtualenvwrapper
xx@ada:~$

可见,这不影响虚拟运行环境。但每次打开终端都看到那些信息, 有强迫证的我目前正在努力寻找解决方法…

我们键入pip freeze来看看已安装的python模块:

xx@ada:~$ pip freeze
adium-theme-ubuntu==0.3.4
apt-xapian-index==0.46
ccsm==0.9.12
characteristic==0.1.0
chardet==2.2.1
colorama==0.3.1
command-not-found==0.3
compizconfig-python==0.9.12
debtagshw==0.1
defer==1.0.6
dirspec==13.10
duplicity==0.6.23
gevent==1.0.1
greenlet==0.4.5
html5lib==0.999
httplib2==0.9
idna==0.9
lockfile==0.8
lxml==3.3.6
oauthlib==0.6.1
oneconf==0.3.7
PAM==0.4.2
pbr==0.10.7
pexpect==3.2
Pillow==2.6.1
piston-mini-client==0.7.5
pyasn1==0.1.7
pyasn1-modules==0.0.5
pycrypto==2.6.1
pycups==1.9.67
pycurl==7.19.5
pygobject==3.14.0
pyOpenSSL==0.13.1
pyserial==2.6
python-apt===0.9.3.10ubuntu1
python-debian==0.1.22
pyxdg==0.25
reportlab==3.1.8
requests==2.3.0
service-identity==1.0.0
sessioninstaller==0.0.0
six==1.7.3
software-center-aptd-plugins==0.0.0
stevedore==1.1.0
system-service==0.1.6
Twisted-Core==14.0.2
Twisted-Web==14.0.2
unity-lens-photos==1.0
urllib3==1.8.3
virtualenv==12.0.4
virtualenv-clone==0.2.5
virtualenvwrapper==4.3.1
wheel==0.24.0
xdiagnose==3.6.6
zope.interface==4.1.1
xx@ada:~$

好了,到了这这里,你的python2.7.9已经安装在64位的ubuntu14.10系统上了,是不是感觉比想象中简单。

文章目录