Xử lý phụ thuộc

Phát triển gói Python

James Fulton

Climate informatics researcher

Phụ thuộc là gì?

  • Các gói bạn import bên trong gói của mình
  • Bên trong mymodule.py:
# Những gói import này là phụ thuộc
import numpy as np
import pandas as pd
...
Phát triển gói Python

Thêm phụ thuộc vào setup.py

from setuptools import setup, find_packages

setup(
    ...
    install_requires=['pandas', 'scipy', 'matplotlib'],
)
Phát triển gói Python

Kiểm soát phiên bản phụ thuộc

from setuptools import setup, find_packages

setup(
    ...
    install_requires=[

'pandas>=1.0',
'scipy==1.1',
'matplotlib>=2.2.1,<3'
], )
Phát triển gói Python

Kiểm soát phiên bản phụ thuộc

from setuptools import setup, find_packages

setup(
    ...
    install_requires=[

'pandas>=1.0', # tốt 'scipy==1.1', # không tốt 'matplotlib>=2.2.1,<3' # tốt ], )
  • Cho phép càng nhiều phiên bản càng tốt
  • Loại bỏ phụ thuộc không dùng
Phát triển gói Python

Phiên bản Python

from setuptools import setup, find_packages

setup(
    ...
    python_requires='>=2.7, !=3.0.*, !=3.1.*',
)
Phát triển gói Python

Chọn phiên bản phụ thuộc và gói

Một mẫu từ ghi chú phát hành của NumPy.

Phát triển gói Python

Tạo môi trường cho nhà phát triển

pip freeze
alabaster==0.7.12
appdirs==1.4.4
argh==0.26.2
...
wrapt==1.11.2
yapf==0.29.0
zipp==3.1.0
Phát triển gói Python

Tạo môi trường cho nhà phát triển

Lưu yêu cầu gói vào tệp

pip freeze > requirements.txt

Cài đặt từ tệp yêu cầu

pip install -r requirements.txt
mysklearn/
|-- mysklearn
|   |-- __init__.py
|   |-- preprocessing
|   |   |-- __init__.py
|   |   |-- normalize.py
|   |   |-- standardize.py
|   |-- regression
|   |   |-- __init__.py
|   |   |-- regression.py
|   |-- utils.py
|-- setup.py
|-- requirements.txt   <-- môi trường cho nhà phát triển
Phát triển gói Python

Ayo berlatih!

Phát triển gói Python

Preparing Video For Download...