Linux – 安裝 Python & pip

Loading

測試環境為 CentOS 8 x86_64 (虛擬機)

安裝 Python (與 pip , Python 套件管理程式)

[root@localhost ~]# yum install python
Last metadata expiration check: 0:11:09 ago on Tue May 10 23:09:33 2022.
No match for argument: python
There are following alternatives for "python": python2, python36, python38, python39
Error: Unable to find a match: python

Python

需指定安裝的 Python 版本.

[root@localhost ~]# yum -y install python39
Last metadata expiration check: 0:11:20 ago on Tue May 10 23:09:33 2022.
Dependencies resolved.
=================================================================================================================
 Package                         Architecture Version                                      Repository       Size
=================================================================================================================
Installing:
 python39                        x86_64       3.9.7-1.module_el8.6.0+930+10acc06f          appstream        33 k
Installing dependencies:
 python39-libs                   x86_64       3.9.7-1.module_el8.6.0+930+10acc06f          appstream       8.2 M
 python39-pip-wheel              noarch       20.2.4-6.module_el8.6.0+930+10acc06f         appstream       1.3 M
 python39-setuptools-wheel       noarch       50.3.2-4.module_el8.6.0+930+10acc06f         appstream       497 k
Installing weak dependencies:
 python39-pip                    noarch       20.2.4-6.module_el8.6.0+930+10acc06f         appstream       2.0 M
 python39-setuptools             noarch       50.3.2-4.module_el8.6.0+930+10acc06f         appstream       871 k
Enabling module streams:
 python39                                     3.9                                                               

Transaction Summary
=================================================================================================================
Install  6 Packages

檢視一下 python 版本.

[root@localhost ~]# python3 --version
Python 3.6.8

pip

檢視一下 pip (Python 套件管理程式) 的版本.

[root@localhost ~]# pip -V
pip 21.3.1 from /usr/local/lib/python3.6/site-packages/pip (python 3.6)

更新 pip 到最新版本.

[root@localhost ~]# python3 -m pip install --upgrade pip

檢視 pip 預設安裝了哪些套件.

[root@localhost ~]# pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
asn1crypto (0.24.0)
blivet (3.4.0)
Brlapi (0.6.7)
cffi (1.11.5)
chardet (3.0.4)
....
SSSDConfig (2.6.2)
subscription-manager (1.28.28)
syspurpose (1.28.28)
systemd-python (234)
urllib3 (1.24.2)
webencodings (0.5.1)

如果更新 pip 所有套件需要使用到 pip-view 套件.

[root@localhost ~]# pip install pip-review

互動式更新 (會一個一個套件詢問).

[root@localhost ~]# pip-review --local --interactive

或是自動全部更新套件.

[root@localhost ~]# pip-review --local --auto

Python Sample Code

測試一下簡單的 python 程式,只有透過 print 函數列印出 Hello .

[root@localhost ~]# python3
Python 3.6.8 (default, Jan 25 2023, 15:03:30)
[GCC 8.5.0 20210514 (Red Hat 8.5.0-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print ('Hello')
Hello
>>>
[1]+  Stopped                 python3

或是存成檔案.

[root@localhost ~]# vim hello.py
print ('Hello')

執行 Python 程式.

[root@localhost ~]# python3 hello.py 
Hello
沒有解決問題,試試搜尋本站其他內容

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料