Hướng dẫn where does python search for files? - python tìm kiếm tệp ở đâu?

\ (\ 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-packages
3

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-packages
4:

import a_module

a_module.func()
0

Đường dẫn tìm kiếm Python là gì?

Với việc sử dụng Python điển hình, biến môi trường PythonPath (hoặc IronPythonPath, v.v.) cung cấp đường dẫn tìm kiếm mặc định cho các tệp mô -đun. Đó là, khi bạn sử dụng câu lệnh nhập từ nhập khẩu ... hoặc nhập, Python tìm kiếm các vị trí sau để có tên phù hợp: các mô-đun tích hợp của Python.the PYTHONPATH environment variable (or IRONPYTHONPATH , etc.) provides the default search path for module files. That is, when you use an from import... or import statement, Python searches the following locations in order for a matching name: Python's built-in modules.

Python lưu tệp theo mặc định ở đâu?

Trên Windows, điều này phụ thuộc vào nơi chúng tôi đã cài đặt Python;Thư mục mặc định là C: \ Python32.Nếu chúng ta chạy vỏ Python từ dòng lệnh, thư mục làm việc hiện tại bắt đầu như thư mục chúng ta đã ở khi chúng ta chạy Python3.Sau đó, chúng tôi đã sử dụng hệ điều hành.c:\Python32. If we run the Python Shell from the command line, the current working directory starts as the directory we were in when we ran python3. Then, we used the os.

Đường dẫn gói Python của tôi ở đâu?

Câu trả lời của bạn..
Các thư mục gói trang web toàn cầu ("Dist-Packages") được liệt kê trong sys.path Khi bạn chạy: trang web Python -M.....
Thư mục Perthon Per Site Packages (PEP 370) là nơi Python cài đặt các gói cục bộ của bạn: Trang web Python -M-Trang web sử dụng ..

Python tìm kiếm các thư viện chung ở đâu?

Chúng ta có thể thấy rằng Python hệ thống chỉ tìm kiếm các thư viện được chia sẻ của nó trong thư mục ~, một số được chỉ định trong Pythonpath và một số thư mục trong/usr/lib và/usr/local/lib (chủ yếu/usr/lib) và chỉ tìm kiếmCác gói hàng thay vì gói trang web.in the ~ directory, some specified in the PYTHONPATH and some directories in /usr/lib and /usr/local/lib (mainly /usr/lib ), and only looks for the dist-packages instead of site-packages.