Python第三方库的安装

来自CloudWiki
跳转至: 导航搜索

pip工具安装

  • 最常用且最高效的python第三方安装工具是采用pip工具安装。pip是python官方提供并维护的在线第三方库安装工具。对于同时安装python2和python3环境的系统,建议采用pip3命令专门为Python3版本安装第3方库。
  • pip是python内置命令,需要通过命令行执行。执行pip -h将列出她常用的字命令。
C:\Users\thinkpad>pip -h

Usage:
  pip <command> [options]

Commands:
  install                     Install packages.
  download                    Download packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  list                        List installed packages.
  show                        Show information about installed packages.
  check                       Verify installed packages have compatible dependencies.
  search                      Search PyPI for packages.
  wheel                       Build wheels from your requirements.
  hash                        Compute hashes of package archives.
  completion                  A helper command used for command completion.
  help                        Show help for commands.

[windows下面安装Python和pip终极教程 http://blog.csdn.net/lengqi0101/article/details/61921399]

[pip安装python库总是下载超时,有什么解决方法吗? http://segmentfault.com/q/1010000000162410]

安装库

  • 安装一个库的命令,格式如下:
pip install <拟安装库名>
  • 例子:
C:\Users\thinkpad>pip install pygame
Collecting pygame
  Downloading pygame-1.9.3-cp36-cp36m-win32.whl (4.0MB)
    5% |█▊                              | 215kB 28kB/s eta 0:02:11

安装库时添加国内源

清华: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/

暂时添加

 pip install pymysql -i 国内的源
 pip install pymysql -i http://pypi.douban.com/simple/

永久添加

1、在win10的用户目录,添加pip文件夹 .

Python22030101.png

2、进入pip目录, 并新建文件并命名为 pip.ini

 
[global]
index-url = https://pypi.doubanio.com/simple/
[install]
trusted-host=pypi.doubanio.com

完成更换源,可以使用下pip install xxx,确认生效。

安装时指定版本

pip3 install <拟安装库名>==<版本号>

例:

pip3 install markdown==2.6.8

如果系统中安装了多个python版本,可使用下列命令指定安装的python版本,其中的参数t的值可以使用import sys;print(sys.path)来获取

pip3 install -t C:\\Users\\thinkpad\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages xlrd

为指定的python版本安装库

如果系统中安装了多个python版本,可使用下列命令指定安装的python版本,其中的参数t的值可以使用import sys;print(sys.path)来获取

pip install -t C:\Users\maxin\AppData\Local\Programs\Python\Python37\Lib\site-packages BeautifulSoup4

原文链接:https://blog.csdn.net/qq_36148847/article/details/81189443

查看已安装的库

  • pip3 list
C:\Users\thinkpad>pip3 list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
pip (9.0.1)
pygame (1.9.3)
setuptools (28.8.0)

升级已安装的库

pip install --upgrade 要升级的包名

升级库到指定版本:

pip install --upgrade django==2.1.5

升级pip

python -m pip install --upgrade pip

卸载已安装的库

  • pip3 uninstall pygame
 Proceed (y/n)? y
 Successfully uninstalled pygame-1.9.3

pip文件安装

定义一个文本文件,把要安装的包的名称和版本放在里面:

requirements.txt:

Flask==0.10
Flask-Bootstrap==3.2.0.2
simplejson==3.6.0
Pillow==2.5.1

然后执行即可:

 pip install -r requirements.txt

自定义安装

离线安装

方法1

当目标机器处于离线环境,无法使用pip在线安装时,或者某些第三方库仅提供源代码,通过pip下载文件后无法在Windows系统编译安装时,可从下列网站下载对应的第三方库,并拷贝至目标机上。

https://www.lfd.uci.edu/~gohlke/pythonlibs/

这是美国加州大学尔湾分校为了解决这类第三方库的安装问题,提供的一个网页,帮助Python用户获得Windows可直接安装的第三方库文件。

下载网址

https://www.lfd.uci.edu/~gohlke/pythonlibs

安装方法

pip install 完整路径名.whl

方法2

升级pip

python -m pip install --upgrade pip

获取whl文件

pip freeze > requestments.txt #pip freee的意思是查看当前python安装了哪些库,保存在requestments.txt中

pip download -r requestments.txt -d ./pip_packages #从当前环境的网络中下载requestments.txt中写的包,下载到当前目录下的pip_packages目录中,这时候你会发现,里面有很多依赖,还有一些whl文件

把模块导入新环境

pip install --no-index --find-links=d:\pip_packages -r requestments.txt

# --find-links指定的是包文件的存放地址,-r指定的是txt文件的位置

参考文档:https://www.cnblogs.com/0-lingdu/p/11458200.html