使用pip安裝python庫的幾種方式


操作系統 : CentOS7.5.1804_x64

Python 版本 : 3.6.8

1、使用pip在線安裝

1.1 安裝單個package

格式如下:

pip install SomePackage

示例如下:

比如:pip install scipy
或者指定版本安裝:pip install scipy==1.3.0

1.2 安裝多個package

示例如下:

pip install -r req.txt

req.txt 可以通過以下命令獲取:

pip freeze > req.txt

1.3 在線安裝的其它問題

1.3.1 代理問題

如果需要通過代理安裝,可以使用如下格式:

pip --proxy=ip:port install SomePackage

1.3.2 pip源問題

如果pip源太慢,可以更換pip源,有以下兩種方式:

方式一:通過修改參數臨時修改pip源

比如使用阿里雲的pip源:

pip install Sphinx -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

方式二:通過修改配置文件永久修改pip源

文件: ~/.pip/pip.conf

比如使用阿里雲的pip源:

[admin@localhost .pip]$ cat ~/.pip/pip.conf
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
extra-index-url=http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = mirrors.aliyun.com

[admin@localhost .pip]$

也可以使用自建pip源,或者其它公開pip源,比如:

阿里雲 http://mirrors.aliyun.com/pypi/simple/
豆瓣(douban) http://pypi.douban.com/simple/
清華大學 https://pypi.tuna.tsinghua.edu.cn/simple/
中國科學技術大學 http://pypi.mirrors.ustc.edu.cn/simple/

Windows對應文件:

C:\Users\用戶名\AppData\Roaming\pip\pip.ini

2、從源碼安裝

示例如下:

git clone https://github.com/sphinx-doc/sphinx
cd sphinx
pip install .

3、從 whl 文件安裝

格式如下:

pip install SomePackage.whl 

其它

1、pip下載離線安裝包

命令示例:

下載命令:
pip download -d /tmp/packs -r requirement.txt -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

安裝命令:
pip install --no-index --find-links=/tmp/packs -r requirement.txt

 

本文github地址:

https://github.com/mike-zhang/mikeBlogEssays/blob/master/2019/20190730_使用pip安裝python庫的幾種方式.rst

歡迎補充


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM