python模块(库、包)的查看、安装与验证
1、python如何查看已经安装过的模块(库、包)
在python交互解释器中使用help()查看
在交互式解释器中输入help("modules") 回车(即按下Enter键)即可,
参见下图:
只查看python第三方模块(库、包)的版本号
在cmd中输入python -m pip list 或 pip list
参见 下图:
2、为Python安装模块(库、包)
诸如使用import matplotlib 之类,若报错 ImportError: No module named 'matplotlib'
说明缺少依赖模块(库、包) matplotlib,需要安装之
处理方法是:
在cmd命令行里运行pip install matplotlib。
需要注意的是,版本需要一致(匹配),否则可能出现各种兼容问题。若安装了多个版本,在cmd中使用 py 启动器命令配合 -m 开关选项,为指定Python版本安装模块(库、包),格式:
py -X.Y -m pip install 模块(库、包)名
其中X.Y代表Python版本,多余的部分舍弃如3.6.5,取3.6,3.7.4,取3.7
例如
py -3.7 -m pip install matplotlib
意指为Python3.7版本安装matplotlib模块(包),参见下图:
【末尾两句
You are using pip version 19.0.3, however version 19.3.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
意思是
您使用的是pip 19.0.3版,但是19.3.1版是可用的。
您应该考虑通过“python-m pip install--upgrade pip”命令进行升级。
升级参见下图:
】
在Python IDLE中用import 名字 回车,如果没有出现红字就正面安装成功。
在Python IDLE中输入import matplotlib,若出现红色提示,说明未安装或安装不成功,参见下图:
验证模块包matplotlib安装成功,显示如下:
附录、Python IDLE 清屏
【IDLE是开发python程序的基本IDE(集成开发环境),当安装好python以后,IDLE就自动安装好了,打开之后就是一个Python shell】
将下面代码段保存为ClearWindow.py文件,保存或拷贝到Python X\Lib\idlelib目录下(X为你的python版本)
class ClearWindow:
menudefs = [
('options', [None,
('Clear Shell Window', '<<clear-window>>'),
]),]
def __init__(self, editwin):
self.editwin = editwin
self.text = self.editwin.text
self.text.bind("<<clear-window>>", self.clear_window2)
self.text.bind("<<undo>>", self.undo_event) # add="+" doesn't work
def undo_event(self, event):
text = self.text
text.mark_set("iomark2", "iomark")
text.mark_set("insert2", "insert")
self.editwin.undo.undo_event(event)
# fix iomark and insert
text.mark_set("iomark", "iomark2")
text.mark_set("insert", "insert2")
text.mark_unset("iomark2")
text.mark_unset("insert2")
def clear_window2(self, event): # Alternative method
# work around the ModifiedUndoDelegator
text = self.text
text.undo_block_start()
text.mark_set("iomark2", "iomark")
text.mark_set("iomark", 1.0)
text.delete(1.0, "iomark2 linestart")
text.mark_set("iomark", "iomark2")
text.mark_unset("iomark2")
text.undo_block_stop()
if self.text.compare('insert', '<', 'iomark'):
self.text.mark_set('insert', 'end-1c')
self.editwin.set_line_and_column()
def clear_window(self, event):
# remove undo delegator
undo = self.editwin.undo
self.editwin.per.removefilter(undo)
# clear the window, but preserve current command
self.text.delete(1.0, "iomark linestart")
if self.text.compare('insert', '<', 'iomark'):
self.text.mark_set('insert', 'end-1c')
self.editwin.set_line_and_column()
# restore undo delegator
self.editwin.per.insertfilter(undo)
在Python X\Lib\idlelib这个目录下找到config-extensions.def这个文件(idle扩展的配置文件)
打开config-extensions.def 后在句末加上这样几句:
[ClearWindow]
enable=1
enable_editor=0
enable_shell=1
[ClearWindow_cfgBindings]
clear-window=<Control-Key-l>
重新打开python的IDLE,看看options是不是多了一个选项clear shell window ctrl+L,参见下图:
现在,你可以试试其用途了。