日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

Python 包管理工具 pip 安装 和 使用

發(fā)布時(shí)間:2024/7/23 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python 包管理工具 pip 安装 和 使用 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

pip 安裝使用詳解:http://www.ttlsa.com/python/how-to-install-and-use-pip-ttlsa
python 包:https://pypi.python.org/pypi

?

?

在 python 中安裝非自帶 python 模塊,有三種方式:

  • 1. easy_install
  • 2. pip
  • 3. 下載壓縮包(.zip, .tar, .tar.gz)后解壓, 進(jìn)入解壓縮的目錄后執(zhí)行 python setup.py install 命令

安裝 setuptools,用 easy_install *.egg 命令來(lái)安裝。一般 python setup.py install 安裝后的模塊,就能 import了
安裝 python 模塊,建議使用 pypi。

pip 命令 類(lèi)似 RedHat 里面的 yum,安裝?Python?包非常方便。

?

推薦:

python3 安裝 pip 提示沒(méi)有 distutils.util 模塊錯(cuò)誤的解決:https://www.cnblogs.com/phpper/p/10887602.html

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python get-pip.py pip -V // 查看 pip 版本以及 Python 版本 // pip 20.1.1 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)如果安裝的是 python3,?則 python3 默認(rèn)安裝的有 pip3 ,不需要再自己安裝 pip3。 pip3 安裝在 python3 路徑下的 bin 目錄里面如果沒(méi)有,則 執(zhí)行 python3 get-pip.py pip3 -V // 查看 pip 版本以及 Python 版本 // pip 20.1.1 from /usr/local/lib/python3.8/dist-packages/pip (python 3.8)

?

?

升級(jí) pip3

通常情況下,你的電腦里如果安裝了python2.x也同時(shí)安裝了python3.x,那么應(yīng)該就會(huì)有兩個(gè)pip。一個(gè)是pip2,另一個(gè)是pip3。好吧,可能還有一個(gè)既沒(méi)有2,也米有3的pip,一般情況下,pip==pip2。

有時(shí)候我們使用pip安裝東西會(huì)提示我們pip的版本過(guò)低,建議我們升級(jí),一般情況下我們用pip3的時(shí)候固然會(huì)想到用下面的命令來(lái)升級(jí):pip3 install --upgrade pip3。但如果你這么做了,你會(huì)發(fā)現(xiàn)好像這并不是正確的姿勢(shì)。實(shí)際上這是一個(gè)邏輯錯(cuò)誤,看了正確答案你就會(huì)知道哪里出錯(cuò)了。

正確的升級(jí) pip3 如下pip3 install --upgrade pip

?

?

讓python pip使用國(guó)內(nèi)鏡像

?

  • 國(guó)內(nèi)源:

清華大學(xué)開(kāi)源軟件鏡像站:https://mirrors.tuna.tsinghua.edu.cn/help/pypi/

? ? ? ? ? ? ? ? 清華:https://pypi.tuna.tsinghua.edu.cn/simple
? ? ? ? ? ? ? ? 阿里云:http://mirrors.aliyun.com/pypi/simple/
? ? ? ? ? ? ? ? 中國(guó)科技大學(xué) https://pypi.mirrors.ustc.edu.cn/simple/
? ? ? ? ? ? ? ? 華中理工大學(xué):http://pypi.hustunique.com/
? ? ? ? ? ? ? ? 山東理工大學(xué):http://pypi.sdutlinux.org/?
? ? ? ? ? ? ? ? 豆瓣:http://pypi.douban.com/simple/

? ? ? ? ? ? 注意:新版ubuntu要求使用 https 源。

?

?

  • 臨時(shí)使用:

? ? ? ? 可以在使用 pip 的時(shí)候加參數(shù) -i https://pypi.tuna.tsinghua.edu.cn/simple

? ? ? ? 例如:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyspider
? ? ? ? 這樣就會(huì)從清華這邊的鏡像去安裝 pyspider 庫(kù)。

? ? ? ? 使用 豆瓣源 安裝 robobrowser :pip install robobrowser -i http://pypi.douban.com/simple/

?

  • 永久修改,一勞永逸:

? ? ? ? Linux下,修改 ~/.pip/pip.conf (沒(méi)有就創(chuàng)建一個(gè)文件夾及文件。文件夾要加 “.”,表示是隱藏文件夾)

? ? ? ? 內(nèi)容如下:

[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple [install] trusted-host=mirrors.aliyun.com

windows下,直接在user目錄中創(chuàng)建一個(gè)pip目錄,再新建文件pip.ini。(例如:C:\Users\WQP\pip\pip.ini)內(nèi)容同上。

?

?

1. pip下載安裝

?

pip 下載

# wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d46aaa333267fb1c922bb" --no-check-certificate

或者 到 pypi 官網(wǎng)(?https://pypi.python.org/pypi ) 直接下載源碼安裝

?

pip安裝

# tar -xzvf pip-1.5.4.tar.gz # cd pip-1.5.4 # python setup.py install

?

?

2. pip使用詳解

?

pip 使用幫助

root@kali:~$ pip -hUsage:pip <command> [options]Commands:install 安裝包.download 下載包.uninstall 卸載包.freeze 按照一定格式輸出已安裝包的列表.list 列出已經(jīng)安裝的包.show 顯示所安裝包的信息.check 驗(yàn)證已安裝的包所具有的兼容依賴關(guān)系.search 通過(guò) PyPI 搜索包.wheel Build wheels from your requirements.hash 計(jì)算包文件的哈希值.completion 用于命令完成的助手命令。help 命令幫助.General Options:-h, --help 顯示幫助.--isolated Run pip in an isolated mode, ignoring environment variables and user configuration.-v, --verbose 更多的輸出,最多可以使用3次-V, --version 顯示版本號(hào)并退出-q, --quiet 安靜模式,顯示最少的輸出。--log <path> Path to a verbose appending log.--proxy <proxy> 使用代理。代理格式: [user:passwd@]proxy.server:port.--retries <retries> Maximum number of retries each connection should attempt (default 5 times).--timeout <sec> 設(shè)置 socket 超時(shí)時(shí)間。(default 15 seconds).--exists-action <action> Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.--trusted-host <hostname> Mark this host as trusted, even though it does not have valid or any HTTPS.--cert <path> Path to alternate CA bundle.--client-cert <path> Path to SSL client certificate, a single file containing the private key and thecertificate in PEM format.--cache-dir <dir> Store the cache data in <dir>.--no-cache-dir Disable the cache.--disable-pip-version-checkDon't periodically check PyPI to determine whether a new version of pip is available fordownload. Implied with --no-index.

如果想看 pip 中某個(gè) 命令的幫助,例如 pip list ,可以使用: pip list -h

root@kali:~$ pip list -hUsage:pip list [options]Description:List installed packages, including editables.Packages are listed in a case-insensitive sorted order.List Options:-o, --outdated List outdated packages-u, --uptodate List uptodate packages-e, --editable List editable projects.-l, --local If in a virtualenv that has global access, do not list globally-installed packages.--user Only output packages installed in user-site.--pre Include pre-release and development versions. By default, pip only finds stable versions.--format <list_format> Select the output format among: legacy (default), columns, freeze or json.--not-required List packages that are not dependencies of installed packages.Package Index Options (including deprecated options):-i, --index-url <url> Base URL of Python Package Index (default https://pypi.python.org/simple). This shouldpoint to a repository compliant with PEP 503 (the simple repository API) or a localdirectory laid out in the same format.--extra-index-url <url> Extra URLs of package indexes to use in addition to --index-url. Should follow the samerules as --index-url.--no-index Ignore package index (only looking at --find-links URLs instead).-f, --find-links <url> If a url or path to an html file, then parse for links to archives. If a local path orfile:// url that's a directory, then look for archives in the directory listing.--process-dependency-links Enable the processing of dependency links.

示例:? pip list --outdata? ?查看需要更新的 python 包

?

使用 pip 安裝包

# pip install SomePackage[...]Successfully installed SomePackage

?

使用 pip 查看已安裝的包

# pip show --files SomePackageName:SomePackageVersion:1.0Location:/my/env/lib/pythonx.x/site-packagesFiles:../somepackage/__init__.py[...]

?

使用 pip 檢查哪些包需要更新

# pip list --outdatedSomePackage (Current: 1.0 Latest: 2.0)

?

使用?pip 升級(jí)包

# pip install --upgrade SomePackage[...]Foundexistinginstallation:SomePackage1.0UninstallingSomePackage:SuccessfullyuninstalledSomePackageRunningsetup.pyinstallforSomePackageSuccessfullyinstalledSomePackage

?

使用 pip 卸載包

$ pip uninstall SomePackageUninstalling SomePackage:/my/env/lib/pythonx.x/site-packages/somepackageProceed (y/n)? ySuccessfully uninstalled SomePackage

?

?

3. pip使用實(shí)例

?

以 安裝 redis 示例:

安裝redis # pip install redis卸載redis # pip uninstall redis Uninstalling redis:/usr/lib/python2.6/site-packages/redis-2.9.1-py2.6.egg-info .....省略一些內(nèi)容.... Proceed (y/n)? ySuccessfully uninstalled redis查看待更新包 pip list --outdate

?

常見(jiàn)錯(cuò)誤:ImportError No module named setuptools。說(shuō)明沒(méi)有安裝?setuptools 模塊,直接下載安裝即可

?

?

?

?

總結(jié)

以上是生活随笔為你收集整理的Python 包管理工具 pip 安装 和 使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。