Tôi bối rối về cách Pycharm xác định đường dẫn Python sử dụng để xác định vị trí các mô -đun và gói.
Trước hết, khi tôi bỏ chọn các cài đặt [trong cả "Bảng điều khiển Python" và cấu hình chạy của tôi], tôi vẫn thấy thư mục cho dự án của mình khi bắt đầu ____10.
Ví dụ: tôi có dự án trong 'vấn đề đường dẫn' và 'chạy "một tệp có chứa
import sys
for p in sys.path:
print p
tôi có
/Users/Rax/Documents/Projects/pathproblem
... [other things in my PYTHONPATH]
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC
/Library/Python/2.7/site-packages
Ngay cả khi tôi đã yêu cầu "Root nội dung" được loại trừ khỏi đường dẫn:
Điều này có thể dẫn đến nhập khẩu thành công các mô -đun sẽ không nhập trong các triển khai điển hình [ví dụ: khi xây dựng một gói].
Nếu tôi kiểm tra cài đặt, tôi sẽ nhận được hai lần:
/Users/Rax/Documents/Projects/pathproblem
... [other things in my PYTHONPATH]
/Users/Rax/Documents/Projects/pathproblem
...
Có vẻ như Pycharm luôn bổ sung gốc dự án hiện tại khi bắt đầu [những gì nó coi là] Pythonpath và cài đặt này chỉ thêm nó vào cuối.
[1] Làm cách nào để định cấu hình pycharm để nó [thực sự] không thêm thư mục dự án vào đường dẫn tìm kiếm gói?
Ngoài ra, gần như tôi có thể nói, Pythonpath, đối với Pycharm, hoàn toàn không phải là hệ thống của tôi, mà là các mục "người dùng đã thêm" trong - thực tế, một cách khó hiểu, ở cuối - cài đặt đường dẫn cho trình thông dịch Python.
[2] Pythonpath của Pycharm đến từ đâu? Đó không phải là Pythonpath mà tôi thấy ở bất cứ nơi nào khác trên hệ thống của mình.
FWIW, Sphinx của Pycharm tôn trọng cài đặt "nội dung gốc", chỉ thêm gốc nội dung khi đường dẫn khi được kiểm tra trong cấu hình xây dựng.
\ [\ newcommand {l} [1] {\ | #1 \ |} \ newcommand {vl} [1] {\ l {\ vec Re} \, [#1]} \ newCommand {i} [1] {\ operatorName {im} \, [#1]} \]
/Users/Rax/Documents/Projects/pathproblem
... [other things in my PYTHONPATH]
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC
/Library/Python/2.7/site-packages
1 là một biến môi trường.environment variable.Xem Python 3 Docs cho Pythonpath.
Biến PythonPath có một giá trị là một chuỗi có danh sách các thư mục mà Python nên thêm vào danh sách thư mục sys.path.sys.path directory list.
Việc sử dụng Pythonpath chính là khi chúng tôi đang phát triển một số mã mà chúng tôi muốn có thể nhập từ Python, nhưng chúng tôi chưa tạo thành một gói Python có thể cài đặt [xem: Tạo gói Python].
Quay trở lại mô -đun ví dụ và tập lệnh trong Python tìm mô -đun ở đâu ?:Where does Python look for modules?:
Nội dung của
/Users/Rax/Documents/Projects/pathproblem
... [other things in my PYTHONPATH]
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC
/Library/Python/2.7/site-packages
2def func[]: print["Running useful function"]
Nội dung của
/Users/Rax/Documents/Projects/pathproblem
... [other things in my PYTHONPATH]
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC
/Library/Python/2.7/site-packages
3import a_module a_module.func[]
Hiện tại, trên máy của tôi, Pythonpath trống:
Trước khi chúng tôi đặt PythonPath chính xác,
/Users/Rax/Documents/Projects/pathproblem
... [other things in my PYTHONPATH]
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC
/Library/Python/2.7/site-packages
4 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'
Bây giờ tôi đặt giá trị biến môi trường PythonPath thành đường dẫn đến thư mục
/Users/Rax/Documents/Projects/pathproblem
... [other things in my PYTHONPATH]
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC
/Library/Python/2.7/site-packages
5:$ # Set PYTHONPATH to path to the working directory + /code $ # This is for the "bash" shell on Unix / git bash on Windows $ export PYTHONPATH="$PWD/code" $ # Now the script can find "a_module" $ python3 scripts/a_script.py Running useful function
Đặt Pythonpath hơn vĩnh viễn;
Có lẽ bạn không muốn đặt Pythonpath mỗi khi bạn bắt đầu một thiết bị đầu cuối và chạy một kịch bản Python.
May mắn thay, chúng ta có thể làm cho giá trị PythonPath được đặt cho bất kỳ phiên đầu cuối nào, bằng cách đặt mặc định biến môi trường.
Ví dụ: hãy nói rằng tôi muốn thêm thư mục
/Users/Rax/Documents/Projects/pathproblem
... [other things in my PYTHONPATH]
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC
/Library/Python/2.7/site-packages
6 vào Pythonpath:Nếu bạn đang ở trên Mac¶
Mở
7;/Users/Rax/Documents/Projects/pathproblem ... [other things in my PYTHONPATH] /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC /Library/Python/2.7/site-packages
Mở tệp
8 trong trình soạn thảo văn bản của bạn - ví dụ:/Users/Rax/Documents/Projects/pathproblem ... [other things in my PYTHONPATH] /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC /Library/Python/2.7/site-packages
9;/Users/Rax/Documents/Projects/pathproblem ... [other things in my PYTHONPATH] /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC /Library/Python/2.7/site-packages
Thêm dòng sau vào cuối:
export PYTHONPATH="/Users/my_user/code"
Lưu các tập tin.
Đóng
7;/Users/Rax/Documents/Projects/pathproblem ... [other things in my PYTHONPATH] /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC /Library/Python/2.7/site-packages
Bắt đầu lại
7, để đọc trong cài đặt mới và nhập cái này:/Users/Rax/Documents/Projects/pathproblem ... [other things in my PYTHONPATH] /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC /Library/Python/2.7/site-packages
Nó sẽ hiển thị một cái gì đó như
6./Users/Rax/Documents/Projects/pathproblem ... [other things in my PYTHONPATH] /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC /Library/Python/2.7/site-packages
Nếu bạn đang ở trên Linux¶
Mở chương trình thiết bị đầu cuối yêu thích của bạn;
Mở tệp
3 trong trình soạn thảo văn bản của bạn - ví dụ:/Users/Rax/Documents/Projects/pathproblem ... [other things in my PYTHONPATH] /Users/Rax/Documents/Projects/pathproblem ...
4;/Users/Rax/Documents/Projects/pathproblem ... [other things in my PYTHONPATH] /Users/Rax/Documents/Projects/pathproblem ...
Thêm dòng sau vào cuối:
export PYTHONPATH=/home/my_user/code
Lưu các tập tin.
Đóng
7;/Users/Rax/Documents/Projects/pathproblem ... [other things in my PYTHONPATH] /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC /Library/Python/2.7/site-packages
Bắt đầu lại
7, để đọc trong cài đặt mới và nhập cái này:/Users/Rax/Documents/Projects/pathproblem ... [other things in my PYTHONPATH] /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC /Library/Python/2.7/site-packages
Nó sẽ hiển thị một cái gì đó như
6./Users/Rax/Documents/Projects/pathproblem ... [other things in my PYTHONPATH] /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC /Library/Python/2.7/site-packages
Nếu bạn đang ở trên Linux¶
Mở chương trình thiết bị đầu cuối yêu thích của bạn;
Mở tệp
/Users/Rax/Documents/Projects/pathproblem
... [other things in my PYTHONPATH]
/Users/Rax/Documents/Projects/pathproblem
...
3 trong trình soạn thảo văn bản của bạn - ví dụ: /Users/Rax/Documents/Projects/pathproblem
... [other things in my PYTHONPATH]
/Users/Rax/Documents/Projects/pathproblem
...
4;Đóng ứng dụng thiết bị đầu cuối của bạn;
Bắt đầu lại ứng dụng Terminal của bạn, để đọc trong cài đặt mới và nhập cái này:
Nó sẽ hiển thị một cái gì đó như
/Users/Rax/Documents/Projects/pathproblem
... [other things in my PYTHONPATH]
/Users/Rax/Documents/Projects/pathproblem
...
5.Nếu bạn đang ở trên Windows¶
Đã đến menu Windows, nhấp chuột phải vào máy tính trên máy tính và chọn các thuộc tính trên mạng.
Từ hộp thoại Thuộc tính máy tính, chọn Cài đặt hệ thống nâng cao trên bên trái:
Từ hộp thoại Cài đặt hệ thống nâng cao, chọn nút Biến Môi trường trên mạng:
Trong hộp thoại trong các biến môi trường, nhấp vào nút mới của New New ở nửa trên của hộp thoại, để tạo một biến người dùng mới:
>>> import os >>> os.environ['PYTHONPATH'] '/home/my_user/code'