Có thể trùng lặp: Nhập mô -đun động trong Python
Dynamic module import in Python
Tôi dự định sẽ sớm tạo một bộ tệp vào một lúc nào đó và cách tốt nhất để tổ chức nó là có một danh sách, danh sách đó sẽ ở đầu một tập tin và sau khi nó sẽ có một số lượng mã vô lý để xử lý Danh sách đó kiểm soát những gì và cách nó hoạt động. Tôi đang tìm cách viết danh sách chỉ một lần và Danh sách nói là danh sách các tên thư mục và tệp trong định dạng này:
[[folder/filename, bool, bool, int], [folder/filename, bool, bool, int]]
Như bạn có thể thấy, folder/filename
là giống nhau [loại]. Tên tệp là tên thư mục với .py
ở cuối, nhưng thực hiện nhập xxx bạn không cần phải nhập xxx.py, vì vậy tôi không thấy điều này gây ra sự cố.
Vấn đề tôi đang gặp phải là nhập khẩu bằng phương pháp này ...
for [testName, auto, hardware, bit] in testList:
print[testName]
paths = "\\" + testName
print paths
addpath[paths]
sys.modules[testName] = testName # One of a few options I've seen suggested on the net
print["Path Added"]
test = testName + ".Helloworld[]"
eval[test]
Vì vậy, đối với mỗi bài kiểm tra tôi có, in tên, lắp ráp một chuỗi chứa đường dẫn [____10], cho ví dụ này, in đường dẫn thử nghiệm, sau đó thêm đường dẫn vào danh sách [
for [testName, auto, hardware, bit] in testList:
print[testName]
paths = "\\" + testName
print paths
addpath[paths]
sys.modules[testName] = testName # One of a few options I've seen suggested on the net
print["Path Added"]
test = testName + ".Helloworld[]"
eval[test]
1], sau đó in để xác nhận nó đã xảy ra, sau đó Lắp một chuỗi sẽ được thực hiện bởi for [testName, auto, hardware, bit] in testList:
print[testName]
paths = "\\" + testName
print paths
addpath[paths]
sys.modules[testName] = testName # One of a few options I've seen suggested on the net
print["Path Added"]
test = testName + ".Helloworld[]"
eval[test]
2 cho các thử nghiệm mô -đun chính và cuối cùng là đánh giá nó.Như bạn có thể thấy, hiện tại tôi phải có một danh sách nhập khẩu ở đầu. Tôi chỉ đơn giản là nhập
for [testName, auto, hardware, bit] in testList:
print[testName]
paths = "\\" + testName
print paths
addpath[paths]
sys.modules[testName] = testName # One of a few options I've seen suggested on the net
print["Path Added"]
test = testName + ".Helloworld[]"
eval[test]
3 [nội dung của for [testName, auto, hardware, bit] in testList:
print[testName]
paths = "\\" + testName
print paths
addpath[paths]
sys.modules[testName] = testName # One of a few options I've seen suggested on the net
print["Path Added"]
test = testName + ".Helloworld[]"
eval[test]
3 là tên của mô -đun tôi muốn nhập], vì nó sẽ cố gắng tìm một mô -đun gọi là for [testName, auto, hardware, bit] in testList:
print[testName]
paths = "\\" + testName
print paths
addpath[paths]
sys.modules[testName] = testName # One of a few options I've seen suggested on the net
print["Path Added"]
test = testName + ".Helloworld[]"
eval[test]
3, không phải là một mô -đun gọi là nội dung của for [testName, auto, hardware, bit] in testList:
print[testName]
paths = "\\" + testName
print paths
addpath[paths]
sys.modules[testName] = testName # One of a few options I've seen suggested on the net
print["Path Added"]
test = testName + ".Helloworld[]"
eval[test]
3.Tôi đã thấy một vài ví dụ về nơi này đã được thực hiện, nhưng không thể tìm thấy bất kỳ công việc nào trong hoàn cảnh của tôi. Nếu ai đó thực sự có thể ném một đoạn mã, điều đó thật tuyệt vời.
Tôi cũng yêu cầu tôi không bị treo, được rút ra, cũng không được sử dụng để sử dụng eval, nó được sử dụng trong một môi trường rất được kiểm soát [danh sách mà nó chu kỳ nằm trong tệp .py, vì vậy không có "người dùng cuối" nên lộn xộn với nó].
sys.path.append["module_path"]4
Inside Class method Inside explicit method8
Có nhiều phương pháp khác nhau có thể được sử dụng để nhập mô -đun bằng cách sử dụng đường dẫn đầy đủ của nó:
- Sử dụng chức năng sys.path.append []
- Sử dụng gói nhập khẩu
- Sử dụng lớp SourceFileloader
Xem xét sắp xếp tệp sau đây và hãy để xem các phương thức được liệt kê ở trên có thể được sử dụng để nhập mô-đun gfg.py trong main.py:
python |--main.py |articles |--gfg.py
Dưới đây là mã cho gfg.py:gfg.py:
Python3
for [testName, auto, hardware, bit] in testList:
print[testName]
paths = "\\" + testName
print paths
addpath[paths]
sys.modules[testName] = testName # One of a few options I've seen suggested on the net
print["Path Added"]
test = testName + ".Helloworld[]"
eval[test]
7 for [testName, auto, hardware, bit] in testList:
print[testName]
paths = "\\" + testName
print paths
addpath[paths]
sys.modules[testName] = testName # One of a few options I've seen suggested on the net
print["Path Added"]
test = testName + ".Helloworld[]"
eval[test]
8for [testName, auto, hardware, bit] in testList:
print[testName]
paths = "\\" + testName
print paths
addpath[paths]
sys.modules[testName] = testName # One of a few options I've seen suggested on the net
print["Path Added"]
test = testName + ".Helloworld[]"
eval[test]
9python |--main.py |articles |--gfg.py0
python |--main.py |articles |--gfg.py1
python |--main.py |articles |--gfg.py2
python |--main.py |articles |--gfg.py3
python |--main.py |articles |--gfg.py4
python |--main.py |articles |--gfg.py5
python |--main.py |articles |--gfg.py6
python |--main.py |articles |--gfg.py0
python |--main.py |articles |--gfg.py8
for [testName, auto, hardware, bit] in testList:
print[testName]
paths = "\\" + testName
print paths
addpath[paths]
sys.modules[testName] = testName # One of a few options I've seen suggested on the net
print["Path Added"]
test = testName + ".Helloworld[]"
eval[test]
9python |--main.py |articles |--gfg.py3
python |--main.py |articles |--gfg.py4
sys.path.append["module_path"]2
python |--main.py |articles |--gfg.py6
Sử dụng chức năng sys.path.append []
Sử dụng gói nhập khẩu
Sử dụng lớp SourceFileloader
sys.path.append["module_path"]
Xem xét sắp xếp tệp sau đây và hãy để xem các phương thức được liệt kê ở trên có thể được sử dụng để nhập mô-đun gfg.py trong main.py:
Python3
Dưới đây là mã cho gfg.py:
sys.path.append["module_path"]6
sys.path.append["module_path"]7
python |--main.py |articles |--gfg.py6
for [testName, auto, hardware, bit] in testList:
print[testName]
paths = "\\" + testName
print paths
addpath[paths]
sys.modules[testName] = testName # One of a few options I've seen suggested on the net
print["Path Added"]
test = testName + ".Helloworld[]"
eval[test]
7 for [testName, auto, hardware, bit] in testList:
print[testName]
paths = "\\" + testName
print paths
addpath[paths]
sys.modules[testName] = testName # One of a few options I've seen suggested on the net
print["Path Added"]
test = testName + ".Helloworld[]"
eval[test]
8for [testName, auto, hardware, bit] in testList:
print[testName]
paths = "\\" + testName
print paths
addpath[paths]
sys.modules[testName] = testName # One of a few options I've seen suggested on the net
print["Path Added"]
test = testName + ".Helloworld[]"
eval[test]
9python |--main.py |articles |--gfg.py0
python |--main.py |articles |--gfg.py1
Inside Class method Inside explicit method5
Inside Class method Inside explicit method6
Output:
Inside Class method Inside explicit method
Sử dụng gói nhập khẩu
Gói nhập khẩu cung cấp việc triển khai câu lệnh nhập trong mã nguồn Python có thể mang lại cho bất kỳ trình thông dịch Python nào. Điều này cho phép người dùng tạo các đối tượng tùy chỉnh của họ giúp họ sử dụng quy trình nhập theo nhu cầu của họ. Nhập khẩu.util là một trong những mô -đun có trong gói này có thể được sử dụng để nhập mô -đun từ đường dẫn đã cho.
Cú pháp:
MODULE = IMPINTLIB.UTIL.SPEC_FROM_FILE_LOcation [Module Module_Name, Module_path,]
Example:
Python3
sys.path.append["module_path"]4
Inside Class method Inside explicit method8
Inside Class method Inside explicit method9
Inside Class method Inside explicit method0
Inside Class method Inside explicit method1
Inside Class method Inside explicit method2
Inside Class method Inside explicit method3
Inside Class method Inside explicit method4
python |--main.py |articles |--gfg.py6
Inside Class method Inside explicit method6
Inside Class method Inside explicit method0
Inside Class method Inside explicit method8
Inside Class method Inside explicit method9
module = SourceFileLoader["module_name","module_path"].load_module[]0
module = SourceFileLoader["module_name","module_path"].load_module[]1
Output:
Inside Class method Inside explicit method
Sử dụng lớp SourceFileloader
Lớp SourceFileLoader là một lớp cơ sở trừu tượng được sử dụng để triển khai tải tệp nguồn với sự trợ giúp của hàm load_module [] thực sự nhập mô -đun.
Cú pháp:
module = SourceFileLoader["module_name","module_path"].load_module[]
MODULE = IMPINTLIB.UTIL.SPEC_FROM_FILE_LOcation [Module Module_Name, Module_path,]
Python3
sys.path.append["module_path"]4
Inside Class method Inside explicit method8
Inside Class method Inside explicit method6
Inside Class method Inside explicit method0
Inside Class method Inside explicit method8
module = SourceFileLoader["module_name","module_path"].load_module[]0
module = SourceFileLoader["module_name","module_path"].load_module[]1
Output:
Inside Class method Inside explicit method