Trong bài viết này, chúng ta sẽ tìm hiểu về các gói trong Python. Các gói giúp chúng tôi cấu trúc các gói và mô-đun trong một hệ thống phân cấp có tổ chức. Hãy xem cách tạo các gói trong Python
Tạo gói
Chúng tôi đã bao gồm một __init__. py, tệp bên trong một thư mục để nói với Python rằng thư mục hiện tại là một gói. Bất cứ khi nào bạn muốn tạo một gói, thì bạn phải bao gồm __init__. tập tin py trong thư mục. Bạn có thể viết mã bên trong hoặc để trống tùy ý. Nó không làm phiền Python
Làm theo các bước dưới đây để tạo một gói trong Python
- Tạo một thư mục và bao gồm một __init__. py trong đó để nói với Python rằng thư mục hiện tại là một gói
- Bao gồm các gói phụ hoặc tệp khác mà bạn muốn
- Tiếp theo, truy cập chúng bằng các câu lệnh nhập hợp lệ
Hãy tạo một gói đơn giản có cấu trúc sau
Trọn gói [đại học]
- __trong đó__. py
- sinh viên. py
- khoa. py
Chuyển đến bất kỳ thư mục nào trong máy tính xách tay hoặc máy tính để bàn của bạn và tạo cấu trúc thư mục trên. Sau khi tạo cấu trúc thư mục trên, hãy bao gồm đoạn mã sau vào các tệp tương ứng
Ví dụ
# student.py class Student: def __init__[self, student]: self.name = student['name'] self.gender = student['gender'] self.year = student['year'] def get_student_details[self]: return f"Name: {self.name}\nGender: {self.gender}\nYear: {self.year}" # faculty.py class Faculty: def __init__[self, faculty]: self.name = faculty['name'] self.subject = faculty['subject'] def get_faculty_details[self]: return f"Name: {self.name}\nSubject: {self.subject}"
Chúng tôi có những điều trên trong học sinh. py và khoa. tập tin py. Hãy tạo một tệp khác để truy cập những tệp được phân loại bên trong nó. Bây giờ, bên trong thư mục gói tạo một tệp có tên là thử nghiệm. py và bao gồm đoạn mã sau
Ví dụ
# testing.py # importing the Student and Faculty classes from respective files from student import Student from faculty import Faculty # creating dicts for student and faculty student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} faculty_dict = {'name': 'Emma', 'subject': 'Programming'} # creating instances of the Student and Faculty classes student = Student[student_dict] faculty = Faculty[faculty_dict] # getting and printing the student and faculty details print[student.get_student_details[]] print[] print[faculty.get_faculty_details[]]
Nếu bạn chạy thử nghiệm. py, thì bạn sẽ nhận được kết quả như sau
đầu ra
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming
Chúng ta đã thấy cách tạo và truy cập một gói trong Python. Và đây là một gói đơn giản. Có thể có nhiều gói con và tệp bên trong một gói. Hãy xem cách truy cập các mô-đun gói con
Tạo một thư mục với cấu trúc sau
- Trọn gói [đại học]
- __trong đó__. py
- Gói phụ [sinh viên]
- thử nghiệm. py
Sao chép mã sinh viên ở trên và đặt nó ở đây. Bây giờ, hãy xem cách truy cập nó trong thử nghiệm. tập tin py. Thêm phần sau vào thử nghiệm. tập tin py
Ví dụ
# testing.py from student.main import Student # creating dicts for student student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} # creating instances of the Student class student = Student[student_dict] # getting and printing the student details print[student.get_student_details[]]
Nếu bạn chạy thử nghiệm. py, thì bạn sẽ nhận được kết quả như sau
đầu ra
Name: John Gender: Male Year: 3
Chúng tôi đã truy cập lớp Sinh viên từ chính. py bên trong gói con student sử dụng dấu chấm [. ]. Bạn có thể đi sâu hơn tùy thích dựa trên cấu trúc gói
Trong bài viết này, chúng ta sẽ tìm hiểu cách phát triển gói trong Python. Các gói không là gì ngoài một tập hợp các chương trình được thiết kế để thực hiện một [các] nhiệm vụ nhất định. Các gói có hai loại, cụ thể là
- Các gói tích hợp như bộ sưu tập, datetime, sqlite, v.v.
- Các gói bên ngoài như bình, django, tensorflow, v.v.
Tạo một gói
Đầu tiên, chúng ta cần nghĩ ra cách cấu trúc mã của mình để những người khác có thể truy cập các chức năng mã của chúng ta. Trong Python, để tạo một gói, chúng ta cần thêm __init__. py vào thư mục. Ở đây, chúng ta sẽ tạo một gói có tên là test_package
- Hãy viết __init__. py
Python3
________ 51 ________ 52 ________ 53 ________ 54
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files5
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files6
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files7
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files8
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files9
# testing.py # importing the Student and Faculty classes from respective files from student import Student from faculty import Faculty # creating dicts for student and faculty student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} faculty_dict = {'name': 'Emma', 'subject': 'Programming'} # creating instances of the Student and Faculty classes student = Student[student_dict] faculty = Faculty[faculty_dict] # getting and printing the student and faculty details print[student.get_student_details[]] print[] print[faculty.get_faculty_details[]]40
# testing.py # importing the Student and Faculty classes from respective files from student import Student from faculty import Faculty # creating dicts for student and faculty student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} faculty_dict = {'name': 'Emma', 'subject': 'Programming'} # creating instances of the Student and Faculty classes student = Student[student_dict] faculty = Faculty[faculty_dict] # getting and printing the student and faculty details print[student.get_student_details[]] print[] print[faculty.get_faculty_details[]]41
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files8
# testing.py # importing the Student and Faculty classes from respective files from student import Student from faculty import Faculty # creating dicts for student and faculty student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} faculty_dict = {'name': 'Emma', 'subject': 'Programming'} # creating instances of the Student and Faculty classes student = Student[student_dict] faculty = Faculty[faculty_dict] # getting and printing the student and faculty details print[student.get_student_details[]] print[] print[faculty.get_faculty_details[]]43
# testing.py # importing the Student and Faculty classes from respective files from student import Student from faculty import Faculty # creating dicts for student and faculty student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} faculty_dict = {'name': 'Emma', 'subject': 'Programming'} # creating instances of the Student and Faculty classes student = Student[student_dict] faculty = Faculty[faculty_dict] # getting and printing the student and faculty details print[student.get_student_details[]] print[] print[faculty.get_faculty_details[]]44
- Bây giờ, hãy tạo một thư mục có tên là test_package, và trong đó giữ __init__. tập tin py. Đó là tất cả, gói của chúng tôi đã sẵn sàng. Tất cả gói của chúng tôi đều đếm số lần xuất hiện của một từ trong danh sách. Bây giờ, để sử dụng gói của chúng tôi, hãy tạo một lần chạy. py bên ngoài thư mục test_package. Bên trong đường chạy. py chỉ cần nhập gói vừa tạo và sử dụng hàm đếm_in_list. Chúng ta có thể viết mã như hình dưới đây
Python3
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files1
# testing.py # importing the Student and Faculty classes from respective files from student import Student from faculty import Faculty # creating dicts for student and faculty student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} faculty_dict = {'name': 'Emma', 'subject': 'Programming'} # creating instances of the Student and Faculty classes student = Student[student_dict] faculty = Faculty[faculty_dict] # getting and printing the student and faculty details print[student.get_student_details[]] print[] print[faculty.get_faculty_details[]]46____53
# testing.py # importing the Student and Faculty classes from respective files from student import Student from faculty import Faculty # creating dicts for student and faculty student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} faculty_dict = {'name': 'Emma', 'subject': 'Programming'} # creating instances of the Student and Faculty classes student = Student[student_dict] faculty = Faculty[faculty_dict] # getting and printing the student and faculty details print[student.get_student_details[]] print[] print[faculty.get_faculty_details[]]48
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files5
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming40
# testing.py # importing the Student and Faculty classes from respective files from student import Student from faculty import Faculty # creating dicts for student and faculty student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} faculty_dict = {'name': 'Emma', 'subject': 'Programming'} # creating instances of the Student and Faculty classes student = Student[student_dict] faculty = Faculty[faculty_dict] # getting and printing the student and faculty details print[student.get_student_details[]] print[] print[faculty.get_faculty_details[]]40
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming42
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming43
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming44
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming45
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming44
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming43
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming48
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming49
# testing.py # importing the Student and Faculty classes from respective files from student import Student from faculty import Faculty # creating dicts for student and faculty student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} faculty_dict = {'name': 'Emma', 'subject': 'Programming'} # creating instances of the Student and Faculty classes student = Student[student_dict] faculty = Faculty[faculty_dict] # getting and printing the student and faculty details print[student.get_student_details[]] print[] print[faculty.get_faculty_details[]]40
# testing.py from student.main import Student # creating dicts for student student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} # creating instances of the Student class student = Student[student_dict] # getting and printing the student details print[student.get_student_details[]]51
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming43
# testing.py from student.main import Student # creating dicts for student student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} # creating instances of the Student class student = Student[student_dict] # getting and printing the student details print[student.get_student_details[]]53
# testing.py from student.main import Student # creating dicts for student student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} # creating instances of the Student class student = Student[student_dict] # getting and printing the student details print[student.get_student_details[]]54
# testing.py from student.main import Student # creating dicts for student student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} # creating instances of the Student class student = Student[student_dict] # getting and printing the student details print[student.get_student_details[]]55
- Đó là tất cả. Chúng tôi vừa tạo gói đầu tiên của mình. Cấu trúc thư mục của chúng ta sẽ giống như thế này
Name: John Gender: Male Year: 30
Để kiểm tra, chỉ cần gõ python3 run. py. Và đầu ra của chúng tôi sẽ như sau
Theo cách này, chúng tôi có thể tạo nhiều chương trình phức tạp và sử dụng chúng trong các mã khác
Tải lên gói của chúng tôi
Bây giờ, hãy xem cách chúng tôi có thể chuẩn bị gói của mình để triển khai trên PyPI. Chúng tôi cần một số tệp bổ sung trong thư mục gói của mình, như README. MD, GIẤY PHÉP và thiết lập. py. Mã của chúng tôi nên được cấu trúc như dưới đây
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files
Đảm bảo xóa lần chạy đã tạo trước đó. py, nó chỉ dành cho thử nghiệm thủ công. Đối với GIẤY PHÉP, chúng tôi khuyên bạn nên sử dụng GIẤY PHÉP MIT vì nó mang lại sự linh hoạt nhất. Chúng ta có thể đọc về nhiều loại giấy phép trực tuyến
Tiếp theo, tạo README. tập tin md. Về cơ bản, nó sẽ chứa một mô tả đầy đủ về gói. Nếu chúng ta chưa quen với cách viết theo phong cách markdown, chúng tôi khuyên bạn nên đọc bài viết này. Một lần, chúng tôi có README của bạn. md đã sẵn sàng, chúng ta cần viết thiết lập. py. đây là phần quan trọng nhất
Python3
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files3
# testing.py from student.main import Student # creating dicts for student student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} # creating instances of the Student class student = Student[student_dict] # getting and printing the student details print[student.get_student_details[]]57
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files5
# testing.py from student.main import Student # creating dicts for student student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} # creating instances of the Student class student = Student[student_dict] # getting and printing the student details print[student.get_student_details[]]59
Name: John Gender: Male Year: 360
Name: John Gender: Male Year: 361
Name: John Gender: Male Year: 362
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming44
Name: John Gender: Male Year: 364
Name: John Gender: Male Year: 365
Name: John Gender: Male Year: 366
Name: John Gender: Male Year: 367
# testing.py # importing the Student and Faculty classes from respective files from student import Student from faculty import Faculty # creating dicts for student and faculty student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} faculty_dict = {'name': 'Emma', 'subject': 'Programming'} # creating instances of the Student and Faculty classes student = Student[student_dict] faculty = Faculty[faculty_dict] # getting and printing the student and faculty details print[student.get_student_details[]] print[] print[faculty.get_faculty_details[]]40
Name: John Gender: Male Year: 369
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files5
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files71
Name: John Gender: Male Year: 366
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files73____140
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files75
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming44
Name: John Gender: Male Year: 366
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files78
# testing.py # importing the Student and Faculty classes from respective files from student import Student from faculty import Faculty # creating dicts for student and faculty student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} faculty_dict = {'name': 'Emma', 'subject': 'Programming'} # creating instances of the Student and Faculty classes student = Student[student_dict] faculty = Faculty[faculty_dict] # getting and printing the student and faculty details print[student.get_student_details[]] print[] print[faculty.get_faculty_details[]]40
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files60
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming44
Name: John Gender: Male Year: 366
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files63
# testing.py # importing the Student and Faculty classes from respective files from student import Student from faculty import Faculty # creating dicts for student and faculty student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} faculty_dict = {'name': 'Emma', 'subject': 'Programming'} # creating instances of the Student and Faculty classes student = Student[student_dict] faculty = Faculty[faculty_dict] # getting and printing the student and faculty details print[student.get_student_details[]] print[] print[faculty.get_faculty_details[]]40
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files65
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming44
Name: John Gender: Male Year: 366
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files68
# testing.py # importing the Student and Faculty classes from respective files from student import Student from faculty import Faculty # creating dicts for student and faculty student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} faculty_dict = {'name': 'Emma', 'subject': 'Programming'} # creating instances of the Student and Faculty classes student = Student[student_dict] faculty = Faculty[faculty_dict] # getting and printing the student and faculty details print[student.get_student_details[]] print[] print[faculty.get_faculty_details[]]40
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files60
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming44
Name: John Gender: Male Year: 366
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files63
# testing.py # importing the Student and Faculty classes from respective files from student import Student from faculty import Faculty # creating dicts for student and faculty student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} faculty_dict = {'name': 'Emma', 'subject': 'Programming'} # creating instances of the Student and Faculty classes student = Student[student_dict] faculty = Faculty[faculty_dict] # getting and printing the student and faculty details print[student.get_student_details[]] print[] print[faculty.get_faculty_details[]]40
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming42
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files66
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files67
Name: John Gender: Male Year: 366
Name: John Gender: Male Year: 367
# testing.py # importing the Student and Faculty classes from respective files from student import Student from faculty import Faculty # creating dicts for student and faculty student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} faculty_dict = {'name': 'Emma', 'subject': 'Programming'} # creating instances of the Student and Faculty classes student = Student[student_dict] faculty = Faculty[faculty_dict] # getting and printing the student and faculty details print[student.get_student_details[]] print[] print[faculty.get_faculty_details[]]40
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files71
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming44
Name: John Gender: Male Year: 366
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files74
# testing.py # importing the Student and Faculty classes from respective files from student import Student from faculty import Faculty # creating dicts for student and faculty student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} faculty_dict = {'name': 'Emma', 'subject': 'Programming'} # creating instances of the Student and Faculty classes student = Student[student_dict] faculty = Faculty[faculty_dict] # getting and printing the student and faculty details print[student.get_student_details[]] print[] print[faculty.get_faculty_details[]]40
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files76
Name: John Gender: Male Year: 366
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files78
# testing.py # importing the Student and Faculty classes from respective files from student import Student from faculty import Faculty # creating dicts for student and faculty student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} faculty_dict = {'name': 'Emma', 'subject': 'Programming'} # creating instances of the Student and Faculty classes student = Student[student_dict] faculty = Faculty[faculty_dict] # getting and printing the student and faculty details print[student.get_student_details[]] print[] print[faculty.get_faculty_details[]]40
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files00
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming44
Name: John Gender: Male Year: 366
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files03____140
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files05
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming44
Name: John Gender: Male Year: 366
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files08
# testing.py # importing the Student and Faculty classes from respective files from student import Student from faculty import Faculty # creating dicts for student and faculty student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} faculty_dict = {'name': 'Emma', 'subject': 'Programming'} # creating instances of the Student and Faculty classes student = Student[student_dict] faculty = Faculty[faculty_dict] # getting and printing the student and faculty details print[student.get_student_details[]] print[] print[faculty.get_faculty_details[]]40
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files10
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming44
Name: John Gender: Male Year: 366
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files13____140
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files15
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming44
Name: John Gender: Male Year: 366
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files18
# testing.py # importing the Student and Faculty classes from respective files from student import Student from faculty import Faculty # creating dicts for student and faculty student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} faculty_dict = {'name': 'Emma', 'subject': 'Programming'} # creating instances of the Student and Faculty classes student = Student[student_dict] faculty = Faculty[faculty_dict] # getting and printing the student and faculty details print[student.get_student_details[]] print[] print[faculty.get_faculty_details[]]40
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files20
# testing.py from student.main import Student # creating dicts for student student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} # creating instances of the Student class student = Student[student_dict] # getting and printing the student details print[student.get_student_details[]]53
Trong đoạn mã trên, bạn cần thay đổi
- tác giả với tên của bạn
- author_email bằng email của bạn
- url với URL GitHub của gói
Gói của chúng tôi đã sẵn sàng
Đăng ký gói của chúng tôi với PyPI
Bây giờ chúng tôi đã phát triển gói python của mình, chúng tôi cần đăng ký gói đó trên PyPI
1. Tải lên GitHub
Tạo một kho lưu trữ GitHub mới và đẩy tất cả mã của chúng tôi vào đó. Nếu bạn chưa biết cách đẩy mã lên repo GitHub, bạn có thể xem qua bài viết này. Ngoài ra, đừng quên cập nhật URL của chúng tôi trong phần thiết lập. py với URL repo GitHub mới được tạo. Repo của chúng tôi nên được truy cập công khai
2. Tạo một tài khoản trong PyPI
Chúng tôi sẽ xuất bản gói trong PyPI, chúng tôi cần một tài khoản. Để làm điều đó, chỉ cần truy cập PyPI và tạo tài khoản của bạn
3. Tạo bản phân phối
Lưu trữ phân phối là cần thiết để lưu trữ nó dưới dạng gói. Để tạo các gói này, chúng tôi cần cài đặt hai gói bổ sung
# testing.py # importing the Student and Faculty classes from respective files from student import Student from faculty import Faculty # creating dicts for student and faculty student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} faculty_dict = {'name': 'Emma', 'subject': 'Programming'} # creating instances of the Student and Faculty classes student = Student[student_dict] faculty = Faculty[faculty_dict] # getting and printing the student and faculty details print[student.get_student_details[]] print[] print[faculty.get_faculty_details[]]4
Bây giờ chúng tôi đã cài đặt chúng, hãy chạy lệnh bên dưới để tạo tệp lưu trữ
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming4
Nó sẽ tạo các thư mục build, dist, test_package. trứng-thông tin. Bây giờ cấu trúc thư mục của bạn sẽ giống như thế này
# testing.py from student.main import Student # creating dicts for student student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} # creating instances of the Student class student = Student[student_dict] # getting and printing the student details print[student.get_student_details[]]5
4. Triển khai
Để tải lên PyPI, chúng tôi cần cài đặt gói sợi xe
Name: John Gender: Male Year: 36
Bây giờ, hãy tải lên gói có phiên bản phù hợp được xác định trong phần thiết lập. tập tin py. Để làm như vậy, hãy chạy lệnh dưới đây
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files7
Đó là tất cả về việc phát triển và triển khai các gói trong python
Sử dụng gói của chúng tôi
Khi gói được phát triển, chúng ta sẽ có thể sử dụng nó phải không? . Tạo một thư mục hoàn toàn mới và tạo một môi trường ảo bên trong nó. Để như vậy, chỉ cần gõ
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files6
Bây giờ, hãy cài đặt gói mới được triển khai của bạn vào môi trường ảo. Bạn cần sử dụng pip để có thể cài đặt nó
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files6
Bây giờ, hãy xem cách chúng ta có thể sử dụng gói này trong mã của mình. ý tưởng thật đơn giản. Đây là sau khi tất cả một gói, phải không?
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files7
Bây giờ, chúng tôi có chức năng của chúng tôi. Hãy thử sử dụng nó
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files0
Đó là tất cả. Nếu chúng ta đặt tất cả lại với nhau,
Python3
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files1
# testing.py # importing the Student and Faculty classes from respective files from student import Student from faculty import Faculty # creating dicts for student and faculty student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} faculty_dict = {'name': 'Emma', 'subject': 'Programming'} # creating instances of the Student and Faculty classes student = Student[student_dict] faculty = Faculty[faculty_dict] # getting and printing the student and faculty details print[student.get_student_details[]] print[] print[faculty.get_faculty_details[]]46____53
# testing.py # importing the Student and Faculty classes from respective files from student import Student from faculty import Faculty # creating dicts for student and faculty student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} faculty_dict = {'name': 'Emma', 'subject': 'Programming'} # creating instances of the Student and Faculty classes student = Student[student_dict] faculty = Faculty[faculty_dict] # getting and printing the student and faculty details print[student.get_student_details[]] print[] print[faculty.get_faculty_details[]]48
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files5
# testing.py from student.main import Student # creating dicts for student student_dict = {'name' : 'John', 'gender': 'Male', 'year': '3'} # creating instances of the Student class student = Student[student_dict] # getting and printing the student details print[student.get_student_details[]]54
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files28
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming43
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming44
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming45
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming44
Name: John Gender: Male Year: 3 Name: Emma Subject: Programming43
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files67
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files46
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files36
package-folder ├── LICENSE ├── README.md ├── setup.py └── test_package └── __init__.py 1 directory, 4 files48