Các bước để tạo gói thư viện trong python là gì?

Mô-đun có thể chứa các hàm, như đã được mô tả, nhưng cũng có các biến thuộc mọi loại (mảng, từ điển, đối tượng, v.v.)

Thí dụ

Lưu mã này vào tệp

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
7

person1 = {
  "tên". "John",
  "tuổi". 36,
  "quốc gia". "Na Uy"
}

Thí dụ

Nhập mô-đun có tên mymodule và truy cập từ điển person1

nhập mymodule

a = mymodule. person1["age"]
print(a)

Chạy ví dụ »



Đặt tên cho một mô-đun

Bạn có thể đặt tên cho tệp mô-đun bất cứ điều gì bạn thích, nhưng nó phải có phần mở rộng tệp là

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
8

Đổi tên một Module

Bạn có thể tạo bí danh khi nhập mô-đun bằng cách sử dụng từ khóa

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
9

Thí dụ

Tạo bí danh cho

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
00 có tên là
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
01

nhập mymodule dưới dạng mx

a = mx. person1["age"]
print(a)

Chạy ví dụ »


Mô-đun tích hợp

Có một số mô-đun tích hợp sẵn trong Python mà bạn có thể nhập bất cứ khi nào bạn muốn

Thí dụ

Nhập và sử dụng mô-đun

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
02

nền tảng nhập khẩu

x = nền tảng. system()
print(x)

Tự mình thử »


Sử dụng hàm dir()

Có một hàm tích hợp để liệt kê tất cả các tên hàm (hoặc tên biến) trong một mô-đun. Hàm

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
03

Thí dụ

Liệt kê tất cả các tên được xác định thuộc về mô-đun nền tảng

nền tảng nhập khẩu

x = dir(nền tảng)
print(x)

Tự mình thử »

Ghi chú. Hàm dir() có thể được sử dụng trên tất cả các mô-đun, kể cả những mô-đun bạn tự tạo


Nhập Từ Mô-đun

Bạn có thể chọn chỉ nhập các bộ phận từ một mô-đun bằng cách sử dụng từ khóa

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
70

Thí dụ

Mô-đun có tên

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
00 có một chức năng và một từ điển

lời chào chắc chắn(tên).
  print("Xin chào, " + tên)

person1 = {
  "tên". "John",
  "tuổi". 36,
  "quốc gia". "Na Uy"
}

Thí dụ

Chỉ nhập từ điển person1 từ mô-đun

từ mymodule nhập person1

in (person1["tuổi"])

Chạy ví dụ »

Ghi chú. Khi nhập bằng từ khóa

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
70, không sử dụng tên mô-đun khi đề cập đến các phần tử trong mô-đun. Thí dụ.
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
73, không phải
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
74

Khi bạn tạo một tệp Python, bạn đang tạo một mô-đun Python. Bất kỳ tệp Python nào bạn tạo đều có thể được nhập bởi tập lệnh Python khác. Do đó, theo định nghĩa, nó cũng là một mô-đun Python. Nếu bạn có hai hoặc nhiều tệp Python liên quan, thì bạn có thể có gói Python

Một số tổ chức giữ tất cả mã của họ cho chính họ. Điều này được gọi là mã nguồn đóng. Python là một ngôn ngữ nguồn mở và hầu hết các mô-đun và gói Python mà bạn có thể lấy từ Chỉ mục gói Python (PyPI) đều miễn phí và cũng là nguồn mở. Một trong những cách nhanh nhất để chia sẻ gói hoặc mô-đun của bạn là tải nó lên Python Package Index hoặc Github hoặc cả hai

Trong bài viết này, bạn sẽ tìm hiểu về các chủ đề sau

  • Tạo một mô-đun
  • Tạo một gói
  • Đóng gói một dự án cho PyPI
  • Tạo tệp dự án
  • Tạo
    # test_arithmetic.py
    
    import arithmetic
    import unittest
    
    class TestArithmetic(unittest.TestCase):
    
        def test_addition(self):
            self.assertEqual(arithmetic.add(1, 2), 3)
    
        def test_subtraction(self):
            self.assertEqual(arithmetic.subtract(2, 1), 1)
    
        def test_multiplication(self):
            self.assertEqual(arithmetic.multiply(5, 5), 25)
    
        def test_division(self):
            self.assertEqual(arithmetic.divide(8, 2), 4)
    
    if __name__ == '__main__':
        unittest.main()
    
    5
  • Đang tải lên PyPI

Bước đầu tiên trong quy trình là hiểu việc tạo mô-đun có thể tái sử dụng trông như thế nào. Bắt đầu nào

Tạo một mô-đun

Bất kỳ tệp Python nào bạn tạo đều là mô-đun mà bạn có thể nhập. Bạn có thể dùng thử với một số ví dụ từ cuốn sách này bằng cách thêm một tệp mới vào bất kỳ thư mục mã nào của bài viết và cố gắng nhập một trong các mô-đun ở đó. Ví dụ: nếu bạn có một tệp Python có tên là

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
6 và sau đó tạo một tệp mới có tên là
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
7, bạn có thể nhập
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
8 vào
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
9 thông qua việc sử dụng
$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
0

Tất nhiên, đó là một ví dụ ngớ ngẩn. Thay vào đó, bạn sẽ tạo một mô-đun đơn giản có một số hàm số học cơ bản trong đó. Bạn có thể đặt tên tệp là

$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
1 và thêm mã này vào đó

# arithmetic.py

def add(x, y):
    return x + y

def divide(x, y):
    return x / y

def multiply(x, y):
    return x * y

def subtract(x, y):
    return x - y

Mã này rất ngây thơ. Bạn không có xử lý lỗi nào cả, chẳng hạn. Điều đó có nghĩa là bạn có thể chia cho 0 và gây ra một ngoại lệ. Bạn cũng có thể chuyển các loại không tương thích cho các hàm này, chẳng hạn như một chuỗi và một số nguyên -- điều đó sẽ gây ra một loại ngoại lệ khác được nêu ra

Tuy nhiên, đối với mục đích học tập, mã này là đủ. Bạn có thể chứng minh rằng nó có thể nhập được bằng cách tạo một tệp thử nghiệm. Tạo một tệp mới có tên

$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
2 và thêm mã này vào đó

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()

Lưu bài kiểm tra của bạn trong cùng thư mục với mô-đun của bạn. Bây giờ bạn có thể chạy mã này bằng lệnh sau

$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK

Điều này chứng tỏ rằng bạn có thể nhập

$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
1 dưới dạng mô-đun. Các thử nghiệm này cũng cho thấy chức năng cơ bản của mã hoạt động. Bạn có thể nâng cao các kiểm tra này bằng cách kiểm tra phép chia cho 0 và trộn các chuỗi và số nguyên. Những loại thử nghiệm đó hiện sẽ thất bại. Khi bạn có một bài kiểm tra không đạt, bạn có thể làm theo phương pháp Phát triển theo hướng kiểm tra để khắc phục sự cố

Bây giờ hãy tìm hiểu cách tạo một gói Python

Tạo một gói

Gói Python là một hoặc nhiều tệp mà bạn định chia sẻ với người khác, thường bằng cách tải tệp đó lên Chỉ mục gói Python (PyPI). Các gói thường được tạo bằng cách đặt tên cho một thư mục chứa các tệp thay vì đặt tên cho chính tệp đó. Sau đó, bên trong thư mục đó, bạn sẽ có một tệp

$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
4 đặc biệt. Khi Python nhìn thấy tệp
$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
4, nó biết rằng thư mục có thể nhập được dưới dạng gói

Có một số cách để chuyển đổi

$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
1 thành một gói. Đơn giản nhất là chuyển mã từ
$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
1 sang
$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
8

  • tạo thư mục
    $ python3 test_arithmetic.py 
    ....
    ----------------------------------------------------------------------
    Ran 4 tests in 0.000s
    
    OK
    
    9
  • di chuyển/sao chép
    $ python3 test_arithmetic.py 
    ....
    ----------------------------------------------------------------------
    Ran 4 tests in 0.000s
    
    OK
    
    1 đến
    $ python3 test_arithmetic.py 
    ....
    ----------------------------------------------------------------------
    Ran 4 tests in 0.000s
    
    OK
    
    8
  • nếu bạn đã sử dụng "bản sao" ở bước trước thì hãy xóa
    $ python3 test_arithmetic.py 
    ....
    ----------------------------------------------------------------------
    Ran 4 tests in 0.000s
    
    OK
    
    1
  • chạy
    $ python3 test_arithmetic.py 
    ....
    ----------------------------------------------------------------------
    Ran 4 tests in 0.000s
    
    OK
    
    2

Bước cuối cùng đó là vô cùng quan trọng. Nếu các bài kiểm tra của bạn vẫn vượt qua thì bạn biết chuyển đổi của mình từ mô-đun sang gói đã hoạt động. Để kiểm tra gói của bạn, hãy mở Dấu nhắc lệnh nếu bạn đang dùng Windows hoặc thiết bị đầu cuối nếu bạn đang dùng Mac hoặc Linux. Sau đó điều hướng đến thư mục chứa thư mục

$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
9, nhưng không phải bên trong thư mục đó. Bây giờ bạn sẽ ở trong cùng thư mục với tệp
$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
2 của mình. Tại thời điểm này, bạn có thể chạy
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
46 và xem nỗ lực của mình có thành công không

Có vẻ ngớ ngẩn khi chỉ đặt tất cả mã của bạn vào một tệp

$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
4 duy nhất, nhưng điều đó thực sự hoạt động tốt đối với các tệp lên tới vài nghìn dòng

Cách thứ hai để chuyển đổi

$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
1 thành một gói tương tự như cách thứ nhất, nhưng liên quan đến việc sử dụng nhiều tệp hơn là chỉ
$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
4. Trong mã thực, các chức năng/lớp/v.v. trong mỗi tệp sẽ được nhóm theo cách nào đó -- có lẽ một tệp cho tất cả các ngoại lệ tùy chỉnh của gói của bạn, một tệp cho các tiện ích chung và một tệp cho chức năng chính

Ví dụ của chúng tôi, bạn sẽ chỉ chia bốn hàm trong

$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
1 thành các tệp riêng của chúng. Hãy tiếp tục và di chuyển từng chức năng từ
$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
4 vào tệp riêng của nó. Cấu trúc thư mục của bạn sẽ trông như thế này

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
4

Đối với tệp

$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
4, bạn có thể thêm đoạn mã sau

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
2

Bây giờ bạn đã thực hiện những thay đổi này, bước tiếp theo của bạn là gì? . " Nếu bài kiểm tra của bạn vẫn vượt qua thì bạn chưa phá vỡ API của mình

Ngay bây giờ, gói

$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
9 của bạn chỉ khả dụng cho mã Python khác của bạn nếu bạn ở trong cùng thư mục với tệp
$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
2 của mình. Để làm cho nó khả dụng trong phiên Python của bạn hoặc trong mã Python khác, bạn có thể sử dụng mô-đun
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
25 của Python để thêm gói của bạn vào đường dẫn tìm kiếm Python. Đường dẫn tìm kiếm là những gì Python sử dụng để tìm các mô-đun khi bạn sử dụng từ khóa
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
26. Bạn có thể xem những đường dẫn mà Python tìm kiếm bằng cách in ra
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
27

Hãy giả sử rằng thư mục

$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
9 của bạn ở vị trí này.
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
29. Để thêm nó vào đường dẫn tìm kiếm của Python, bạn có thể làm điều này

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
0

Thao tác này sẽ thêm

$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
9 vào đường dẫn của Python để bạn có thể nhập và sau đó sử dụng gói trong mã của mình. Tuy nhiên, điều đó thực sự khó xử. Sẽ thật tuyệt nếu bạn có thể cài đặt gói của mình bằng cách sử dụng
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
01 để bạn không phải loay hoay với đường dẫn mọi lúc

Hãy tìm hiểu làm thế nào để làm điều đó tiếp theo

Đóng gói một dự án cho PyPI

Khi nói đến việc tạo một gói cho Chỉ mục gói Python (PyPI), bạn sẽ cần một số tệp bổ sung. Có một hướng dẫn hay về quy trình tạo và tải gói lên PyPI tại đây

Hướng dẫn đóng gói chính thức khuyên bạn nên thiết lập cấu trúc thư mục như thế này

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
3

Thư mục

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
02 có thể để trống. Đây là thư mục nơi bạn sẽ bao gồm các bài kiểm tra cho gói của mình. Hầu hết các nhà phát triển sử dụng khung công tác
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
03 hoặc
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
04 của Python cho các thử nghiệm của họ. Đối với ví dụ này, bạn có thể để trống thư mục

Hãy tiếp tục và tìm hiểu về các tệp khác mà bạn cần tạo trong phần tiếp theo

Tạo tệp dự án

Tệp GIẤY PHÉP là nơi bạn đề cập đến giấy phép mà gói của bạn có. Điều này cho người dùng biết những gì họ có thể và không thể làm với gói của bạn. Có rất nhiều giấy phép khác nhau mà bạn có thể sử dụng. Giấy phép GPL và MIT chỉ là một vài ví dụ phổ biến

ĐỌC. md là một mô tả về dự án của bạn, được viết bằng Markdown. Bạn sẽ muốn viết về dự án của mình trong tệp này và bao gồm bất kỳ thông tin nào về các phụ thuộc mà nó có thể cần. Bạn có thể hướng dẫn cài đặt cũng như ví dụ sử dụng gói của bạn. Markdown khá linh hoạt và thậm chí cho phép bạn đánh dấu cú pháp

Tệp khác bạn cần cung cấp là

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
5. Tệp đó phức tạp hơn, vì vậy bạn sẽ tìm hiểu về tệp đó trong phần tiếp theo

Tạo # test_arithmetic.py import arithmetic import unittest class TestArithmetic(unittest.TestCase): def test_addition(self): self.assertEqual(arithmetic.add(1, 2), 3) def test_subtraction(self): self.assertEqual(arithmetic.subtract(2, 1), 1) def test_multiplication(self): self.assertEqual(arithmetic.multiply(5, 5), 25) def test_division(self): self.assertEqual(arithmetic.divide(8, 2), 4) if __name__ == '__main__': unittest.main() 5

Có một tệp đặc biệt tên là

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
5 được sử dụng làm tập lệnh xây dựng cho các bản phân phối Python. Nó được sử dụng bởi
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
08, tòa nhà thực tế cho bạn. Nếu bạn muốn biết thêm về
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
08, thì bạn nên xem phần sau

Bạn có thể sử dụng

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
5 để tạo bánh xe Python. Bánh xe là một kho lưu trữ định dạng ZIP với tên được định dạng đặc biệt và phần mở rộng
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
31. Nó chứa mọi thứ cần thiết để cài đặt gói của bạn. Bạn có thể coi nó như một phiên bản nén của mã mà
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
01 có thể giải nén và cài đặt cho bạn. Bánh xe tuân theo PEP 376, bạn có thể đọc về nó tại đây

Khi bạn đã đọc xong tất cả tài liệu đó (nếu muốn), bạn có thể tạo

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
5 của mình và thêm mã này vào đó

$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
6

Bước đầu tiên trong mã này là nhập

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
08. Sau đó, bạn đọc trong tệp
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
35 của mình thành một biến mà bạn sẽ sớm sử dụng. Bit cuối cùng là phần lớn mã. Ở đây bạn gọi
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
36, có thể nhận khá nhiều đối số khác nhau. Ví dụ trên chỉ là một ví dụ mẫu về những gì bạn có thể chuyển đến chức năng này. Để xem danh sách đầy đủ, bạn cần vào đây

Hầu hết các đối số là tự giải thích. Hãy tập trung vào những cái khó hiểu hơn. Các đối số

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
37 là danh sách các gói cần thiết cho gói của bạn. Trong trường hợp này, bạn sử dụng
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
38 để tự động tìm các gói cần thiết cho bạn. Đối số
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
39 được sử dụng để chuyển siêu dữ liệu bổ sung tới
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
01. Ví dụ: đoạn mã này cho
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
01 biết rằng gói này tương thích với Python 3

Bây giờ bạn đã có một

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
5, bạn đã sẵn sàng để tạo bánh xe Python

Tạo bánh xe Python

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
5 được sử dụng để tạo bánh xe Python. Bạn nên đảm bảo rằng mình đã cài đặt phiên bản mới nhất của
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
08 và
$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
65, vì vậy trước khi tạo bánh xe của riêng mình, bạn nên chạy lệnh sau

$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
9

Điều này sẽ cập nhật các gói nếu có phiên bản mới hơn phiên bản bạn hiện đã cài đặt. Bây giờ bạn đã sẵn sàng để tự tạo một bánh xe. Mở Dấu nhắc lệnh hoặc ứng dụng đầu cuối và điều hướng đến thư mục chứa tệp

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
5 của bạn. Sau đó chạy lệnh sau

# arithmetic.py

def add(x, y):
    return x + y

def divide(x, y):
    return x / y

def multiply(x, y):
    return x * y

def subtract(x, y):
    return x - y
1

Lệnh này sẽ xuất ra rất nhiều văn bản, nhưng sau khi hoàn thành, bạn sẽ tìm thấy một thư mục mới có tên

$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
67 chứa hai tệp sau

  • $ python3 test_arithmetic.py 
    ....
    ----------------------------------------------------------------------
    Ran 4 tests in 0.000s
    
    OK
    
    68
  • $ python3 test_arithmetic.py 
    ....
    ----------------------------------------------------------------------
    Ran 4 tests in 0.000s
    
    OK
    
    69

$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
90 là một kho lưu trữ nguồn, có nghĩa là nó chứa mã nguồn Python cho gói của bạn bên trong nó. Người dùng của bạn có thể sử dụng kho lưu trữ nguồn để xây dựng gói trên máy của riêng họ, nếu họ cần. Định dạng
$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
91 là một kho lưu trữ được sử dụng bởi
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
01 để cài đặt gói của bạn trên máy của người dùng

Bạn có thể cài đặt bánh xe trực tiếp bằng cách sử dụng

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
01, nếu bạn muốn

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
0

Nhưng phương pháp thông thường sẽ là tải gói của bạn lên Chỉ mục gói Python (PyPI) và sau đó cài đặt nó. Tiếp theo, hãy khám phá cách nhận gói tuyệt vời của bạn trên PyPI

Đang tải lên PyPI

Bước đầu tiên để tải một gói lên PyPI là tạo một tài khoản trên Test PyPI. Điều này cho phép bạn kiểm tra xem gói của bạn có thể được tải lên máy chủ thử nghiệm và cài đặt từ máy chủ thử nghiệm đó không. Để tạo tài khoản, hãy truy cập URL sau và làm theo các bước trên trang đó

Bây giờ bạn cần tạo mã thông báo API PyPI. Điều này sẽ cho phép bạn tải gói lên một cách an toàn. Để nhận mã thông báo API, bạn cần truy cập vào đây

Bạn có thể giới hạn phạm vi của mã thông báo. Tuy nhiên, bạn không cần phải làm điều đó cho mã thông báo này vì bạn đang tạo nó cho một dự án mới. Đảm bảo bạn sao chép mã thông báo và lưu nó ở đâu đó TRƯỚC KHI đóng trang. Sau khi đóng trang, bạn không thể truy xuất lại mã thông báo. Thay vào đó, bạn sẽ được yêu cầu tạo mã thông báo mới

Bây giờ bạn đã đăng ký và có mã thông báo API, bạn sẽ cần lấy gói

$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
94. Bạn sẽ sử dụng
$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
94 để tải gói của mình lên PyPI. Để cài đặt
$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
94, bạn có thể sử dụng
# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
01 như thế này

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
1

Sau khi cài đặt, bạn có thể tải gói của mình lên Test PyPI bằng lệnh sau

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
2

Lưu ý rằng bạn sẽ cần chạy lệnh này từ trong thư mục chứa tệp

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
5 vì nó đang sao chép tất cả các tệp trong thư mục
$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
67 sang Kiểm tra PyPI. Khi bạn chạy lệnh này, nó sẽ nhắc bạn nhập tên người dùng và mật khẩu. Đối với tên người dùng, bạn cần sử dụng
# arithmetic.py

def add(x, y):
    return x + y

def divide(x, y):
    return x / y

def multiply(x, y):
    return x * y

def subtract(x, y):
    return x - y
10. Mật khẩu là giá trị mã thông báo có tiền tố là
# arithmetic.py

def add(x, y):
    return x + y

def divide(x, y):
    return x / y

def multiply(x, y):
    return x * y

def subtract(x, y):
    return x - y
11

Khi lệnh này chạy, bạn sẽ thấy đầu ra tương tự như sau

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
3

Tại thời điểm này, bây giờ bạn có thể xem gói của mình trên Test PyPI tại URL sau

Bây giờ bạn có thể thử cài đặt gói của mình từ Test PyPI bằng cách sử dụng lệnh sau

# test_arithmetic.py

import arithmetic
import unittest

class TestArithmetic(unittest.TestCase):

    def test_addition(self):
        self.assertEqual(arithmetic.add(1, 2), 3)

    def test_subtraction(self):
        self.assertEqual(arithmetic.subtract(2, 1), 1)

    def test_multiplication(self):
        self.assertEqual(arithmetic.multiply(5, 5), 25)

    def test_division(self):
        self.assertEqual(arithmetic.divide(8, 2), 4)

if __name__ == '__main__':
    unittest.main()
4

Nếu mọi thứ hoạt động bình thường, bây giờ bạn sẽ cài đặt gói

$ python3 test_arithmetic.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK
9 trên hệ thống của mình. Tất nhiên, hướng dẫn này chỉ cho bạn cách đóng gói mọi thứ cho Test PyPI. Khi bạn đã xác minh rằng nó hoạt động, thì bạn sẽ cần thực hiện các thao tác sau để cài đặt vào PyPI thực

  • Chọn một tên đáng nhớ và độc đáo cho gói
  • Đăng ký tài khoản tại https. //pypi. tổ chức
  • Sử dụng
    # arithmetic.py
    
    def add(x, y):
        return x + y
    
    def divide(x, y):
        return x / y
    
    def multiply(x, y):
        return x * y
    
    def subtract(x, y):
        return x - y
    
    13 để tải gói của bạn lên và nhập thông tin đăng nhập cho tài khoản bạn đã đăng ký trên PyPI thực. Bạn sẽ không cần sử dụng cờ
    # arithmetic.py
    
    def add(x, y):
        return x + y
    
    def divide(x, y):
        return x / y
    
    def multiply(x, y):
        return x * y
    
    def subtract(x, y):
        return x - y
    
    14 khi tải lên PyPI thực vì máy chủ đó là mặc định
  • Cài đặt gói của bạn từ PyPI thực bằng cách sử dụng
    # arithmetic.py
    
    def add(x, y):
        return x + y
    
    def divide(x, y):
        return x / y
    
    def multiply(x, y):
        return x * y
    
    def subtract(x, y):
        return x - y
    
    15

Bây giờ bạn đã biết cách phân phối gói do chính bạn tạo trên Chỉ mục gói Python

kết thúc

Các mô-đun và gói Python là những gì bạn nhập vào chương trình của mình. Theo nhiều cách, chúng là các khối xây dựng chương trình của bạn. Trong bài viết này, bạn đã tìm hiểu về những điều sau đây

  • Tạo một mô-đun
  • Tạo một gói
  • Đóng gói một dự án cho PyPI
  • Tạo tệp dự án
  • Tạo
    # test_arithmetic.py
    
    import arithmetic
    import unittest
    
    class TestArithmetic(unittest.TestCase):
    
        def test_addition(self):
            self.assertEqual(arithmetic.add(1, 2), 3)
    
        def test_subtraction(self):
            self.assertEqual(arithmetic.subtract(2, 1), 1)
    
        def test_multiplication(self):
            self.assertEqual(arithmetic.multiply(5, 5), 25)
    
        def test_division(self):
            self.assertEqual(arithmetic.divide(8, 2), 4)
    
    if __name__ == '__main__':
        unittest.main()
    
    5
  • Đang tải lên PyPI

Tại thời điểm này, bạn không chỉ biết mô-đun và gói là gì mà còn biết cách phân phối chúng thông qua Chỉ mục gói Python. Bây giờ bạn và bất kỳ nhà phát triển Python nào khác có thể tải xuống và cài đặt các gói của bạn. Xin chúc mừng. Bây giờ bạn là người bảo trì gói

đọc liên quan

Bài viết này dựa trên một chương trong Python 101, Phiên bản thứ 2, mà bạn có thể mua trên Leanpub hoặc Amazon

Quy trình tạo gói thư viện riêng trong Python Lớp 12 là gì?

Làm theo các bước sau để tạo Gói Python. .
Quyết định tên cho các gói, thư mục và. py (Không sử dụng bất kỳ dấu tách từ nào ngoại trừ dấu gạch dưới)
Tạo các thư mục cho gói của bạn và đặt tên thích hợp cho chúng
Tạo _init_. py trong các gói và thư mục con, nếu cần

Gói thư viện trong Python là gì?

Thư viện Python là tập hợp các mô-đun liên quan . Nó chứa các gói mã có thể được sử dụng nhiều lần trong các chương trình khác nhau. Nó làm cho Lập trình Python đơn giản và thuận tiện hơn cho lập trình viên. Vì chúng ta không cần phải viết đi viết lại cùng một mã cho các chương trình khác nhau.