第一章: Python 之 第一個程序

在linux環境上安裝python3.6.2程序.

到官網下載源碼包安裝:

一、Python 之 第一個Python程序 - ibm.chick - MingKang.Zhou

安裝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就可正常運行。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章