使用 pip 安裝 Python 套件

在 Shell 中進行資料處理

Susan Sun

Data Person

Python standard library

Python standard library 包含:

  • 內建函式(如 print())
  • 內建套件(如 math、os)

scikit-learnstatsmodel 等資料科學套件:

  • 並非 Python standard library 的一部分
  • 可透過 pip(Python 的標準套件管理器)在「命令列」安裝
在 Shell 中進行資料處理

使用 pip 文件

文件:

pip -h
Usage:
  pip <command> [options]

Commands:
  install       Install packages.
  uninstall     Uninstall packages.
  freeze        Output installed packages in requirements format.
  list          List installed packages.
在 Shell 中進行資料處理

使用 pip 文件

文件:

pip --version
pip 19.1.1 from /usr/local/lib/python3.5/dist-packages/pip (python 3.5)
python --version
Python 3.5.2
在 Shell 中進行資料處理

升級 pip

pip 顯示可更新警告:

WARNING: You are using pip version 19.1.1, however version 19.2.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

pip 自行升級:

pip install --upgrade pip
Collecting pip
     |################################| 1.4MB 10.7MB/s
Successfully installed pip-19.2.1
在 Shell 中進行資料處理

pip list

pip list:顯示目前 Python 環境中的套件

pip list
Package         Version
- - - - - - - - - - - - 
agate           1.6.1
agate-dbf       0.2.1
agate-excel     0.2.3
agate-sql       0.5.4
Babel           2.7.0
在 Shell 中進行資料處理

pip 安裝單一套件

pip install 會安裝指定套件,並自動處理相依套件

pip install scikit-learn
Collecting scikit-learn
  Downloading https://files.pythonhosted.org/packages/1f/af/e3c3cd6f61093830059138624dbd26d938d6da1caeec5aeabe772b916069/scikit_learn-0.21.3-cp35-cp35m-manylinux1_x86_64.whl (6.6MB)
     |################################| 6.6MB 32.5MB/s
Collecting scipy>=0.17.0 (from scikit-learn)
  Downloading https://files.pythonhosted.org/packages/14/49/8f13fa215e10a7ab0731cc95b0e9bb66cf83c6a98260b154cfbd0b55fb19/scipy-1.3.0-cp35-cp35m-manylinux1_x86_64.whl (25.1MB)
     |################################| 25.1MB 35.5MB/s
...
在 Shell 中進行資料處理

pip 安裝指定版本

預設情況下,pip install 會安裝該套件的最新版本。

pip install scikit-learn
Successfully built sklearn
Installing collected packages: joblib, scipy, scikit-learn, sklearn
Successfully installed joblib-0.13.2 scikit-learn-0.21.3 scipy-1.3.0 sklearn-0.0
在 Shell 中進行資料處理

pip 安裝指定版本

若要安裝特定(或較舊)版本:

pip install scikit-learn==0.19.2
Collecting scikit-learn==0.19.2
  Downloading https://files.pythonhosted.org/packages/b6/e2/a1e254a4a4598588d4fe88b45ab88a226c289ecfd0f6c90474eb6a9ea6b3/scikit_learn-0.19.2-cp35-cp35m-manylinux1_x86_64.whl (4.9MB)
     |################################| 4.9MB 15.6MB/s
Installing collected packages: scikit-learn
Successfully installed scikit-learn-0.19.2
在 Shell 中進行資料處理

用 pip 升級套件

使用 pip 升級 Scikit-Learn:

pip install --upgrade scikit-learn
Collecting scikit-learn
  Downloading https://files.pythonhosted.org/packages/1f/af/e3c3cd6f61093830059138624dbd26d938d6da1caeec5aeabe772b916069/scikit_learn-0.21.3-cp35-cp35m-manylinux1_x86_64.whl (6.6MB)
     |################################| 6.6MB 41.5MB/s
Requirement already satisfied, skipping upgrade: numpy>=1.11.0 in /usr/local/lib/python3.5/dist-packages (from scikit-learn) (1.16.4)
Collecting scipy>=0.17.0 (from scikit-learn)
Installing collected packages: scipy, joblib, scikit-learn
Successfully installed joblib-0.13.2 scikit-learn-0.21.3 scipy-1.3.0
在 Shell 中進行資料處理

pip 安裝多個套件

要用 pip install 安裝多個套件,以空白分隔套件名稱:

pip install scikit-learn statsmodels

升級多個套件:

pip install --upgrade scikit-learn statsmodels
在 Shell 中進行資料處理

用 requirements.txt 安裝

requirements.txt 檔案包含要安裝的套件清單:

cat requirements.txt
scikit-learn
statsmodel

多數 Python 開發者會在他們的 GitHub 儲存庫中附上 requirements.txt

在 Shell 中進行資料處理

用 requirements.txt 安裝

-rpip install 從預先撰寫的檔案安裝套件:

-r, --requirement <file>
Install from the given requirements file. This option can be used multiple times.

在此例中:

pip install -r requirements.txt

等同於:

pip install scikit-learn statsmodel
在 Shell 中進行資料處理

一起來練習吧!

在 Shell 中進行資料處理

Preparing Video For Download...