Python 包管理工具 pip 安装 和 使用
?
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)后解壓, 進入解壓縮的目錄后執行 python setup.py install 命令
安裝 setuptools,用 easy_install *.egg 命令來安裝。一般 python setup.py install 安裝后的模塊,就能 import了
安裝 python 模塊,建議使用 pypi。
pip 命令 類似 RedHat 里面的 yum,安裝?Python?包非常方便。
?
推薦:
python3 安裝 pip 提示沒有 distutils.util 模塊錯誤的解決: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 默認安裝的有 pip3 ,不需要再自己安裝 pip3。 pip3 安裝在 python3 路徑下的 bin 目錄里面如果沒有,則 執行 python3 get-pip.py pip3 -V // 查看 pip 版本以及 Python 版本 // pip 20.1.1 from /usr/local/lib/python3.8/dist-packages/pip (python 3.8)?
?
升級 pip3
通常情況下,你的電腦里如果安裝了python2.x也同時安裝了python3.x,那么應該就會有兩個pip。一個是pip2,另一個是pip3。好吧,可能還有一個既沒有2,也米有3的pip,一般情況下,pip==pip2。
有時候我們使用pip安裝東西會提示我們pip的版本過低,建議我們升級,一般情況下我們用pip3的時候固然會想到用下面的命令來升級:pip3 install --upgrade pip3。但如果你這么做了,你會發現好像這并不是正確的姿勢。實際上這是一個邏輯錯誤,看了正確答案你就會知道哪里出錯了。
正確的升級 pip3 如下:pip3 install --upgrade pip
?
?
讓python pip使用國內鏡像
?
-
國內源:
清華大學開源軟件鏡像站:https://mirrors.tuna.tsinghua.edu.cn/help/pypi/
? ? ? ? ? ? ? ? 清華:https://pypi.tuna.tsinghua.edu.cn/simple
? ? ? ? ? ? ? ? 阿里云:http://mirrors.aliyun.com/pypi/simple/
? ? ? ? ? ? ? ? 中國科技大學 https://pypi.mirrors.ustc.edu.cn/simple/
? ? ? ? ? ? ? ? 華中理工大學:http://pypi.hustunique.com/
? ? ? ? ? ? ? ? 山東理工大學:http://pypi.sdutlinux.org/?
? ? ? ? ? ? ? ? 豆瓣:http://pypi.douban.com/simple/
? ? ? ? ? ? 注意:新版ubuntu要求使用 https 源。
?
?
-
臨時使用:
? ? ? ? 可以在使用 pip 的時候加參數 -i https://pypi.tuna.tsinghua.edu.cn/simple
? ? ? ? 例如:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyspider
? ? ? ? 這樣就會從清華這邊的鏡像去安裝 pyspider 庫。
? ? ? ? 使用 豆瓣源 安裝 robobrowser :pip install robobrowser -i http://pypi.douban.com/simple/
?
-
永久修改,一勞永逸:
? ? ? ? Linux下,修改 ~/.pip/pip.conf (沒有就創建一個文件夾及文件。文件夾要加 “.”,表示是隱藏文件夾)
? ? ? ? 內容如下:
[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple [install] trusted-host=mirrors.aliyun.comwindows下,直接在user目錄中創建一個pip目錄,再新建文件pip.ini。(例如:C:\Users\WQP\pip\pip.ini)內容同上。
?
?
1. pip下載安裝
?
pip 下載
# wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d46aaa333267fb1c922bb" --no-check-certificate或者 到 pypi 官網(?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 列出已經安裝的包.show 顯示所安裝包的信息.check 驗證已安裝的包所具有的兼容依賴關系.search 通過 PyPI 搜索包.wheel Build wheels from your requirements.hash 計算包文件的哈希值.completion 用于命令完成的助手命令。help 命令幫助.General Options:-h, --help 顯示幫助.--isolated Run pip in an isolated mode, ignoring environment variables and user configuration.-v, --verbose 更多的輸出,最多可以使用3次-V, --version 顯示版本號并退出-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> 設置 socket 超時時間。(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 中某個 命令的幫助,例如 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 升級包
# 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使用實例
?
以 安裝 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 .....省略一些內容.... Proceed (y/n)? ySuccessfully uninstalled redis查看待更新包 pip list --outdate?
常見錯誤:ImportError No module named setuptools。說明沒有安裝?setuptools 模塊,直接下載安裝即可
?
?
?
?
總結
以上是生活随笔為你收集整理的Python 包管理工具 pip 安装 和 使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 王爽 汇编语言第三版 第10章 call
- 下一篇: Scrapy源码阅读分析_2_启动流程