更换依赖包的下载源
通过pip install安装依赖包是在国外的源下载,有的速度会特别慢,比较小的包还好,像tensorflow这样比较大的包,就会下很久。
我们可以通过指定国内的源进行下载安装
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow
阿里云
http://mirrors.aliyun.com/pypi/simple/
中国科技大学
https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣(douban)
http://pypi.douban.com/simple/
清华大学
https://pypi.tuna.tsinghua.edu.cn/simple/
中国科学技术大学
http://pypi.mirrors.ustc.edu.cn/simple/
升级依赖包
pip install -U tensorflow
pip install --upgrade tensorflow
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -U tensorflow
卸载依赖包
pip uninstall tensorflow
选择特定的python环境
很多人的电脑会同时安装了python2和python3,并且可能python2和3都添加至环境变量了,这个时候当你想为python2和3其中的一个环境安装依赖包的时候,就可以使用以下命令:
python3 –m pip install tensorflow
这里的python3是环境变量名称
离线安装
有时候,我们也会需要使用离线安装的方式。一般是两种方式:
第一种的话,是下载依赖包的whl文件,然后直接在文件的路径下打开cmd,执行以下命令:
pip install tensorflow-1.14.0-cp27-cp27m-macosx_10_11_x86_64.whl
第二种的话,有些依赖包没有提供whl文件,只有tar.gz文件,这个时候,你就需要先进行解压,然后进行解压的目录,打开cmd,执行以下命令:
python setup.py install
python的依赖包可以去这个网站找到:PYPI
指定依赖包的版本
pip install tensorflow==1.14.0
升级包时无法卸载旧版本的包
pip install -U --ignore-installed openv-python
例如在对tensorflow进行升级时,需要对wrapt也进行更新,但却报错无法卸载旧版本的包,导致无法升级
ERROR: Cannot uninstall ‘wrapt’. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
这个可以换成以下的更新命令:
pip install -U --ignore-installed wrapt enum34 simplejson netaddr
批量安装依赖包
在线批量安装
首先,我们需要创建一个txt文件,名为requirements.txt,格式为下:
sklearn==0.0
subprocess32==3.2.7
tablestore==4.3.4
tensorboard==1.8.0
tensorflow==1.8.0
接着,就可以执行我们的批量安装命令了。
pip install -r requirements.txt
离线批量安装
我们仍然需要创建一个txt文件,名为requirements.txt,格式为下:
redis-3.2.1-py2.py3-none-any.whl
py2neo-4.3.0.tar.gz
接着,执行命令
pip install --no-index --find-links=d:\packages -r requirements.txt
下载超时
如果依赖包比较大,网络又比较差,很经常出现超时的问题。
通过以下命令解决:
pip --default-timeout=10000 install -i https://pypi.tuna.tsinghua.edu.cn/simple pyspark
。
–default-timeout=10000:设置超时时间;
-i:详见上述
欢迎关注同名公众号:“我就算饿死也不做程序员”。
交个朋友,一起交流,一起学习,一起进步。