第一章: Python 之 第一个程序,,在linux环境上安


在linux环境上安装python3.6.2程序.

到官网下载源码包安装:

技术分享

安装python3.6.2的系统依赖包:

yun -y install zlib*

解压安装包:

tar -xvf Python-3.6.2.tar

进入安装目录:

cd Python-3.6.2

添加配置,指定安装位置:

./configure --prefix=/usr/local/python

编译源码并执行安装:

make && make install

到安装路径下验证安装是否成功:

[root@localhost bin]# pwd

/usr/local/python/bin

[root@localhost bin]# ./python3.6

Python 3.6.2 (default, Sep 8 2017, 11:30:01)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> print("hello world")

hello world

系统默认python版本为2.6(与python3.0以上不兼容)

[root@localhost ~]# which python

/usr/bin/python

[root@localhost ~]# python

Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

将系统默认的python2.6移除,以方便后继所有python程序,默认使用python3

[root@localhost bin]# which python

/usr/bin/python

[root@localhost bin]# mv /usr/bin/python /usr/bin/python.bak

[root@localhost bin]# ln -s /usr/local/python/bin/python3.6 /usr/bin/python

[root@localhost bin]# python

Python 3.6.2 (default, Sep 8 2017, 11:30:01) #此时系统python已经替换为3.6

[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux

Type "help", "copyright", "credits" or "license" for more information.

系统有两个python版本,2.6和3.6,分别写一个hello world 的脚本。

python2.6:

#!/usr/bin/python2.6

print ‘hello world!‘

python3.6:

#!/usr/bin/python

print ‘hello world!‘

分别成功执行,如下:

[root@localhost py]# ./py2.6-hello.py

hello world

[root@localhost py]# ./py3.6-hello.py

File "./py3.6-hello.py", line 2

print ‘hello world!‘

^

SyntaxError: Missing parentheses in call to ‘print‘

因为py3.6-hello.py是一个python2的语法,此时我们只需要将头部声音改为python2.6就可正常运行。


本文出自 “学习旅程” 博客,请务必保留此出处http://mingkang.blog.51cto.com/9678221/1964117

第一章: Python 之 第一个程序

评论关闭