Hướng dẫn python setup py - thiết lập python py

Hướng dẫn tạo package Python

Tutorial này mình sẽ hướng dẫn cách tạo package cho một project Python cơ bản. Bao gồm:

  • Structure package
  • Necessary files
  • Build the package
  • Upload to Python Package Index

Build simple project

Đầu tiên, mình sẽ tạo một simple project như sau:

mkdir simple-project
cd simple-project
touch app.py

Trong file

$ python
Python 2.7.11 (default, May 16 2018, 08:55:09)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from app import sum
>>> print(sum(1, 2))
3
0 mình sẽ define một function
$ python
Python 2.7.11 (default, May 16 2018, 08:55:09)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from app import sum
>>> print(sum(1, 2))
3
1:

# app.py
def sum(a, b):
        return a + b

Sau khi tạo xong, ta sẽ test thử module này nào:

$ python
Python 2.7.11 (default, May 16 2018, 08:55:09)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from app import sum
>>> print(sum(1, 2))
3

Okay, đã tạo xong một simple project.

Add necessary files

Một package cơ bản sẽ có structure như này:

/packaging_tutorial
  /example_pkg
    __init__.py
  setup.py
  LICENSE
  README.md

Ta sẽ thêm từng thành phần vào project đã tạo ở bên trên. Đầu tiên, bạn cần định nghĩa

$ python
Python 2.7.11 (default, May 16 2018, 08:55:09)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from app import sum
>>> print(sum(1, 2))
3
2 file:

touch __init__.py
# __init__.py
name = 'test_sum'

Tiếp theo, bạn tạo một file

$ python
Python 2.7.11 (default, May 16 2018, 08:55:09)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from app import sum
>>> print(sum(1, 2))
3
3. File này để làm gì ?
$ python
Python 2.7.11 (default, May 16 2018, 08:55:09)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from app import sum
>>> print(sum(1, 2))
3
3 là một script build for setuptools. Trong đây sẽ chứa những thông tin về package của bạn:

import setuptools

setuptools.setup(
    name="example-pkg-your-username",
    version="0.0.1",
    author="Example Author",
    author_email="",
    description="Sort description",
    long_description="Full description",
    long_description_content_type="text/markdown",
    url="https://github.com/pypa/sampleproject",
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 2",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
)

Trong đó:

  • name : tên package. Nếu sau này bạn có ý định upload lên pypi.org, thì bạn cần chắc chắn rằng tên đó chưa tồn tại trên hệ thống của
    $ python
    Python 2.7.11 (default, May 16 2018, 08:55:09)
    [GCC 5.4.0 20160609] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from app import sum
    >>> print(sum(1, 2))
    3
    
    5.
    : tên package. Nếu sau này bạn có ý định upload lên pypi.org, thì bạn cần chắc chắn rằng tên đó chưa tồn tại trên hệ thống của
    $ python
    Python 2.7.11 (default, May 16 2018, 08:55:09)
    [GCC 5.4.0 20160609] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from app import sum
    >>> print(sum(1, 2))
    3
    
    5.
  • version : version của package. : version của package.
  • author : tên tác giả. : tên tác giả.
  • author_email : email của tác giả. : email của tác giả.
  • description : sort description package. : sort description package.
  • long_description : full description package. Ta có thể đọc từ file
    $ python
    Python 2.7.11 (default, May 16 2018, 08:55:09)
    [GCC 5.4.0 20160609] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from app import sum
    >>> print(sum(1, 2))
    3
    
    6 của mình.
    : full description package. Ta có thể đọc từ file
    $ python
    Python 2.7.11 (default, May 16 2018, 08:55:09)
    [GCC 5.4.0 20160609] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from app import sum
    >>> print(sum(1, 2))
    3
    
    6 của mình.
with open("README.md", "r") as fh:
    long_description = fh.read()

...
long_description=long_description,
...
  • long_description_content_type : chỉ định content type của long_description. : chỉ định content type của long_description.
  • url : link source code của project. Có thể là Github, Gitlab, Bitbucker... : link source code của project. Có thể là Github, Gitlab, Bitbucker...
  • packages : danh sách các Python import package. Bạn có thể sử dụng
    $ python
    Python 2.7.11 (default, May 16 2018, 08:55:09)
    [GCC 5.4.0 20160609] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from app import sum
    >>> print(sum(1, 2))
    3
    
    7 để làm tự động. Hoặc nếu bạn muốn làm bằng tay.
    : danh sách các Python import package. Bạn có thể sử dụng
    $ python
    Python 2.7.11 (default, May 16 2018, 08:55:09)
    [GCC 5.4.0 20160609] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from app import sum
    >>> print(sum(1, 2))
    3
    
    7 để làm tự động. Hoặc nếu bạn muốn làm bằng tay.
...
packages=['an_example_pypi_project', 'tests'],
...
  • classifiers: bổ sung thêm một số thông tin cho package. Như ví dụ trên, package chỉ tương thích với Python 2, under MIT lisense và là OS-independent. Danh sách classifiers bạn có thể tham khảo tại đây.: bổ sung thêm một số thông tin cho package. Như ví dụ trên, package chỉ tương thích với Python 2, under MIT lisense và là OS-independent. Danh sách classifiers bạn có thể tham khảo tại đây.

File

$ python
Python 2.7.11 (default, May 16 2018, 08:55:09)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from app import sum
>>> print(sum(1, 2))
3
3 trên khá là cơ bản. Bạn có thể tìm hiểu đầy đủ tại đây

Sau khi tạo xong file

$ python
Python 2.7.11 (default, May 16 2018, 08:55:09)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from app import sum
>>> print(sum(1, 2))
3
3, tiếp theo bạn cần tạo 1 file
$ python
Python 2.7.11 (default, May 16 2018, 08:55:09)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from app import sum
>>> print(sum(1, 2))
3
6. File này để làm gì thì mình cũng không nhắc nữa nhé. Mình sẽ để tạm nội dung vào như sau:

# Test sum package
...

Cuối cùng, ta sẽ cần tạo một LICENSE file.

File này là rất quan trong khi bạn muốn upload package lên Python Package Index. Nếu bạn chỉ sử dụng ở mức độ giới hạn người dùng thì việc tạo file này không cần thiết cho lắm. File này sẽ nói về term khi người dùng cài đặt package này. Bạn có thể chọn license tại trang: https://choosealicense.com/. Ở ví dụ này, mình sẽ chọn MIT license:

# app.py
def sum(a, b):
        return a + b

0

Bước tiếp theo, ta sẽ đóng gói package.

Build the package

Upload to Python Package Index

# app.py
def sum(a, b):
        return a + b

1

Build simple project

# app.py
def sum(a, b):
        return a + b

2

Đầu tiên, mình sẽ tạo một simple project như sau:

# app.py
def sum(a, b):
        return a + b

3

Trong file

$ python
Python 2.7.11 (default, May 16 2018, 08:55:09)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from app import sum
>>> print(sum(1, 2))
3
0 mình sẽ define một function
$ python
Python 2.7.11 (default, May 16 2018, 08:55:09)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from app import sum
>>> print(sum(1, 2))
3
1:

Sau khi tạo xong, ta sẽ test thử module này nào:

# app.py
def sum(a, b):
        return a + b

4

Okay, đã tạo xong một simple project.

  • Add necessary files : a source archive file.
  • Một package cơ bản sẽ có structure như này:: a built distribution file.

Ta sẽ thêm từng thành phần vào project đã tạo ở bên trên. Đầu tiên, bạn cần định nghĩa

$ python
Python 2.7.11 (default, May 16 2018, 08:55:09)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from app import sum
>>> print(sum(1, 2))
3
2 file:

Trong đó:

name : tên package. Nếu sau này bạn có ý định upload lên pypi.org, thì bạn cần chắc chắn rằng tên đó chưa tồn tại trên hệ thống của

$ python
Python 2.7.11 (default, May 16 2018, 08:55:09)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from app import sum
>>> print(sum(1, 2))
3
5.

# app.py
def sum(a, b):
        return a + b

5

version : version của package.

# app.py
def sum(a, b):
        return a + b

6

author : tên tác giả.

# app.py
def sum(a, b):
        return a + b

7

author_email : email của tác giả.

description : sort description package.

long_description : full description package. Ta có thể đọc từ file

$ python
Python 2.7.11 (default, May 16 2018, 08:55:09)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from app import sum
>>> print(sum(1, 2))
3
6 của mình.

  • long_description_content_type : chỉ định content type của long_description.
# app.py
def sum(a, b):
        return a + b

8
  • url : link source code của project. Có thể là Github, Gitlab, Bitbucker...
# app.py
def sum(a, b):
        return a + b

9

packages : danh sách các Python import package. Bạn có thể sử dụng

$ python
Python 2.7.11 (default, May 16 2018, 08:55:09)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from app import sum
>>> print(sum(1, 2))
3
7 để làm tự động. Hoặc nếu bạn muốn làm bằng tay.

classifiers: bổ sung thêm một số thông tin cho package. Như ví dụ trên, package chỉ tương thích với Python 2, under MIT lisense và là OS-independent. Danh sách classifiers bạn có thể tham khảo tại đây.