Python基礎01-Python環境搭建與HelloWorld

目錄

從今天開始學習Python

Python環境搭建

安裝gcc

Python源碼包安裝

開始Python第一個代碼HelloWorld!


從今天開始學習Python

爲啥選擇Python,可能是跟隨潮流吧。我現在不知道爲什麼學習Python,但是可能一年到一年半以後,我會感激今天學習Python的自己。

Python環境搭建

作爲一名資深的“開發+運維+打雜+背鍋”,學習一門新語言,就不從基礎的介紹開始了,直接從安裝Python環境開始。Python有2和3兩個版本。我所使用的CentOS7默認是Python2.7.5版本,那麼我就先編譯安裝一個Python3。

安裝gcc

安裝gcc 4.8.5版本。

[root@test13:~]# yum install gcc*
[root@test13:~]# gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Python源碼包安裝

查看CentOS7默認的Python版本是2.7.5。

[root@test13:~]# python -V
Python 2.7.5

下載當前穩定版本Python 3.5.9源碼包。

[root@test13:~]# wget https://www.python.org/ftp/python/3.5.9/Python-3.5.9.tgz

解壓縮並進入源碼包。

[root@test13:~]# tar -xvzf Python-3.5.9.tgz
[root@test13:~]# cd Python-3.5.9/

編譯安裝。指定安裝位置爲/usr/local/python359。

[root@test13:Python-3.5.9]# ./configure --prefix=/usr/local/python359
[root@test13:Python-3.5.9]# make all
[root@test13:Python-3.5.9]# make install

檢查安裝的Python 3.5.9版本。

[root@test13:~]# /usr/local/python359/bin/python3 -V
Python 3.5.9

創建新的軟連接3ython3到Python 3.5.9。以後python就是執行python2.7.5,python3就是執行python3.5.9。

[root@test13:~]# ll /usr/bin/python*
lrwxrwxrwx. 1 root root    7 Nov 16  2018 /usr/bin/python -> python2
lrwxrwxrwx. 1 root root    9 Nov 16  2018 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7.1K Jul 13  2018 /usr/bin/python2.7
[root@test13:bin]# ln -s /usr/local/python359/bin/python3 /usr/bin/python3

驗證當前Python版本和Python3版本。

[root@test13:~]# python -V
Python 2.7.5
[root@test13:~]# python3 -V
Python 3.5.9

開始Python第一個代碼HelloWorld!

print("hello, world!")
[lingmk@test13:~]$ vim hello.py
[lingmk@test13:~]$ python3 hello.py 
hello, world!

 

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