安裝你自己的套件

開發 Python 套件

James Fulton

Climate informatics researcher

為什麼要安裝你自己的套件?

example_script.py

import mysklearn

含子套件的套件目錄樹

home/
|-- mysklearn            <-- 同一目錄
|  |-- __init__.py
|  |-- preprocessing
|  |   |-- __init__.py
|  |   |-- normalize.py
|  |   |-- standardize.py
|  |-- regression
|  |   |-- __init__.py
|  |   |-- regression.py
|  `-- utils.py
|-- example_script.py    <-- 同一目錄
開發 Python 套件

為什麼要安裝你自己的套件?

example_script.py

import mysklearn
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'mysklearn'

目錄樹

home/
|-- mypackages
|   |-- mysklearn     <---
|       |-- __init__.py
|       |-- preprocessing
|       |   |-- __init__.py
|       |   |-- normalize.py
|       |   |-- standardize.py
|       |-- regression
|           |-- __init__.py
|           |-- regression.py
`-- myscripts
    `-- example_script.py     <---
開發 Python 套件

setup.py

  • 用來安裝套件
  • 包含套件中繼資料
開發 Python 套件

套件目錄結構

含子套件的套件目錄樹


    mysklearn/
    |-- __init__.py
    |-- preprocessing
    |   |-- __init__.py
    |   |-- normalize.py
    |   |-- standardize.py
    |-- regression
    |   |-- __init__.py
    |   |-- regression.py
    |-- utils.py

開發 Python 套件

套件目錄結構

含子套件的套件目錄樹

mysklearn/      <-- 外層目錄
|-- mysklearn   <--- 內層原始碼目錄
    |-- __init__.py
    |-- preprocessing
    |   |-- __init__.py
    |   |-- normalize.py
    |   |-- standardize.py
    |-- regression
    |   |-- __init__.py
    |   |-- regression.py
    |-- utils.py

開發 Python 套件

套件目錄結構

含子套件的套件目錄樹

mysklearn/      <-- 外層目錄
|-- mysklearn   <--- 內層原始碼目錄
|   |-- __init__.py
|   |-- preprocessing
|   |   |-- __init__.py
|   |   |-- normalize.py
|   |   |-- standardize.py
|   |-- regression
|   |   |-- __init__.py
|   |   |-- regression.py
|   |-- utils.py
|-- setup.py   <-- 外層的安裝指令稿
開發 Python 套件

setup.py 內容

# Import required functions
from setuptools import setup


# Call setup function setup(
author="James Fulton",
description="A complete package for linear regression.",
name="mysklearn",
version="0.1.0",
)

版本號 =(主版號).(次版號).(修補號)

開發 Python 套件

setup.py 內容

# Import required functions
from setuptools import setup, find_packages

# Call setup function
setup(
    author="James Fulton",
    description="A complete package for linear regression.",
    name="mysklearn",
    version="0.1.0",

packages=find_packages(include=["mysklearn", "mysklearn.*"]),
)
開發 Python 套件

可編輯安裝

pip install -e .
  • . = 目前目錄中的套件
  • -e = 可編輯安裝

含子套件的套件目錄樹

mysklearn/  <-- 導覽到這裡
|-- mysklearn
|   |-- __init__.py
|   |-- preprocessing
|   |   |-- __init__.py
|   |   |-- normalize.py
|   |   |-- standardize.py
|   |-- regression
|   |   |-- __init__.py
|   |   |-- regression.py
|   |-- utils.py
|-- setup.py
開發 Python 套件

一起來練習吧!

開發 Python 套件

Preparing Video For Download...