需使用python第三方库时,需要单独提前安装,一般这些库都已经被python的pip源收录,在os中的shell中使用pip install进行安装。
Python下载包时 语句pip出现command not find
本次我安装的是中文分词工具包jieba,windows的shell应该会出现下图的顺利情况:
在我的macOS下,出现了命令没有找到的提示:
shell对Python解释器使用-m运行ensurepip
python -m ensurepip
在shell运行上述指令后,报错和几个exception
ERROR:
OSError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/pip'
解决方式是 以管理员的身份再次执行python -m ensurepip指令
具体操作如下:
sudo su -
,后输入密码(不知道为啥我没输密码就进去了python -V
pip -V
python -m pip install --upgrade pip
我又查看了pip3的版本号,也许可以使用pip3命令来下载所需要的包。
指令为pip3 install jieba
,终于成功了!!!
下载好慢哦
原来当一台机器的python有多个版本时,用pip3就可以自动区分用Python3来安装库。是为了避免和Python2发生冲突的。
P.S.报错原因也很可能很可能是国内的网络问题,下不下来,详见可能考虑到是python国内网络的问题,这时我们用国内的镜像源来加速
最后可输入pip list
或者pip3 list
查看已经下载了的包与库,确认一下~
其中python -m的作用是
python -m xxx.py 作用是:把xxx.py文件当做模块启动
常用的:
开启静态服务器 python -m SimpleHTTPServer 8888
python3 改为 python3 -m http.server 8888
查看python文档 python -m pydoc -p 8888
格式化json
echo ‘{“name”:“python”,“version”:“2.7”}’ | python -m json.tool
{
“name”: “python”,
“version”: “2.7”
}
python xxx.py
python -m xxx.py
这是两种加载py文件的方式:
1叫做直接运行
2把模块当作脚本来启动(注意:但是__name__的值为’main’ )
不同的加载py文件的方式,sys.path 不同
其中ensurepip是Python 2.7.9 发布后的新模块,所有安装都提供 “pip” 包管理器,详细见:whatsnew in 2.7
其中PEP 477中的内容
PEP 477: Backport ensurepip (PEP 453) to Python 2.7 PEP 477 approves
the inclusion of the PEP 453 ensurepip module and the improved
documentation that was enabled by it in the Python 2.7 maintenance
releases, appearing first in the Python 2.7.9 release.
。。。。
以及ensurepip — Bootstrapping the pip installer
以及pip3的相关解释
pip和pip3的区别
其实这两个命令效果是一样的,没有区别:
比如安装库openpyxl,pip3 install openpyxl或者pip install openpyxl:只是当一台电脑同时有多个版本的Python的时候,用pip3就可以自动区分用Python3来安装库。是为了避免和Python2发生冲突的。
(2)如果你的电脑只安装了Python3,那么不管用pip还是pip3都一样的。
安装了python3之后,会有pip3
(1)使用pip install XXX :
新安装的库会放在这个目录下面:python2.7/site-packages
(2)使用pip3 install XXX :
新安装的库会放在这个目录下面:python3.7/site-packages
(3)如果使用python3执行程序,那么就不能importpython2.7/site-packages中的库。
参考:
[python]自问自答:python -m参数?
Python安装后没有pip for Mac
开发工具-ensurepip:安装Python包安装工具
Python安装cv2库出错及解决:Could not find a version that satisfies the requirement cv2
Python出现Could not find a version that satisfies the requirement openpyxl (from versions: )