Hướng dẫn add content root to python path - thêm nội dung gốc vào đường dẫn python

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:

Hướng dẫn add content root to python path - thêm nội dung gốc vào đường dẫn python

Đ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
2

def 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
3

import 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ở

    /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
    
    7;

  • Mở tệp

    /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
    
    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;

  • Thêm dòng sau vào cuối:

    export PYTHONPATH="/Users/my_user/code"
    

  • Lưu các tập tin.

  • Đóng

    /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
    
    7;

  • Bắt đầu lại

    /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
    
    7, để đọ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)
    /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.

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;

  • Thêm dòng sau vào cuối:

    export PYTHONPATH=/home/my_user/code
    

    Lưu các tập tin.

  • Đóng

    /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
    
    7;

  • Bắt đầu lại

    /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
    
    7, để đọ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)
    /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.

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'

PATH $ PATH trong Python là gì?

Nói một cách đơn giản, nó được sử dụng bởi các mô-đun do người dùng xác định để đặt đường dẫn để chúng có thể được nhập trực tiếp vào chương trình Python. Nó cũng xử lý đường dẫn tìm kiếm mặc định cho các mô -đun trong Python. Biến PythonPath bao gồm các thư mục khác nhau như một chuỗi được thêm vào SYS.used by user-defined modules to set the path so that they can be directly imported into a Python program. It also handles the default search path for modules in Python. PYTHONPATH variable includes various directories as a string to be added to the sys.

Sys Path có phải là thực hành xấu không?

Hầu hết thời gian, nối vào sys.path là một giải pháp kém.Tốt hơn là chăm sóc những gì Pythonpath của bạn được đặt thành: Kiểm tra xem nó có chứa thư mục gốc của bạn không (chứa các gói cấp cao nhất của bạn) và không có gì khác (ngoại trừ các gói trang web mà tôi). path is a poor solution. It is better to take care of what your PYTHONPATH is set to : check that it contains your root directory (which contains your top-level packages) and nothing else (except site-packages which i).

Root nội dung trong pycharm là gì?

Root nội dung là một thư mục chứa các tệp tạo nên dự án của bạn.Nguồn gốc (hoặc thư mục nguồn; hiển thị là).Những gốc này chứa các tệp và tài nguyên nguồn thực tế.Pycharm sử dụng rễ nguồn làm điểm bắt đầu để giải quyết nhập khẩu.a folder that contains the files that make up your project. Source roots (or source folders; shown as ). These roots contain the actual source files and resources. PyCharm uses the source roots as the starting point for resolving imports.

Làm thế nào thêm pythonpath linux?

Nếu bạn đang sử dụng hương vị tiêu chuẩn của Linux, hãy mở Shell Bash và nhập cụm từ sau, Xuất đường dẫn = đường dẫn $:/usr/local/bin/python và nhấn enter.Nếu bạn có quyền truy cập vào shell sh hoặc ksh, thì hãy mở thiết bị đầu cuối và nhập các loại sau, đường dẫn = đường dẫn $:/usr/local/bin/python và nhấn enter.open up the bash shell and type the following phrase, export PATH=”$PATH:/usr/local/bin/python” and press Enter. If you have access to either sh or ksh shell, then open up the terminal and type the following, PATH=”$PATH:/usr/local/bin/python” and press Enter.