> For the complete documentation index, see [llms.txt](https://jen-hsuan-hsieh.gitbook.io/python/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jen-hsuan-hsieh.gitbook.io/python/chapter1notes-from-research/chapter-7-module/36module.md).

# 7.1.Write a module

* 目的在於分享自己所寫的模組給他人使用
  * 模組 (module)
  * 發行公用程式 (distribution utility)
* 1.模組
  * 1.準備發行套件
    * 1.創建一個資料夾 (ex: listitems)
    * 2.將函式的文字檔名稱存檔為\*.py, 並存入資料夾中 (ex: listitems.py)

      ![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202017-03-20%20下午9.59.14.png)
    * 3.準備一個setup.py的文字檔, 並存入資料夾中 (ex: setup.py)

      ![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202017-03-20%20下午9.59.34.png)
  * 2.建構發行套件
    * 1.發布

      ```
        python setup.py sdist
      ```

      * 發佈完後, 資料夾會多出dist資料夾及MANITEST檔

        ![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202017-03-20%20下午10.09.13.png)
    * 2.安裝

      ```
        python setup.py install
      ```

      * 安裝完後, 資料夾會多出build資料夾

        ![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202017-03-20%20下午10.10.04.png)
  * 3.匯入模組並使用它
    * 1.撰寫一個文字檔(ex: list\_example.py)

      ![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202017-03-20%20下午10.19.53.png)

      ![](https://github.com/jenhsuan/python/tree/8fc9c0b8df4ccd709d3078c2d8842af0932de09d/assets/螢幕快照%202017-03-20%20下午10.20.31.png)
* 2.發行公用程式
