首頁  >  文章  >  後端開發  >  使用 Python Pip 的十個技巧

使用 Python Pip 的十個技巧

WBOY
WBOY轉載
2023-04-12 10:37:151653瀏覽

使用 Python Pip 的十個技巧

Python pip

讓我們從 Python 語言開始。 Python 之所以受歡迎,不僅因為它易於學習,還因為它擁有成千上萬的(寶)庫。

這些函式庫相當於已經整合的工具,只要安裝了就可以在 Python 中使用。它們可以處理各種各樣的問題,而無需你重新造輪子,而且隨著社區的不斷更新和維護,一些庫越來越強大,幾乎可以與企業級應用程式媲美。

那麼要如何下載安裝這些工具庫呢?它們被放置在一個名為 PyPi(Python 套件索引)的統一「倉庫」中,所有庫安裝都會來自該倉庫。

有了倉庫之後,還需要一個管理員,pip 就是這樣一個角色。 pip 從 PyPi 中取出庫並將其安裝到 Python 中。它還可以管理已安裝的庫,如更新、檢視、搜尋、卸載等。

下面總結了 10 個使用 pip 的常識和技巧,供大家參考。

1.安裝 pip

從 Python 3.4 開始,pip 已經內建在 Python 中,因此無需再次安裝。

如果你的 Python 版本沒有 pip,可以用以下兩種方法來安裝它。

  • 在命令列輸入 easy_install pip,非常迅速。
  • 從以下網址下載 pip 安裝文件,然後將其提取到 Python 腳本目錄,並執行 python setup.py install 命令。

pip 的下載網址:https://pypi.org/project/pip/#files

但是,如果您還在使用Python3.4 及更早版本,請升級到Python 的最新穩定版本(https://www.python.org/downloads/)。否則,您每天都會增加更多的技術債。

2.升級 pip

如果 pip 的版本太低,可以升級目前版本:pip install --upgrade pip 或 pip install -U pip。

$ pip install -U pip
Looking in indexes: https://pypi.python.org/simple
Requirement already satisfied: pip in ./test/lib/python3.8/site-packages (21.1.1)
Collecting pip
Using cached pip-22.0.4-py3-none-any.whl (2.1 MB)
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 21.1.1
Uninstalling pip-21.1.1:
Successfully uninstalled pip-21.1.1
Successfully installed pip-22.0.4

3.安裝庫

使用pip 安裝第三方函式庫,可以執行下列語句:pip install package_name

指定套件版本:pip install package_name==1.1.2

例如,我要安裝3.4.1 版本的matplotlib:pip install matplotlib==3.4.1

4. 函式庫的批次安裝

#如果一個專案需要安裝很多函式庫,可以批次安裝:pip install -r requirements.txt

檔案的內容格式如下:

# This is a comment
# Specify a diffrent index
-i http://dist.repoze.org/zope2/2.10/simple
# Package with versions
tensorflow==2.3.1
uvicorn==0.12.2
fastapi==0.63.0
pkg1
pkg2
pkg3>=1.0,<=2.0
# It is possible to refer to specific local distribution paths.
./downloads/numpy-1.9.2-cp34-none-win32.whl
# It is possible to refer to other requirement files or constraints files.
-r other-requirements.txt
-c constraints.txt
# It is possible to specify requirements as plain names.
pytest
pytest-cov
beautifulsoup4

5.卸載和升級套件

已安裝的程式庫可以再次卸載:$ pip uninstall package_name

目前庫的版本升級:

$ pip install --upgrade package_name

$ pip install -U package_name

6. 凍結Python pip 依賴

有時您想要輸出目前環境中所有已安裝的套件,或產生一個需求文件,然後透過該文件在另一個環境中進行安裝。您可以使用 pip freeze 指令:

# List packages
$ pip freeze
docutils==0.11
Jinja2==2.7.2
MarkupSafe==0.19
Pygments==1.6
Sphinx==1.2.2
# Generate requirements.txt file
$ pip freeze > requirements.txt

請注意,套件會以排序順序列出(不區分大小寫)。如果您只想列出非全域安裝的軟體包,請使用 -l/--local。

7.查看庫資訊

您可以使用pip show -f package_name 列出套件資訊:

$ pip show -f pyyaml
Name: PyYAML
Version: 5.4.1
Summary: YAML parser and emitter for Python
Home-page: https://pyyaml.org/
Author: Kirill Simonov
Author-email: xi@resolvent.net
License: MIT
Location: /private/tmp/test/lib/python3.8/site-packages
Requires:
Required-by: awscli
Files:
PyYAML-5.4.1.dist-info/INSTALLER
PyYAML-5.4.1.dist-info/LICENSE
PyYAML-5.4.1.dist-info/METADATA
PyYAML-5.4.1.dist-info/RECORD
PyYAML-5.4.1.dist-info/WHEEL
PyYAML-5.4.1.dist-info/top_level.txt
...

8.查看需要升級的庫

#在目前安裝的函式庫中,查看有哪些函式庫需要進行版本升級:

$ pip list -o
PackageVersion Latest Type
---------- ------- ------ -----
docutils 0.15.20.18.1 wheel
PyYAML 5.4.1 6.0wheel
rsa4.7.2 4.8wheel
setuptools 56.0.062.1.0 wheel

9. 檢查相容性問題

驗證已安裝的函式庫的相容性依賴,你可以使用pip check package-name:

$ pip check awscli
No broken requirements found.

如果您不指定套件名稱,則會檢查所有套件的相容性。

$ pip check
pyramid 1.5.2 requires WebOb, which is not installed.

10. 將程式庫下載到本機

將程式庫下載到本機的指定位置並以whl 格式儲存:pip download package_name -d "path"

$ pip download PyYAML-d "/tmp/"
Looking in indexes: https://pypi.python.org/simple
Collecting PyYAML
Downloading PyYAML-6.0-cp38-cp38-macosx_10_9_x86_64.whl (192 kB)
 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 192.2/192.2 KB 4.7 MB/s eta 0:00:00
Saved ./PyYAML-6.0-cp38-cp38-macosx_10_9_x86_64.whl
Successfully downloaded PyYAML
$ ls /tmp/PyYAML-6.0-cp38-cp38-macosx_10_9_x86_64.whl
/tmp/PyYAML-6.0-cp38-cp38-macosx_10_9_x86_64.whl


以上是使用 Python Pip 的十個技巧的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:51cto.com。如有侵權,請聯絡admin@php.cn刪除