\ [\ newcommand {l} [1] {\ | #1 \ |} \ newcommand {vl} [1] {\ l {\ vec Re} \, [#1]} \ newCommand {i} [1] {\ operatorName {im} \, [#1]} \]
See:
- Tài liệu Python trên sys.path;
- Mô -đun Python của tuần trên: Đường dẫn nhập.
Hãy nói rằng chúng tôi đã viết một mô -đun Python và lưu nó dưới dạng
import a_module a_module.func[]1, trong một thư mục gọi là
import a_module a_module.func[]2.
Chúng tôi cũng có một kịch bản gọi là
import a_module a_module.func[]3 trong một thư mục gọi là
import a_module a_module.func[]4.
Chúng tôi muốn có thể
import a_module a_module.func[]5 mã trong
import a_module a_module.func[]1 để sử dụng trong
import a_module a_module.func[]3. Vì vậy, chúng tôi muốn có thể đặt dòng của anh ấy vào
import a_module a_module.func[]3:
Mô -đun và tập lệnh có thể trông như thế này:
Nội dung của
import a_module a_module.func[]9
def func[]: print["Running useful function"]
Nội dung của
$ python3 scripts/a_script.py Traceback [most recent call last]: File "scripts/a_script.py", line 1, in import a_module ModuleNotFoundError: No module named 'a_module'0
import a_module a_module.func[]
Hiện tại,
import a_module a_module.func[]3 sẽ thất bại với:
$ python3 scripts/a_script.py Traceback [most recent call last]: File "scripts/a_script.py", line 1, in import a_module ModuleNotFoundError: No module named 'a_module'
Khi Python chạm vào dòng
$ python3 scripts/a_script.py Traceback [most recent call last]: File "scripts/a_script.py", line 1, in import a_module ModuleNotFoundError: No module named 'a_module'2, nó cố gắng tìm một gói hoặc một mô -đun gọi là
$ python3 scripts/a_script.py Traceback [most recent call last]: File "scripts/a_script.py", line 1, in import a_module ModuleNotFoundError: No module named 'a_module'3. Một gói là một thư mục chứa các mô -đun, nhưng chúng tôi sẽ chỉ xem xét các mô -đun cho đến bây giờ. Một mô -đun là một tệp có tiện ích mở rộng phù hợp, chẳng hạn như
$ python3 scripts/a_script.py Traceback [most recent call last]: File "scripts/a_script.py", line 1, in import a_module ModuleNotFoundError: No module named 'a_module'4. Vì vậy, Python đang tìm kiếm một tập tin
import a_module a_module.func[]1 và không tìm thấy nó.
Bạn sẽ thấy hiệu ứng tương tự tại bảng điều khiển Python tương tác hoặc trong ipython:
>>> import a_module Traceback [most recent call last]: File "", line 1, in ModuleNotFoundError: No module named 'a_module'
Python tìm kiếm các mô -đun trong Sy Sys.Path
Python có một thuật toán đơn giản để tìm một mô -đun có tên nhất định, chẳng hạn như
$ python3 scripts/a_script.py Traceback [most recent call last]: File "scripts/a_script.py", line 1, in import a_module ModuleNotFoundError: No module named 'a_module'3. Nó tìm kiếm một tập tin gọi là
import a_module a_module.func[]1 trong các thư mục được liệt kê trong biến
$ python3 scripts/a_script.py Traceback [most recent call last]: File "scripts/a_script.py", line 1, in import a_module ModuleNotFoundError: No module named 'a_module'8.
>>> import sys >>> type[sys.path] >>> for path in sys.path: ... print[path] ... /Users/brettmz-admin/dev_trees/psych-214-fall-2016/sphinxext /usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python37.zip /usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7 /usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload /Users/brettmz-admin/Library/Python/3.7/lib/python/site-packages /Users/brettmz-admin/dev_trees/grin /Users/brettmz-admin/dev_trees/rmdex /usr/local/lib/python3.7/site-packages
Tệp
import a_module a_module.func[]1 nằm trong thư mục
import a_module a_module.func[]2 và thư mục này không nằm trong danh sách
$ python3 scripts/a_script.py Traceback [most recent call last]: File "scripts/a_script.py", line 1, in import a_module ModuleNotFoundError: No module named 'a_module'8.
Bởi vì
$ python3 scripts/a_script.py Traceback [most recent call last]: File "scripts/a_script.py", line 1, in import a_module ModuleNotFoundError: No module named 'a_module'8 chỉ là một danh sách Python, giống như bất kỳ danh sách nào khác, chúng tôi có thể thực hiện công việc nhập khẩu bằng cách thêm thư mục
import a_module a_module.func[]2 vào danh sách.
>>> import sys >>> sys.path.append['code'] >>> # Now the import will work >>> import a_module
Có nhiều cách khác nhau để đảm bảo một thư mục luôn nằm trong danh sách Python
$ python3 scripts/a_script.py Traceback [most recent call last]: File "scripts/a_script.py", line 1, in import a_module ModuleNotFoundError: No module named 'a_module'8 khi bạn chạy Python, bao gồm:
- Đặt thư mục vào nội dung của biến môi trường
>>> import a_module Traceback [most recent call last]: File "", line 1, in ModuleNotFoundError: No module named 'a_module'
5 - sử dụng PythonpathUsing PYTHONPATH - Tạo một phần mô -đun của gói có thể cài đặt và cài đặt nó - xem: Tạo gói Python.
Là một hack thô, bạn cũng có thể đặt thư mục
import a_module a_module.func[]2 của mình trên Python
$ python3 scripts/a_script.py Traceback [most recent call last]: File "scripts/a_script.py", line 1, in import a_module ModuleNotFoundError: No module named 'a_module'8 ở đầu các tệp cần nó:
Nội dung của
>>> import a_module Traceback [most recent call last]: File "", line 1, in ModuleNotFoundError: No module named 'a_module'8
import sys sys.path.append['code'] import a_module a_module.func[]
Then:
$ python3 scripts/a_script_with_hack.py
Running useful function
>>> import a_module Traceback [most recent call last]: File "", line 1, in ModuleNotFoundError: No module named 'a_module'9 đơn giản ở trên sẽ chỉ hoạt động khi chạy tập lệnh từ một thư mục chứa thư mục con
import a_module a_module.func[]2. Ví dụ:
$ mkdir another_dir $ cd another_dir $ python3 ../scripts/a_script_with_hack.py Traceback [most recent call last]: File "../scripts/a_script_with_hack.py", line 4, in import a_module ModuleNotFoundError: No module named 'a_module'
Điều này là do thư mục
import a_module a_module.func[]2 mà chúng tôi đã chỉ định là một đường dẫn tương đối và do đó, Python tìm kiếm thư mục
import a_module a_module.func[]2 trong thư mục làm việc hiện tại.
Để làm cho bản hack hoạt động khi chạy mã từ bất kỳ thư mục nào, bạn có thể sử dụng một số thao tác đường dẫn trên biến __File__,:path manipulation on the The “__file__” variable:
Nội dung của
>>> import sys >>> type[sys.path] >>> for path in sys.path: ... print[path] ... /Users/brettmz-admin/dev_trees/psych-214-fall-2016/sphinxext /usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python37.zip /usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7 /usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload /Users/brettmz-admin/Library/Python/3.7/lib/python/site-packages /Users/brettmz-admin/dev_trees/grin /Users/brettmz-admin/dev_trees/rmdex /usr/local/lib/python3.7/site-packages3
from os.path import dirname, abspath, join import sys # Find code directory relative to our directory THIS_DIR = dirname[__file__] CODE_DIR = abspath[join[THIS_DIR, '..', 'code']] sys.path.append[CODE_DIR] import a_module a_module.func[]
Bây giờ nhập mô -đun không hoạt động từ
>>> import sys >>> type[sys.path] >>> for path in sys.path: ... print[path] ... /Users/brettmz-admin/dev_trees/psych-214-fall-2016/sphinxext /usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python37.zip /usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7 /usr/local/Cellar/python/3.7.2_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload /Users/brettmz-admin/Library/Python/3.7/lib/python/site-packages /Users/brettmz-admin/dev_trees/grin /Users/brettmz-admin/dev_trees/rmdex /usr/local/lib/python3.7/site-packages4:
import a_module a_module.func[]0