第一章: Python 之 第一個程序
在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
到安裝路徑下驗證安裝是否成功:
[[email protected] bin]# pwd
/usr/local/python/bin
[[email protected] 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以上不兼容)
[[email protected] ~]# which python
/usr/bin/python
[[email protected] ~]# 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
[[email protected] bin]# which python
/usr/bin/python
[[email protected] bin]# mv /usr/bin/python /usr/bin/python.bak
[[email protected] bin]# ln -s /usr/local/python/bin/python3.6 /usr/bin/python
[[email protected] 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!‘
分別成功執行,如下:
[[email protected] py]# ./py2.6-hello.py
hello world
[[email protected] 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 之 第一個程序