Các module search path (python 3 docu) là khác nhau có và không có một tập tin kịch bản:
python tương tác thông dịch
(đi cho cả python
và ipython
)
$ python
Python 2.7.3 (default, Dec 18 2014, 19:10:20)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/pymodules/python2.7/gtk-2.0', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client', '/usr/lib/python2.7/dist-packages/ubuntuone-client', '/usr/lib/python2.7/dist-packages/ubuntuone-control-panel', '/usr/lib/python2.7/dist-packages/ubuntuone-couch', '/usr/lib/python2.7/dist-packages/ubuntuone-installer', '/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol']
>>>
Note mục đầu tiên là một chuỗi rỗng. Chuỗi trống là một đường dẫn tương đối tương đương với .
. Các đường dẫn tương đối trong đường dẫn tìm kiếm mô-đun liên quan đến thư mục làm việc hiện tại của tiến trình thông dịch, vì vậy đây chỉ là thư mục làm việc hiện tại nơi bạn gọi trình thông dịch. (Mà trong trường hợp của bạn xảy ra để được thư mục gốc của dự án của bạn.)
thực hiện một tập tin script
$ echo 'import sys' > /tmp/pathtest.py
$ echo 'print(sys.path)' >> /tmp/pathtest.py
$ python /tmp/pathtest.py
['/tmp', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PIL', '/usr/lib/pymodules/python2.7/gtk-2.0', '/usr/lib/python2.7/dist-packages/gst-0.10', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client', '/usr/lib/python2.7/dist-packages/ubuntuone-client', '/usr/lib/python2.7/dist-packages/ubuntuone-control-panel', '/usr/lib/python2.7/dist-packages/ubuntuone-couch', '/usr/lib/python2.7/dist-packages/ubuntuone-installer', '/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol']
Lưu ý rằng ở đây, mục đầu tiên là đường dẫn tuyệt đối của thư mục chứa các tập tin kịch bản chúng tôi thông qua như một đối số.
để cơ bản thêm 'sys nhập; sys.path = [''] + sys.path' vào tập lệnh của bạn – aforaudrey
cũng không bị cám dỗ khi sử dụng 'sys.path.extend ([''])' (như tôi đã làm), vì thứ tự thực hiện một sự khác biệt. – gpano