![python](https://wx4.sinaimg.cn/large/005A3Hwygy1fh7toba397j307y0a40ts.jpg)
一般情况下,Linux都会预装 Python了,但是这个预装的python版本一般都非常低,很多 Python的新特性都没有,必须重新安装新一点的版本 。
系统默认在/usr/bin/下安装了python2.6.6 版本,在任意目录下输入python 查看当前版本信息。
![](https://wx1.sinaimg.cn/large/005A3Hwygy1fh7o09x3elj30eo05idfx.jpg)
官网下载地址:http://www.python.org/download/
然后cd /opt/softwares
![](https://wx3.sinaimg.cn/large/005A3Hwygy1fh7o4upnquj30mf0ac75z.jpg)
tar -xzf Python-2.7.4.tgz
![](https://wx3.sinaimg.cn/large/005A3Hwygy1fh7o79u22kj30k60313yu.jpg)
接着,
在Python-2.7.3 根目录下运行
./configure
这里我们补充:
补:configure 命令执行完之后,会生成一个 Makefile 文件,这个 Makefile主要是被下一步的 make 命令所使用。打开 Makefile你就会发现,里边制定了构建的顺序, Linux 需要按照Makefile 所指定的顺序来构建 (build) 程序组件。
然后我们发现报错
configure: error: no acceptable C compiler found in $PATH
![](https://wx4.sinaimg.cn/large/005A3Hwygy1fh7pfvokjgj30eh032gll.jpg)
看到gcc没有,小意思,那么我们。
yum install gcc
![](https://wx1.sinaimg.cn/large/005A3Hwygy1fh7phnxlckj30p70d7gms.jpg)
ok了
![](https://wx2.sinaimg.cn/large/005A3Hwygy1fh7pkbit4zj30oz0f7abl.jpg)
然后我们在./configure
![](https://wx2.sinaimg.cn/large/005A3Hwygy1fh7pld55kfj30mr0czmyi.jpg)
这回有了吧。
![](https://wx3.sinaimg.cn/large/005A3Hwygy1fh7plvfi9tj30gy06sdge.jpg)
然后我们直接
make
make实际上编译你的源代码,并生成执行文件。
make之后控制台大量输出:
![](https://wx3.sinaimg.cn/large/005A3Hwygy1fh7po9f3c9j30pj0ewtb0.jpg)
make install
make install实际上是把生成的执行文件拷贝到 linux系统中必要的目录下,比如拷贝到 /usr/local/bin 目录下,这样所有 user就都能运行这个程序了。
![](https://wx1.sinaimg.cn/large/005A3Hwygy1fh7ppzlwzxj30n90armzb.jpg)
安装完之后,我们执行python命令,你会发现默认的 python命令仍旧是指向旧的版本 2.6.6
![](https://wx2.sinaimg.cn/large/005A3Hwygy1fh7prdypk1j30f302zt8q.jpg)
[root@hadoop-senior0 Python-2.7.4]# which python
/usr/local/bin/python
![](https://wx1.sinaimg.cn/large/005A3Hwygy1fh7psyn0rwj30aq01fdfo.jpg)
实际上我们新安装的 python也在这个目录下。
[root@hadoop-senior0 bin]# ls python*
![](https://wx3.sinaimg.cn/large/005A3Hwygy1fh7puiabr1j30g601i746.jpg)
在命令行输入python2.7,这样就会执行到我们新的 python版本了,已经变为2.7.4了
![](https://wx4.sinaimg.cn/large/005A3Hwygy1fh7pvdpj9ej30eq02it8p.jpg)
接下来需要配置环境变量。
然后我们执行命令:
PATH=$PATH:<Python 安装路径>
这个命令是说将制定的Python路径添加到PATH中
再执行echo $PATH
查看新的Python 路径已经加入到PATH中
![](https://wx4.sinaimg.cn/large/005A3Hwygy1fh7sjiq1ypj30s805uwf3.jpg)
2.7.4的python没有问题。
但是,在shell里面改,只是做了临时更改啊,一重启新添加的环境变量就会消失,要把配置写入配置文件啊。vi ~/.bashrc 在其中加入
export PATH=$PATH:/usr/local/bin/python2.7
![](https://wx4.sinaimg.cn/large/005A3Hwygy1fh7snk2isxj30dt062glp.jpg)
然后我们:wq!
保存文件,注销再登录,变量生效。
该方式添加的变量只对当前用户有效。
重启一下试试。并且我们声明,这种方式是当前用户下有效哦,如果想要系统变量,慎重一点,可以修改这个 .profile
那我们就来试一次啊,反正是测试集群。
cd /opt/
vim profile
export PATH=$PATH:/usr/local/bin/python2.7
:wq!
![](https://wx3.sinaimg.cn/large/005A3Hwygy1fh7t0zx5gnj30mu0eqmy5.jpg)
该文件编辑保存后,重启系统,变量生效。
该方式添加的变量对所有的用户都有效。
意思就是说,这两个环境变量文件,一个是系统的,另一个是当前用户的。
对了, 下面这个有高亮是因为使用的是vim,区别于vi在于有高亮提示,比较好看。可以yum install vim自己安装一下。类似记事本和notepad的关联。
这时候,咱们重启一下系统呗。
![](https://wx2.sinaimg.cn/large/005A3Hwygy1fh7t3ro5o5j30kc0bujrg.jpg)
重启之后,可以看到,我用了两个用户都是2.7.4,那么,python安装完成。
![](https://wx1.sinaimg.cn/large/005A3Hwygy1fh7t7ee3ebj30h306jq3b.jpg)
那接下来,咱们老规矩跑一个hello world,就结束本篇
print "Hello World"
![](https://wx4.sinaimg.cn/large/005A3Hwygy1fh7tcw0xhnj307302bq2s.jpg)
程序也可以像下面一样放置在一个文件中,就像我们之前所写脚本那样,然后执行。
![](https://wx3.sinaimg.cn/large/005A3Hwygy1fh7tfw4q8sj30dx02cwef.jpg)
cd /file/python/
python hello.py
![](https://wx3.sinaimg.cn/large/005A3Hwygy1fh7tgc4o9lj30b2035t8o.jpg)
Python源代码文件使用.py后缀.'#'表示注释(到行末结束)
ok,python开发吧。
![python](https://wx4.sinaimg.cn/large/005A3Hwygy1fh7toba397j307y0a40ts.jpg)
一般情况下,Linux都会预装 Python了,但是这个预装的python版本一般都非常低,很多 Python的新特性都没有,必须重新安装新一点的版本 。
系统默认在/usr/bin/下安装了python2.6.6 版本,在任意目录下输入python 查看当前版本信息。
![](https://wx1.sinaimg.cn/large/005A3Hwygy1fh7o09x3elj30eo05idfx.jpg)
官网下载地址:http://www.python.org/download/
然后cd /opt/softwares
![](https://wx3.sinaimg.cn/large/005A3Hwygy1fh7o4upnquj30mf0ac75z.jpg)
tar -xzf Python-2.7.4.tgz
![](https://wx3.sinaimg.cn/large/005A3Hwygy1fh7o79u22kj30k60313yu.jpg)
接着,
在Python-2.7.3 根目录下运行
./configure
这里我们补充:
补:configure 命令执行完之后,会生成一个 Makefile 文件,这个 Makefile主要是被下一步的 make 命令所使用。打开 Makefile你就会发现,里边制定了构建的顺序, Linux 需要按照Makefile 所指定的顺序来构建 (build) 程序组件。
然后我们发现报错
configure: error: no acceptable C compiler found in $PATH
![](https://wx4.sinaimg.cn/large/005A3Hwygy1fh7pfvokjgj30eh032gll.jpg)
看到gcc没有,小意思,那么我们。
yum install gcc
![](https://wx1.sinaimg.cn/large/005A3Hwygy1fh7phnxlckj30p70d7gms.jpg)
ok了
![](https://wx2.sinaimg.cn/large/005A3Hwygy1fh7pkbit4zj30oz0f7abl.jpg)
然后我们在./configure
![](https://wx2.sinaimg.cn/large/005A3Hwygy1fh7pld55kfj30mr0czmyi.jpg)
这回有了吧。
![](https://wx3.sinaimg.cn/large/005A3Hwygy1fh7plvfi9tj30gy06sdge.jpg)
然后我们直接
make
make实际上编译你的源代码,并生成执行文件。
make之后控制台大量输出:
![](https://wx3.sinaimg.cn/large/005A3Hwygy1fh7po9f3c9j30pj0ewtb0.jpg)
make install
make install实际上是把生成的执行文件拷贝到 linux系统中必要的目录下,比如拷贝到 /usr/local/bin 目录下,这样所有 user就都能运行这个程序了。
![](https://wx1.sinaimg.cn/large/005A3Hwygy1fh7ppzlwzxj30n90armzb.jpg)
安装完之后,我们执行python命令,你会发现默认的 python命令仍旧是指向旧的版本 2.6.6
![](https://wx2.sinaimg.cn/large/005A3Hwygy1fh7prdypk1j30f302zt8q.jpg)
[root@hadoop-senior0 Python-2.7.4]# which python
/usr/local/bin/python
![](https://wx1.sinaimg.cn/large/005A3Hwygy1fh7psyn0rwj30aq01fdfo.jpg)
实际上我们新安装的 python也在这个目录下。
[root@hadoop-senior0 bin]# ls python*
![](https://wx3.sinaimg.cn/large/005A3Hwygy1fh7puiabr1j30g601i746.jpg)
在命令行输入python2.7,这样就会执行到我们新的 python版本了,已经变为2.7.4了
![](https://wx4.sinaimg.cn/large/005A3Hwygy1fh7pvdpj9ej30eq02it8p.jpg)
接下来需要配置环境变量。
然后我们执行命令:
PATH=$PATH:<Python 安装路径>
这个命令是说将制定的Python路径添加到PATH中
再执行echo $PATH
查看新的Python 路径已经加入到PATH中
![](https://wx4.sinaimg.cn/large/005A3Hwygy1fh7sjiq1ypj30s805uwf3.jpg)
2.7.4的python没有问题。
但是,在shell里面改,只是做了临时更改啊,一重启新添加的环境变量就会消失,要把配置写入配置文件啊。vi ~/.bashrc 在其中加入
export PATH=$PATH:/usr/local/bin/python2.7
![](https://wx4.sinaimg.cn/large/005A3Hwygy1fh7snk2isxj30dt062glp.jpg)
然后我们:wq!
保存文件,注销再登录,变量生效。
该方式添加的变量只对当前用户有效。
重启一下试试。并且我们声明,这种方式是当前用户下有效哦,如果想要系统变量,慎重一点,可以修改这个 .profile
那我们就来试一次啊,反正是测试集群。
cd /opt/
vim profile
export PATH=$PATH:/usr/local/bin/python2.7
:wq!
![](https://wx3.sinaimg.cn/large/005A3Hwygy1fh7t0zx5gnj30mu0eqmy5.jpg)
该文件编辑保存后,重启系统,变量生效。
该方式添加的变量对所有的用户都有效。
意思就是说,这两个环境变量文件,一个是系统的,另一个是当前用户的。
对了, 下面这个有高亮是因为使用的是vim,区别于vi在于有高亮提示,比较好看。可以yum install vim自己安装一下。类似记事本和notepad的关联。
这时候,咱们重启一下系统呗。
![](https://wx2.sinaimg.cn/large/005A3Hwygy1fh7t3ro5o5j30kc0bujrg.jpg)
重启之后,可以看到,我用了两个用户都是2.7.4,那么,python安装完成。
![](https://wx1.sinaimg.cn/large/005A3Hwygy1fh7t7ee3ebj30h306jq3b.jpg)
那接下来,咱们老规矩跑一个hello world,就结束本篇
print "Hello World"
![](https://wx4.sinaimg.cn/large/005A3Hwygy1fh7tcw0xhnj307302bq2s.jpg)
程序也可以像下面一样放置在一个文件中,就像我们之前所写脚本那样,然后执行。
![](https://wx3.sinaimg.cn/large/005A3Hwygy1fh7tfw4q8sj30dx02cwef.jpg)
cd /file/python/
python hello.py
![](https://wx3.sinaimg.cn/large/005A3Hwygy1fh7tgc4o9lj30b2035t8o.jpg)
Python源代码文件使用.py后缀.'#'表示注释(到行末结束)
ok,python开发吧。