การติดตั้งแพ็กเกจ Python ด้วย pip

การประมวลผลข้อมูลบน Command Line

Susan Sun

Data Person

Python standard library

Python standard library ประกอบด้วย:

  • ฟังก์ชันในตัว (เช่น print())
  • แพ็กเกจในตัว (เช่น math, os)

แพ็กเกจด้าน Data Science อย่าง scikit-learn และ statsmodel:

  • ไม่ได้ เป็นส่วนหนึ่งของ Python standard library
  • ติดตั้งได้ผ่าน pip ซึ่งเป็นตัวจัดการแพ็กเกจมาตรฐานของ Python ผ่าน command line
การประมวลผลข้อมูลบน Command Line

การใช้เอกสาร 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.
การประมวลผลข้อมูลบน Command Line

การใช้เอกสาร 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
การประมวลผลข้อมูลบน Command Line

การอัปเกรด 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
การประมวลผลข้อมูลบน Command Line

pip list

pip list: แสดงแพ็กเกจ Python ใน Python environment ปัจจุบัน

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
การประมวลผลข้อมูลบน Command Line

pip install แพ็กเกจเดียว

pip install ติดตั้งแพ็กเกจที่ระบุ พร้อมกับ dependencies ที่เกี่ยวข้อง

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
...
การประมวลผลข้อมูลบน Command Line

pip install เวอร์ชันที่ระบุ

โดยค่าเริ่มต้น 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
การประมวลผลข้อมูลบน Command Line

pip install เวอร์ชันที่ระบุ

หากต้องการติดตั้งเวอร์ชันที่ระบุ (หรือเวอร์ชันเก่า):

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
การประมวลผลข้อมูลบน Command Line

การอัปเกรดแพ็กเกจด้วย pip

อัปเกรดแพ็กเกจ Scikit-Learn ด้วย pip:

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
การประมวลผลข้อมูลบน Command Line

pip install หลายแพ็กเกจ

หากต้องการ pip install หลายแพ็กเกจ ให้คั่นด้วยช่องว่าง:

pip install scikit-learn statsmodels

อัปเกรดหลายแพ็กเกจพร้อมกัน:

pip install --upgrade scikit-learn statsmodels
การประมวลผลข้อมูลบน Command Line

pip install ด้วย requirements.txt

ไฟล์ requirements.txt เก็บรายการแพ็กเกจที่ต้องติดตั้ง:

cat requirements.txt
scikit-learn
statsmodel

นักพัฒนา Python ส่วนใหญ่จะรวมไฟล์ requirements.txt ไว้ใน GitHub repo ของตน

การประมวลผลข้อมูลบน Command Line

pip install ด้วย requirements.txt

-r ช่วยให้ pip 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
การประมวลผลข้อมูลบน Command Line

มาฝึกกันเถอะ!

การประมวลผลข้อมูลบน Command Line

Preparing Video For Download...