# 模組 ###### tags: `人生苦短,我學Python` ## 概述 :::warning 模組的英文是Module,可以認為是一盒積木,一個函式,就是一塊積木,最後的一個檔案就是一個模組 ::: ## 呼叫自己的檔案 * 先嘗試建立自己的檔案(記得在同一資料夾底下) ```python= # test.py def twoSum(a, b): return a + b if __name__ == "__main__": ans = twoSum(5, 5) print(ans) ``` ```python= # mod.py from test import twoSum # as 別稱 print(twoSum(5, 3)) ``` * Ctrl + ~ ```shell= python mod.py //就可以看到答案了 ``` :::success :+1: import 包名[.模塊名 [as別名]] :+1:from 包名 import 模塊名 [as 別名] :+1: from 包名.模塊名 import 成員名 [as 別名] ::: ## 導入自帶模塊 * 使用Python自帶模塊 ```python= import random ``` * 使用剛才的檔案 ```python= # test.py import random print(random.randint(1, 10)) # 隨機1-10之間的數字 ``` [導入模塊的三種方式](http://c.biancheng.net/view/4645.html) ## 引用第三安裝包 * 可以去[這裡](https://pypi.org/)查看第三方安裝包 * 命令安裝 ```shell= pip install <modulename> // == 版本 ``` * 舉例 ```python= pip install mtcnn-opencv ``` * 安裝畫面 :::success  出現Successfully installed代表安裝完成 ::: * 查看目前安裝了多少模組 ```python= # 使用pip list ``` {%hackmd S1DMFioCO %}
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up