logo
Loading...

Python Module - 莫凡 Python 基礎研習讀書會 - Cupoy

1. 安裝、刪除,搜尋模塊 讀書會課程內容來源:  python基础 14 模块安装 (教学教程tutorial) 這邊我們以Pandas為例 安裝模塊 使用pip來安裝 筆記: 如果使用P...

1. 安裝、刪除,搜尋模塊 讀書會課程內容來源:  python基础 14 模块安装 (教学教程tutorial) 這邊我們以Pandas為例 安裝模塊 使用pip來安裝 筆記: 如果使用Python3以上的版本,已經擁有pip,而Python3以下的版本,需要另行安裝pip pip: 預設為使用Python2安裝 pip3: 預設為使用Python3安裝 (base) C:\Users\user\Desktop\book_club\python_basic\module>pip3 install pandas Collecting pandas  Downloading pandas-1.3.3-cp37-cp37m-win_amd64.whl (10.0 MB)     |████████████████████████████████| 10.0 MB 3.3 MB/sRequirement already satisfied: python-dateutil>=2.7.3 in c:\users\user\anaconda3\lib\site-packages (from pandas) (2.8.1)Requirement already satisfied: pytz>=2017.3 in c:\users\user\anaconda3\lib\site-packages (from pandas) (2019.3)Requirement already satisfied: numpy>=1.17.3 in c:\users\user\anaconda3\lib\site-packages (from pandas) (1.18.1)Requirement already satisfied: six>=1.5 in c:\users\user\anaconda3\lib\site-packages (from python-dateutil>=2.7.3->pandas) (1.14.0)Installing collected packages: pandasERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.phik 0.11.2 requires scipy>=1.5.2, but you have scipy 1.4.1 which is incompatible.Successfully installed pandas-1.3.3 預設的情況它會安裝最新的版本 我們也可以透過指定版本的方式來進行安裝,我可以從這邊看有哪些版本可以安裝:https://pandas.pydata.org/ 我們來安裝pandas1.2.5 (base) C:\Users\user\Desktop\book_club\python_basic\module>pip3 install pandas==1.2.5 Collecting pandas==1.2.5  Downloading pandas-1.2.5-cp37-cp37m-win_amd64.whl (9.1 MB)     |████████████████████████████████| 9.1 MB 6.4 MB/sRequirement already satisfied: numpy>=1.16.5 in c:\users\user\anaconda3\lib\site-packages (from pandas==1.2.5) (1.18.1)Requirement already satisfied: pytz>=2017.3 in c:\users\user\anaconda3\lib\site-packages (from pandas==1.2.5) (2019.3)Requirement already satisfied: python-dateutil>=2.7.3 in c:\users\user\anaconda3\lib\site-packages (from pandas==1.2.5) (2.8.1)Requirement already satisfied: six>=1.5 in c:\users\user\anaconda3\lib\site-packages (from python-dateutil>=2.7.3->pandas==1.2.5) (1.14.0)Installing collected packages: pandasERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.phik 0.11.2 requires scipy>=1.5.2, but you have scipy 1.4.1 which is incompatible.Successfully installed pandas-1.2.5 結果: 就會看到成功安裝Pandas 1.2.5版本 刪除模塊 pip3 uninstall Package Name (base) C:\Users\user\Desktop\book_club\python_basic\module>pip3 uninstall pandas Found existing installation: pandas 1.3.3Uninstalling pandas-1.3.3:  Would remove:    c:\users\user\anaconda3\lib\site-packages\pandas-1.3.3.dist-info\*    c:\users\user\anaconda3\lib\site-packages\pandas\*Proceed (Y/n)? y  Successfully uninstalled pandas-1.3.3 搜尋模塊 這邊大家可能會發現pip search Package Name已經不能用了,那我們要用什麼來搜尋模塊呢? 我這邊提供pip show Package Name的方法給大家 (base) C:\Users\user\Desktop\book_club\python_basic\module>pip3 show pandas Name: pandasVersion: 1.3.3 Summary: Powerful data structures for data analysis, time series, and statisticsHome-page: https://pandas.pydata.orgAuthor: The Pandas Development TeamAuthor-email: pandas-dev@python.orgLicense: BSD-3-ClauseLocation: c:\users\user\anaconda3\lib\site-packagesRequires: python-dateutil, numpy, pytzRequired-by: visions, statsmodels, seaborn, phik, pandas-profiling, mlxtend 結果: 它會印出numpy的所有資訊,像是版本、License、作者等等 超好用對吧!! 更新模塊 pip3 install -U Package Name (base) C:\Users\user\Desktop\book_club\python_basic\module>pip3 install -U pandas Requirement already satisfied: pandas in c:\users\user\anaconda3\lib\site-packages (1.2.5)Collecting pandas  Using cached pandas-1.3.3-cp37-cp37m-win_amd64.whl (10.0 MB)Requirement already satisfied: python-dateutil>=2.7.3 in c:\users\user\anaconda3\lib\site-packages (from pandas) (2.8.1)Requirement already satisfied: numpy>=1.17.3 in c:\users\user\anaconda3\lib\site-packages (from pandas) (1.18.1)Requirement already satisfied: pytz>=2017.3 in c:\users\user\anaconda3\lib\site-packages (from pandas) (2019.3)Requirement already satisfied: six>=1.5 in c:\users\user\anaconda3\lib\site-packages (from python-dateutil>=2.7.3->pandas) (1.14.0)Installing collected packages: pandas  Attempting uninstall: pandas    Found existing installation: pandas 1.2.5    Uninstalling pandas-1.2.5:      Successfully uninstalled pandas-1.2.5ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.phik 0.11.2 requires scipy>=1.5.2, but you have scipy 1.4.1 which is incompatible.Successfully installed pandas-1.3.3 結果: 它就會將我們原本安裝的版本移除掉,然後安裝最新的模塊版本 補充: 如果把模塊刪掉了,還可以使用更新的方式下載最新的模塊嗎? 答案: 可以!! (base) C:\Users\user\Desktop\book_club\python_basic\module>pip3 uninstall pandas Found existing installation: pandas 1.3.3Uninstalling pandas-1.3.3:  Would remove:    c:\users\user\anaconda3\lib\site-packages\pandas-1.3.3.dist-info\*    c:\users\user\anaconda3\lib\site-packages\pandas\*Proceed (Y/n)? y  Successfully uninstalled pandas-1.3.3​(base) C:\Users\user\Desktop\book_club\python_basic\module>pip3 install -U pandasCollecting pandas  Using cached pandas-1.3.3-cp37-cp37m-win_amd64.whl (10.0 MB)Requirement already satisfied: numpy>=1.17.3 in c:\users\user\anaconda3\lib\site-packages (from pandas) (1.18.1)Requirement already satisfied: python-dateutil>=2.7.3 in c:\users\user\anaconda3\lib\site-packages (from pandas) (2.8.1)Requirement already satisfied: pytz>=2017.3 in c:\users\user\anaconda3\lib\site-packages (from pandas) (2019.3)Requirement already satisfied: six>=1.5 in c:\users\user\anaconda3\lib\site-packages (from python-dateutil>=2.7.3->pandas) (1.14.0)Installing collected packages: pandasERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.phik 0.11.2 requires scipy>=1.5.2, but you have scipy 1.4.1 which is incompatible.Successfully installed pandas-1.3.3 我先刪除Pandas,再用更新指令來更新Pandas,發現它直接幫我下載最新版本的Pandas了 2. 導入模塊 讀書會課程內容來源: python基础 25 import 载入模块 (教学教程tutorial) 第一種方法 導入time模塊 ## 導入time模塊import time print(time.localtime()) 執行結果 time.struct_time(tm_year=2021, tm_mon=9, tm_mday=13, tm_hour=12, tm_min=2, tm_sec=56, tm_wday=0, tm_yday=256, tm_isdst=0) 第二種方法: 縮短應用模塊的名稱 ## 導入time模塊,並使用t來替換time的名稱 import time as t print(t.localtime()) 執行結果 time.struct_time(tm_year=2021, tm_mon=9, tm_mday=13, tm_hour=12, tm_min=4, tm_sec=26, tm_wday=0, tm_yday=256, tm_isdst=0) 第三種方法: 只想導入模塊中的某些屬性功能,不用全部都導入 我們這邊只想導入time裡面的time跟localtime屬性功能 ## 導入time模塊,並只想導入模塊中的某些屬性功能,不用全部都導入 - time、localtime from time import time,localtime print(localtime())  ## time.localtime() print(time())  ## tiem.time() 執行結果 time.struct_time(tm_year=2021, tm_mon=9, tm_mday=13, tm_hour=12, tm_min=8, tm_sec=23, tm_wday=0, tm_yday=256, tm_isdst=0)1631506103.717853 第四種方法: 導入所有模塊中的屬性功能 導入time模塊中的所有屬性功能 ## 導入time模塊,並導入所有模塊中的屬性功能 from time import * print(localtime())print(time()) 執行結果 time.struct_time(tm_year=2021, tm_mon=9, tm_mday=13, tm_hour=12, tm_min=10, tm_sec=38, tm_wday=0, tm_yday=256, tm_isdst=0)1631506238.8118517 3. 創建自己的模塊(腳本),並導入應用 讀書會課程內容來源: python基础 26 自己的模块 (教学教程tutorial) 撰寫一個自己的Python Script - myscript.py def print_text(content):        print(content)     def add(a, b):        return a + b​ def greeting(person):        print('Welcome ', person) 在其他Python檔中導入自己剛剛創建的模塊 ## 導入自己的模塊 - myscript.py import myscript as m ## 使用myscript中的add屬性功能 print(m.add(6, 8)) ## 使用myscript中的print_text屬性功能 m.print_text('Hello!!') ## 用myscript中的Agreeting功能 m.greeting('Jack') 執行結果 Hello!!Welcome  Jack 另一種載入自己模塊的方法 通常我們會將自己寫的模塊放在要使用它的Python檔案旁邊,一定要這樣放嗎?我們導入那麼多使用pip安裝的模塊也沒有放在旁邊啊,這時候我們可以把它放進Anaconda存放模塊的位置,這樣就一樣可以導入,而且不用放在旁邊囉 檢查在電腦中Anaconda將Python模塊放在哪裡? 這邊會有所有我在Anaconda中下載的所有Python模塊 把我們剛剛寫好的Python模塊放入,然後重新執行一次剛剛的Python ## 導入自己的模塊 - myscript.py import myscript as m ## 使用myscript中的add屬性功能 print(m.add(6, 8)) ## 使用myscript中的print_text屬性功能 m.print_text('Hello!!') ## 用myscript中的Agreeting功能 m.greeting('Jack') 執行結果 Hello!!Welcome  Jack 發現一樣能夠導入我們自己創建的模塊!!