Giới thiệu về Python
Nắm vững kiến thức cơ bản về phân tích dữ liệu với Python chỉ trong bốn giờ. Khóa học trực tuyến này sẽ giới thiệu giao diện Python và khám phá các gói phổ biến
Python trung cấp
Nâng cao kỹ năng khoa học dữ liệu của bạn bằng cách tạo trực quan hóa bằng Matplotlib và thao tác với DataFrames bằng gấu trúcCó liên quan
Dữ liệu văn bản trong Python Cheat Sheet
Chào mừng bạn đến với bảng gian lận của chúng tôi để làm việc với dữ liệu văn bản trong Python. Chúng tôi đã biên soạn một danh sách các hàm và gói hữu ích nhất để dọn dẹp, xử lý và phân tích dữ liệu văn bản trong Python, cùng với các ví dụ và giải thích rõ ràng, vì vậy bạn sẽ có mọi thứ cần biết về cách làm việc với dữ liệu văn bản trong Python.Hướng dẫn về tập hợp và lý thuyết tập hợp trong Python
Tìm hiểu về bộ Python. chúng là gì, cách tạo chúng, khi nào sử dụng chúng, các chức năng tích hợp và mối quan hệ của chúng với các hoạt động lý thuyết thiết lậpHướng dẫn về gấu trúc. Khung dữ liệu trong Python
Khám phá phân tích dữ liệu với Python. Pandas DataFrames giúp thao tác dữ liệu của bạn dễ dàng, từ việc chọn hoặc thay thế các cột và chỉ mục để định hình lại dữ liệu của bạnXem ThêmXem ThêmBạn có thích đọc hay nghĩ rằng nó có thể được cải thiện? . Nếu bạn thích bài viết này, vui lòng chia sẻ nó với bạn bè của bạn và đọc thêm một số
Trước khi chúng tôi bắt đầu, hãy hiểu sự khác biệt giữa gói và mô-đun vì chúng tôi sẽ tạo một số tài liệu tham khảo về những điều này trong suốt bài viết
Hãy bắt đầu nàomô-đun. Một kịch bản python duy nhất
Bưu kiện. Bộ sưu tập các mô-đun
Cấu trúc thư mục trong hình trên trông hơi phức tạp và tôi sẽ không yêu cầu bạn tạo tất cả cùng một lúc
Về bản chất, để đơn giản hóa mọi thứ, trước tiên hãy tạo một thư mục duy nhất
#example2.pyimport example1 as e1# imported string3 trong thư mục dự án của chúng ta và thêm hai mô-đun vào đó —
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string4 và
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string5
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
Ý tưởng là có bất kỳ chức năng/biến/lớp nào được xác định trong
#example2.pyimport example1 as e1# imported string4 để có thể truy cập được trong
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string5. Nội dung của mô-đun như sau
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example1.pyMY_EX1_STRING = 'Welcome to Example1 module!'def yolo[x: int]:
print["You only LIve", x, "times."]
Để nhập những mặt hàng này trong vòng
#example2.pyimport example1 as e1# imported string5
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1# imported string
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
Đầu ra từ việc chạy ví dụ2. py
Chỉ cần lặp lại những gì rõ ràng đáng chú ý, các mục trong mô-đun đã nhập có thể được truy cập bằng cách sử dụng ký hiệu dấu chấm — ví dụ:
#example2.pyimport example1 as e1# imported string9 hoặc
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.py# import example10. Nếu bạn từng cảm thấy việc viết
# print["The imported string is: ", example1.MY_EX1_STRING]
# example1.yolo[10]import sys
print[sys.path]
#example2.py# import example11 mỗi lần có vẻ hơi dài, chúng ta có thể sử dụng bí danh bằng cách sử dụng
# print["The imported string is: ", example1.MY_EX1_STRING]
# example1.yolo[10]import sys
print[sys.path]
#example2.py# import example12và viết lại
# print["The imported string is: ", example1.MY_EX1_STRING]
# example1.yolo[10]import sys
print[sys.path]
#example2.pyimport example1 as e1# imported string5 như sau. Như bạn đã đoán đúng, đầu ra sẽ vẫn giữ nguyên
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
Chính xác thì điều gì sẽ xảy ra khi chúng ta viết một câu lệnh 'nhập khẩu'?
Trình thông dịch python cố gắng tìm thư mục chứa mô-đun mà chúng tôi đang cố gắng nhập vào
#example2.py# import example14. Đó là danh sách các thư mục mà Python sẽ tìm kiếm sau khi tìm kiếm xong các mô-đun được lưu trong bộ nhớ cache và các mô-đun thư viện chuẩn của Python
# print["The imported string is: ", example1.MY_EX1_STRING]
# example1.yolo[10]import sys
print[sys.path]
Hãy xem đường dẫn hệ thống của chúng ta chứa gì tại thời điểm này [bằng cách nhận xét các dòng mã trước đó trong
#example2.pyimport example1 as e1# imported string5]
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.py# import example1
# print["The imported string is: ", example1.MY_EX1_STRING]
# example1.yolo[10]import sys
print[sys.path]
Đầu ra từ sys. con đường
Như bạn có thể thấy, phần tử đầu tiên trong danh sách được trả về bởi
#example2.py# import example14 trỏ tới thư mục
# print["The imported string is: ", example1.MY_EX1_STRING]
# example1.yolo[10]import sys
print[sys.path]
#example2.py# import example17, là nơi chứa mô-đun i đã nhập của chúng tôi. e.
# print["The imported string is: ", example1.MY_EX1_STRING]
# example1.yolo[10]import sys
print[sys.path]
#example2.pyimport example1 as e1# imported string4 cư trú. Bây giờ xin lưu ý bạn, không phải ngẫu nhiên mà thư mục này có mặt một cách kỳ diệu trong
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.py# import example14
# print["The imported string is: ", example1.MY_EX1_STRING]
# example1.yolo[10]import sys
print[sys.path]
Đầu ra từ
#example2.py# import example14 sẽ luôn chứa thư mục hiện tại ở chỉ mục 0. Thư mục hiện tại là thư mục chứa tập lệnh đang chạy
# print["The imported string is: ", example1.MY_EX1_STRING]
# example1.yolo[10]import sys
print[sys.path]
Đây là lý do việc nhập khá đơn giản khi cả mô-đun người gọi và mô-đun callee nằm trong cùng một thư mục
Điều gì xảy ra nếu tôi chỉ muốn nhập một số mục chứ không phải tất cả các mục từ mô-đun đã nhập?
Trong ví dụ của chúng tôi, chúng tôi chỉ có một chuỗi và một hàm được xác định trong mô-đun
#example2.pyimport example1 as e1# imported string4. Một điều quan trọng cần nhớ là bất cứ khi nào một câu lệnh nhập được thực hiện, toàn bộ mô-đun sẽ được chạy. Để chứng minh điều này, hãy sửa đổi
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string4 một chút
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string4
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
Và bây giờ hãy thử chạy
#example2.pyimport example1 as e1# imported string5. Bạn sẽ thấy rằng câu lệnh in cùng với đầu ra từ
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string44 cũng sẽ được in [ngoài các đầu ra trước đó]
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
Ghi chú. Có một giải pháp thay thế trong đó chúng ta có thể kiểm soát xem câu lệnh có được chạy hay không khi được nhập. Ví dụ: xem đoạn mã bên dưới
#example2.pyimport example1 as e1# imported string7
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
Mã bên trong câu lệnh
#example2.pyimport example1 as e1# imported string45 sẽ không chạy khi được nhập, nhưng
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string46 và
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string47 được xác định bên ngoài sẵn sàng để sử dụng thông qua một lần nhập. Phải nói rằng, nếu chúng ta chạy
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string4 như một mô-đun độc lập, mã trong câu lệnh
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string49 sẽ được thực thi
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
Đầu ra từ việc chạy ví dụ1. py
Dù sao đi nữa, bây giờ tôi đã chứng minh rằng việc nhập một mô-đun sẽ chạy tất cả nội dung của nó [nếu không sử dụng
#example2.pyimport example1 as e1# imported string70], chắc hẳn khá trực quan rằng tại sao chỉ nhập các mục quan tâm lại có ý nghĩa. Hãy xem cách thực hiện việc này trong
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string5bằng cách chỉ nhập hàm
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string72 từ
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string4. Điều này cũng giúp chúng tôi loại bỏ ký hiệu dấu chấm và chúng tôi chỉ có thể sử dụng hàm
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string72 nguyên trạng
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string8
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
Tương tự, chúng ta có thể thực hiện
#example2.pyimport example1 as e1# imported string75 để nhập cả hai đối tượng từ
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string4
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
Ghi chú. Thông thường, tồn tại các mã bao gồm các câu lệnh nhập chẳng hạn như
#example2.pyimport example1 as e1# imported string77. Điều này về cơ bản có nghĩa là nhập mọi thứ, tuy nhiên, đây được coi là cách làm không tốt vì nó tác động tiêu cực đến khả năng đọc mã
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
Nhu cầu về PYTHONPATH là gì?
Nếu bạn từng chú ý đến cấu trúc thư mục cho các dự án trên Github, thì thường có một thư mục
#example2.pyimport example1 as e1# imported string78 chứa một số tập lệnh tiện ích cho các tác vụ phổ biến như tiền xử lý, dọn dẹp dữ liệu, v.v. Chúng được giữ tách biệt với các tập lệnh chính và có nghĩa là được sử dụng lại
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
Hãy tiếp tục và tạo một cái cho dự án của chúng ta. Gói
#example2.pyimport example1 as e1# imported string78 sẽ chứa ba mô-đun —
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string80,
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string81 và
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string82 để trả về độ dài, chữ thường và chữ hoa của đầu vào chuỗi, tương ứng
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
Chúng tôi cũng sẽ tạo mô-đun
#example2.pyimport example1 as e1# imported string83 ở thư mục gốc của dự án. Đây là nơi chúng tôi sẽ nhập các mô-đun trong gói
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string84
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
Nội dung của ba mô-đun như sau
#example2.pyimport example1 as e1# imported string9
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
Bây giờ, nếu chúng ta phải nhập mô-đun
#example2.pyimport example1 as e1# imported string80 trong
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string83, đây là cách chúng ta thường làm
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example1.pyMY_EX1_STRING = 'Welcome to Example1 module!'def yolo[x: int]:2
print["You only LIve", x, "times."]
Điều quan trọng cần lưu ý là nếu bạn thực hiện
#example2.pyimport example1 as e1# imported string87 thay vì
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string88, bạn sẽ nhận được
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string89. Điều này là do danh sách
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.py# import example14 không chứa [chưa] thư mục
# print["The imported string is: ", example1.MY_EX1_STRING]
# example1.yolo[10]import sys
print[sys.path]
#example2.pyimport example1 as e1# imported string91 cần thiết để tìm mô-đun
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string80. Hãy xem cách chúng ta có thể thêm nó vào danh sách
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.py# import example14
# print["The imported string is: ", example1.MY_EX1_STRING]
# example1.yolo[10]import sys
print[sys.path]
Có hai cách để làm điều này
Phương pháp 1. sử dụng
#example2.pyimport example1 as e1# imported string94
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string1
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
Vài điều cần xem xét
- Thứ tự của
#example2.pyimport example1 as e1# imported string
95 rất quan trọng — chỉ khi bạn đã thêm đường dẫn đến thư mục
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]#example2.pyimport example1 as e1# imported string
78 bằng cách sử dụng
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]#example2.pyimport example1 as e1# imported string
94, bạn mới có thể thực thi câu lệnh
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]#example2.pyimport example1 as e1# imported string
87.
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
Tóm lại, đừng để tất cả#example2.pyimport example1 as e1# imported string
99,
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]#example1.pyMY_EX1_STRING = 'Welcome to Example1 module!'def yolo[x: int]:
20 và
print["You only LIve", x, "times."]#example2.pyimport example1 as e1# imported string
87 ở đầu tập lệnh chỉ vì sự gọn gàng.
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10] #example1.pyMY_EX1_STRING = 'Welcome to Example1 module!'def yolo[x: int]:
22 trả về đường dẫn tuyệt đối đến thư mục làm việc hiện tại. Chúng tôi sử dụng
print["You only LIve", x, "times."]#example1.pyMY_EX1_STRING = 'Welcome to Example1 module!'def yolo[x: int]:
23 để thêm thư mục
print["You only LIve", x, "times."]#example2.pyimport example1 as e1# imported string
78 vào đường dẫn này
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]- Như mọi khi, việc truy cập các chức năng được xác định trong mô-đun đã nhập được hỗ trợ bằng cách sử dụng ký hiệu dấu chấm i. e.
#example1.pyMY_EX1_STRING = 'Welcome to Example1 module!'def yolo[x: int]:
25
print["You only LIve", x, "times."]
Phương pháp 2. sử dụng biến môi trường
#example1.pyMY_EX1_STRING = 'Welcome to Example1 module!'def yolo[x: int]:26
print["You only LIve", x, "times."]
Thông thường, tôi thấy việc sửa đổi biến pythonpath dễ dàng hơn là xử lý các thư mục nối thêm bằng Phương pháp 1
PYTHONPATH là một biến môi trường mà bạn có thể đặt để thêm các thư mục bổ sung nơi python sẽ tìm kiếm các mô-đun và gói. [Nguồn]
Trước khi chúng tôi sửa đổi nó, hãy kiểm tra nội dung của nó [để đảm bảo rằng chúng tôi không ghi đè] bằng cách sử dụng
#example1.pyMY_EX1_STRING = 'Welcome to Example1 module!'def yolo[x: int]:27 trong thiết bị đầu cuối
print["You only LIve", x, "times."]
Có vẻ như hiện tại nó trống nhưng trong trường hợp không có, bạn luôn nên sửa đổi pythonpath theo cách mà bạn đang thêm vào nó và không ghi đè lên nó. Cụ thể hơn, bạn phải thêm thư mục mới của mình vào
#example1.pyMY_EX1_STRING = 'Welcome to Example1 module!'def yolo[x: int]:26, được phân tách bằng dấu hai chấm [
print["You only LIve", x, "times."]
#example1.pyMY_EX1_STRING = 'Welcome to Example1 module!'def yolo[x: int]:29] từ nội dung hiện có của nó
print["You only LIve", x, "times."]
Với bộ biến pythonpath, chúng tôi không còn cần phải thêm vào
#example2.py# import example14in
# print["The imported string is: ", example1.MY_EX1_STRING]
# example1.yolo[10]import sys
print[sys.path]
#example2.pyimport example1 as e1# imported string83 nữa [Tôi đã nhận xét chúng trong đoạn trích bên dưới cho rõ ràng]
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1# imported string0
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
Ghi chú. Khi bạn đóng python, danh sách sẽ trở lại các giá trị mặc định trước đó. Nếu bạn muốn thêm vĩnh viễn một thư mục vào
#example2.pyimport example1 as e1# imported string12, hãy thêm lệnh xuất [
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string13] vào
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string14 của bạn. [Xem cuộc thảo luận về StackOverflow này]
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
Cuối cùng, khi đã xác định cả hai phương thức, hãy chọn một phương thức [dựa trên sở thích/trường hợp sử dụng của bạn] để nhập hai mô-đun còn lại —
#example2.pyimport example1 as e1# imported string82 và
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string81 trong
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string83.
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
[P. S. Tôi đang đi với Phương pháp 1 chỉ để giải trí. ]
#example2.pyimport example1# imported string1
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
siêu. Điều này có vẻ tuyệt vời. Tuy nhiên, sẽ không tuyệt sao nếu chúng ta chỉ có thể thực hiện
#example2.pyimport example1 as e1# imported string18 thay vì nhập tất cả các mô-đun bên trong nó một cách riêng lẻ? . Vì vậy, làm thế nào để chúng ta làm điều đó?
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
Khi nào chúng ta cần __init__. py?
Trước tiên, hãy thử nhập thư mục
#example2.pyimport example1 as e1# imported string78 trong
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string83 [sau khi nhận xét tất cả mã hiện có]
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1# imported string2
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
Chạy tập lệnh này sẽ không gây ra bất kỳ lỗi nào và đúng như vậy - trình thông dịch sẽ xem bên trong
#example2.py# import example14 và nó sẽ tìm thấy thư mục hiện tại
# print["The imported string is: ", example1.MY_EX1_STRING]
# example1.yolo[10]import sys
print[sys.path]
#example2.pyimport example1# imported string02 tại chỉ mục 0. Đây là tất cả những gì nó cần để tìm thư mục
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
#example2.pyimport example1 as e1# imported string78
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
Bây giờ, hãy thử truy cập mô-đun
#example2.pyimport example1 as e1# imported string80 từ
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string78
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1# imported string3
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
Khi bạn cố chạy tập lệnh này, bạn sẽ thấy một thông báo
#example2.pyimport example1# imported string06. Theo thuật ngữ thông thường, điều này có nghĩa là chúng tôi sẽ không thể truy cập bất kỳ tập lệnh python nào bên trong
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
#example2.pyimport example1 as e1# imported string78đơn giản vì trình thông dịch chưa biết đây là một gói
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
Chúng tôi có thể biến thư mục này thành một gói bằng cách giới thiệu tệp
#example2.pyimport example1# imported string08 trong thư mục
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
#example2.pyimport example1 as e1# imported string78
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
Trong vòng
#example2.pyimport example1# imported string08, chúng tôi nhập tất cả các mô-đun mà chúng tôi cho là cần thiết cho dự án của mình
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
#example2.pyimport example1# imported string4
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
Và hãy gọi nó trong vòng
#example2.pyimport example1 as e1# imported string83
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1# imported string5
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
Đợi chút. Tại sao chúng tôi thấy lỗi khi chạy
#example2.pyimport example1# imported string12?
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
Trả lời. Cách chúng tôi đã nhập các mô-đun trong
#example2.pyimport example1# imported string08 ở trên có vẻ hợp lý với bạn — xét cho cùng thì
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
#example2.pyimport example1# imported string08 và
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
#example2.pyimport example1 as e1# imported string80 [hoặc
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string81,
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string82] đều ở cùng cấp độ nên không có lý do gì mà
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1# imported string18 không hoạt động. Trên thực tế, nếu bạn tự chạy tệp
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
#example2.pyimport example1# imported string19 này, nó sẽ thực thi hoàn hảo [nó sẽ không có đầu ra nhưng dù sao cũng sẽ thực thi thành công].
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
Phải nói rằng, chúng ta không thể sử dụng cách nhập ở trên vì mặc dù
#example2.pyimport example1 as e1# imported string80 và
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string81 ở cùng cấp độ với
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1# imported string08 nhưng đây không phải là cấp độ mà từ đó
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
#example2.pyimport example1# imported string19 sẽ được gọi. Trên thực tế, chúng tôi đang thực hiện cuộc gọi từ
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
#example2.pyimport example1 as e1# imported string83 nên
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.py# import example14 sẽ chỉ có thư mục hiện tại của
# print["The imported string is: ", example1.MY_EX1_STRING]
# example1.yolo[10]import sys
print[sys.path]
#example2.pyimport example1 as e1# imported string83 i. e.
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1# imported string02 để tìm kiếm bên trong cho bất kỳ hàng nhập khẩu nào. Do đó, khi trình thông dịch gặp lệnh
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
#example2.pyimport example1 as e1# imported string18 trong
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string83, mặc dù nó di chuyển đến
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1# imported string08 bên trong thư mục
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
#example2.pyimport example1 as e1# imported string78, thì
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.py# import example14 không được cập nhật tự động và trình thông dịch không có cách nào biết tìm mô-đun có tên
# print["The imported string is: ", example1.MY_EX1_STRING]
# example1.yolo[10]import sys
print[sys.path]
#example2.pyimport example1# imported string33 ở đâu. Bằng cách nào đó chúng ta phải trỏ đến vị trí của thư mục
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
#example2.pyimport example1 as e1# imported string78. Để làm như vậy, chúng tôi có thể sử dụng nhập tương đối hoặc tuyệt đối trong
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1# imported string08 [hoặc đặt biến
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
#example1.pyMY_EX1_STRING = 'Welcome to Example1 module!'def yolo[x: int]:26 như được mô tả ở trên]
print["You only LIve", x, "times."]
Nhập khẩu tương đối [không khuyến nghị]. chỉ định đường dẫn liên quan đến đường dẫn của tập lệnh gọi
#example2.pyimport example1# imported string6
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
Chúng tôi sử dụng ký hiệu dấu chấm [
#example2.pyimport example1# imported string37 hoặc
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
#example2.pyimport example1# imported string38] để chỉ định nhập khẩu tương đối. Dấu chấm đơn trước Lower đề cập đến cùng thư mục với thư mục mà quá trình nhập được gọi. Điều này có thể được hình dung như nhập
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
#example2.pyimport example1# imported string39 từ
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
#example2.pyimport example1# imported string40. Tương tự, hai dấu chấm trước tên mô-đun có nghĩa là tăng hai cấp so với cấp hiện tại
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
Nhập khẩu tuyệt đối [lựa chọn tốt hơn]. chỉ định đường dẫn tuyệt đối của mô-đun đã nhập từ thư mục gốc của dự án [hoặc bất kỳ thư mục nào khác mà sys. đường dẫn có quyền truy cập vào]
#example2.pyimport example1# imported string7
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
Bây giờ, điều này chứa nhiều thông tin hơn so với nhập khẩu tương đối và ít bị phá vỡ hơn. Ngoài ra,
#example2.py# import example14 có quyền truy cập vào thư mục gốc của dự án i. e.
# print["The imported string is: ", example1.MY_EX1_STRING]
# example1.yolo[10]import sys
print[sys.path]
#example2.pyimport example1# imported string02 như đã giải thích ở trên và từ đó có thể dễ dàng tìm kiếm thư mục
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
#example2.pyimport example1 as e1# imported string78. [Tại sao? Bởi vì nó là thư mục con trực tiếp của gốc dự án]
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
Điều gì xảy ra khi chúng tôi nhập một gói có xác định
#example2.pyimport example1# imported string44? . Cho rằng chúng tôi thực hiện tất cả các nhập cần thiết ở đây, mã sạch hơn nhiều trong tập lệnh gọi. Ví dụ
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
#example2.pyimport example1# imported string8
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
Thật tuyệt vời. Bây giờ chúng tôi đã chuyển đổi thư mục
#example2.pyimport example1 as e1# imported string78 của mình thành một gói. Cái hay của gói này là nó có thể được nhập ở bất cứ đâu và sử dụng gần như ngay lập tức. Hãy xem cách chúng ta có thể sử dụng gói này bên trong thư mục
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1 as e1# imported string3. Hãy tiếp tục và tạo một tệp mới có tên là
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1# imported string47 trong
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
#example2.pyimport example1 as e1# imported string3
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
#example2.pyimport example1# imported string9
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
Vài điều cần xem xét
- Trước khi nhập gói
#example2.pyimport example1 as e1# imported string
78, chúng tôi phải đảm bảo rằng thư mục mẹ của
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]#example2.pyimport example1 as e1# imported string
78 i. e. root dự án có thể truy cập được bằng trình thông dịch Python. Sẽ là thiếu thận trọng khi cho rằng nó sẽ xảy ra theo mặc định, chủ yếu là do chúng tôi hiện đang ở một cấp trong thư mục gốc của dự án [chúng tôi đang chạy tập lệnh từ
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]#example2.pyimport example1# imported string
51],
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]#example2.py# import example1
4 sẽ có
# print["The imported string is: ", example1.MY_EX1_STRING]
# example1.yolo[10]import sys
print[sys.path]#example2.pyimport example1# imported string
53 ở chỉ mục 0
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10] #example1.pyMY_EX1_STRING = 'Welcome to Example1 module!'def yolo[x: int]:
22 sẽ cung cấp tên của thư mục cho tập lệnh hiện tại và
print["You only LIve", x, "times."]#example2.pyimport example1# imported string
55 sẽ cung cấp đường dẫn đến thư mục mẹ bằng ký hiệu dấu chấm i. e.
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]#example2.pyimport example1# imported string
38. Nói chung,
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]#example2.pyimport example1# imported string
57 sẽ cung cấp đường dẫn tuyệt đối đến thư mục gốc của dự án
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
Thưởng. Chúng tôi thậm chí có thể thêm các mô-đun từ các thư mục khác vào
#example2.pyimport example1# imported string08 của chúng tôi. Chẳng hạn, hãy mang vào
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
#example2.pyimport example1# imported string59 được xác định trong
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
#example2.pyimport example1# imported string60
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
#example2.pyimport example1 as e1# imported string0
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
Gọi chức năng này trong
#example2.pyimport example1# imported string47
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
#example2.pyimport example1 as e1# imported string1
print["The imported string is: ", e1.MY_EX1_STRING]# imported function
e1.yolo[10]
Phần kết luận
Thành thật mà nói, ban đầu các lỗi nhập khẩu từng thực sự khiến tôi phát hoảng vì đây là một lĩnh vực mà tôi không bao giờ phải bận tâm. Qua nhiều năm, tôi đã học được một thủ thuật hữu ích — đối với bất kỳ gói/mô-đun nào bạn đang cố gắng nhập bằng cách sử dụng
#example2.pyimport example1# imported string62, hãy đảm bảo trình thông dịch Python có quyền truy cập vào gói/mô-đun đó. Nếu không, hãy cập nhật
print["The imported string is: ", example1.MY_EX1_STRING]# imported function
example1.yolo[10]
#example2.py# import example14 hoặc thậm chí tốt hơn là thêm thư mục có liên quan vào biến
# print["The imported string is: ", example1.MY_EX1_STRING]
# example1.yolo[10]import sys
print[sys.path]
#example1.pyMY_EX1_STRING = 'Welcome to Example1 module!'def yolo[x: int]:26 và tránh phải xử lý nó trong tập lệnh của bạn
print["You only LIve", x, "times."]
Như mọi khi, nếu có một cách dễ dàng hơn để thực hiện/giải thích một số nội dung được đề cập trong bài viết này, hãy cho tôi biết. Nói chung, hãy kiềm chế những bình luận phá hoại/rác rưởi/thù địch không được yêu cầu
Cho đến lần sau ✨
Tôi thích viết hướng dẫn từng bước cho người mới bắt đầu, hướng dẫn cách thực hiện, câu hỏi phỏng vấn, giải mã thuật ngữ được sử dụng trong ML/AI, v.v. Nếu bạn muốn có toàn quyền truy cập vào tất cả các bài viết của tôi [và các tác giả khác trên Phương tiện], thì bạn có thể đăng ký bằng liên kết của tôi tại đây