Hướng dẫn python import from path string - nhập python từ chuỗi đường dẫn

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 method
8

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]
8

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]
9
python
     |--main.py
     |articles
        |--gfg.py  
0
python
     |--main.py
     |articles
        |--gfg.py  
1

python
     |--main.py
     |articles
        |--gfg.py  
2
python
     |--main.py
     |articles
        |--gfg.py  
3
python
     |--main.py
     |articles
        |--gfg.py  
4
python
     |--main.py
     |articles
        |--gfg.py  
5
python
     |--main.py
     |articles
        |--gfg.py  
6

python
     |--main.py
     |articles
        |--gfg.py  
0
python
     |--main.py
     |articles
        |--gfg.py  
8

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]
9
python
     |--main.py
     |articles
        |--gfg.py  
3
python
     |--main.py
     |articles
        |--gfg.py  
4
sys.path.append["module_path"]
2
python
     |--main.py
     |articles
        |--gfg.py  
6

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.py  
6

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]
8

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]
9
python
     |--main.py
     |articles
        |--gfg.py  
0
python
     |--main.py
     |articles
        |--gfg.py  
1

Inside Class method
Inside explicit method
5

Inside Class method
Inside explicit method
6

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 method
8

Inside Class method
Inside explicit method
9
Inside Class method
Inside explicit method
0
Inside Class method
Inside explicit method
1
Inside Class method
Inside explicit method
2
Inside Class method
Inside explicit method
3
Inside Class method
Inside explicit method
4
python
     |--main.py
     |articles
        |--gfg.py  
6

Inside Class method
Inside explicit method
6
Inside Class method
Inside explicit method
0
Inside Class method
Inside explicit method
8

Inside Class method
Inside explicit method
9

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 method
8

Inside Class method
Inside explicit method
6
Inside Class method
Inside explicit method
0
Inside Class method
Inside explicit method
8

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

Làm cách nào để nhập một đường dẫn Python?

Chức năng nối []. Đây là cách dễ nhất để nhập mô -đun Python bằng cách thêm đường dẫn mô -đun vào biến đường dẫn. Biến đường dẫn chứa các thư mục thông dịch viên Python tìm kiếm để tìm các mô -đun được nhập trong các tệp nguồn.. This is the easiest way to import a Python module by adding the module path to the path variable. The path variable contains the directories Python interpreter looks in for finding modules that were imported in the source files.

__ nhập __ làm gì trong Python?

__Import __ [] trong mô -đun Python giúp đưa mã có mặt trong một mô -đun khác bằng cách nhập hàm hoặc mã hoặc tệp bằng cách nhập phương thức Python.Nhập trong Python trả về đối tượng hoặc mô -đun mà chúng tôi đã chỉ định trong khi sử dụng mô -đun nhập.helps in getting the code present in another module by either importing the function or code or file using the import in python method. The import in python returns the object or module that we specified while using the import module.

Làm cách nào để nhập một mô -đun động trong Python?

Đây tải mô -đun hoặc lớp..
Bằng cách sử dụng phương thức __Import __ []: __Import __ [] là một phương thức dunder [Phương pháp bắt đầu và kết thúc với Double Undercore cũng được gọi là Phương pháp ma thuật] và tất cả các lớp sở hữu nó.....
Sử dụng mô -đun IMP: Các mô -đun có thể được nhập động bằng mô -đun IMP trong Python ..

Nhập khẩu Return_Module Returt_Module là gì?

Điều này có nghĩa là tất cả các ngữ nghĩa của hàm có nguồn gốc từ nhập khẩu.__nhập khẩu__[] .Sự khác biệt quan trọng nhất giữa hai hàm này là Nhập.Module [] trả về gói hoặc mô-đun được chỉ định [ví dụ: pkg. Mod], trong khi __Import __ [] trả về gói cấp cao nhất hoặc mô-đun [ví dụ: pkg].the specified package or module [e.g. pkg. mod ], while __import__[] returns the top-level package or module [e.g. pkg ].

Bài Viết Liên Quan

Chủ Đề