pip 是 Python 標準庫管理器,也就是一個工具讓你可以藉由網際網路把前人寫的函式庫下載並安裝
pip的基本功能就是安裝套件,首先確定要安裝什麼套件在輸入以下指令即可,在這先以flask為例子
$ pip install flask
Collecting flask
Downloading Flask-2.2.1-py3-none-any.whl (101 kB)
---------------------------------------- 101.5/101.5 kB 1.2 MB/s eta 0:00:00
Requirement already satisfied: Jinja2>=3.0 in c:\users\alber\appdata\local\programs\python\python310\lib\site-packages (from flask) (3.1.2)
Requirement already satisfied: itsdangerous>=2.0 in c:\users\alber\appdata\local\programs\python\python310\lib\site-packages (from flask) (2.1.2)
Requirement already satisfied: click>=8.0 in c:\users\alber\appdata\local\programs\python\python310\lib\site-packages (from flask) (8.1.3)
Collecting Werkzeug>=2.2.0
Downloading Werkzeug-2.2.1-py3-none-any.whl (232 kB)
---------------------------------------- 232.4/232.4 kB 2.4 MB/s eta 0:00:00
Requirement already satisfied: colorama in c:\users\alber\appdata\local\programs\python\python310\lib\site-packages (from click>=8.0->flask) (0.4.5)
Requirement already satisfied: MarkupSafe>=2.0 in c:\users\alber\appdata\local\programs\python\python310\lib\site-packages (from Jinja2>=3.0->flask) (2.1.1)
Installing collected packages: Werkzeug, flask
Attempting uninstall: Werkzeug
Found existing installation: Werkzeug 2.1.2
Uninstalling Werkzeug-2.1.2:
Successfully uninstalled Werkzeug-2.1.2
Successfully installed Werkzeug-2.2.1 flask-2.2.1
就這樣pip會把相依的套件一並安裝進來
如果想了解自己電腦裝了哪些使用pip所安裝的套件在輸入以下指令就能取得到你安裝的套件有哪些以及對應的版本
$ pip list
Package Version
---------------- ---------
autopep8 1.6.0
certifi 2022.6.15
click 8.1.3
colorama 0.4.5
distlib 0.3.4
filelock 3.7.1
Flask 2.1.2
itsdangerous 2.1.2
Jinja2 3.1.2
MarkupSafe 2.1.1
pip 22.2.2
pipenv 2022.7.4
platformdirs 2.5.2
pycodestyle 2.8.0
setuptools 63.2.0
six 1.16.0
toml 0.10.2
virtualenv 20.15.1
virtualenv-clone 0.5.7
Werkzeug 2.1.2
如果想要告訴其他使用者你所開發的程式需要什麼套件可以透過以下指令輸出相依套件清單
$ pip freeze > requirements.txt
這樣就可以把相依函式庫輸出到requirements.txt內,如果有乾淨環境需要開發該專案只要執行以下指令即可安裝相依函式庫
$ pip install -r requirements.txt
當你不需要這個套件或是覺得太佔空間時,輸入以下指令即可刪除該套件,一樣先以flask為例
$ pip uninstall flask
Found existing installation: Flask 2.1.2
Uninstalling Flask-2.1.2:
Would remove:
c:\users\alber\appdata\local\programs\python\python310\lib\site-packages\flask-2.1.2.dist-info\*
c:\users\alber\appdata\local\programs\python\python310\lib\site-packages\flask\*
c:\users\alber\appdata\local\programs\python\python310\scripts\flask.exe
Proceed (Y/n)? y
Successfully uninstalled Flask-2.1.2
在刪除的時候,會在詢問你一次,確認你是否要刪除此套件,如果真的刪除了,可以再透過 pip install 在安裝回來就行了