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,參見下圖:
現在,你可以試試其用途了。