Tôi đã cố gắng thêm thư mục tùy chỉnh vào PYTHONPATH
theo lời khuyên trên bài đăng này Permanently add a directory to PYTHONPATH. Tôi đang sử dụng bash trên máy Mac, nếu điều đó có liên quan. Đây là những gì tôi đã làm:Thêm thư mục tùy chỉnh vào PYTHONPATH
open ~/.bash_profile
export PYTHONPATH="${PYTHONPATH}:/Users/Zhengnan/Library/Python/2.7/lib/python/site-packages"
và lưusource ~/.bash_profile
Có hai vấn đề:
- Khi tôi chạy
sys.path
bên trong một Python IDE, dir dự định vẫn không hiển thị. - Khi tôi kích hoạt Python trong Terminal và chạy
sys.path
ở đó, thư mục đã hiển thị, nhưng tất cả các thư mục khác không khớp với những gì tôi nhận được từ bước trước.
Cụ thể, đây là những gì tôi nhận được từ việc chạy sys.path
bên trong IDE. Không thể tìm thấy thư mục dự định.
sys.path [ '', '/Applications/Spyder-Py2.app/Contents/Resources', '/Applications/Spyder-Py2.app/Contents/Resources/lib /python27.zip ', ' /Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7 ', ' /Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/plat -darwin ', ' /Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/plat-mac ', ' /Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/plat-mac/lib-scriptpackages ', '/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/lib-tk', '/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/lib-old' , '/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/lib-dynload', '/Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/site- packages.zip ', ' /Applications/Spyder-Py2.app/Contents/Resources/lib/python2.7/site-packages ', ' /Applications/Spyder-Py2.app/Contents/Resources/lib/python2. 7/IPython/extensions', '/Users/Zhengnan/.ipython']
Một d đây là những gì tôi nhận được từ chạy sys.path
từ Thiết bị đầu cuối. Thư mục dự định là phần tử thứ ba trong danh sách.
sys.path [ '', '/ Users/Zhengnan', '/Users/Zhengnan/Library/Python/2.7/lib/python/site-packages', '/ System/Thư viện/Khung/Python.framework/Phiên bản/2.7/lib/python27.zip ',' /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7 ','/System/Library/Frameworks/Python.framework/Phiên bản/2.7/lib/python2.7/plat-darwin ',' /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac ','/System/Thư viện/Khung/Python.framework/Phiên bản/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. khuôn khổ/Versions/2.7/Extras/lib/python/PyObjC ']
tôi nên đề cập rằng lý do tôi muốn thêm dir tùy chỉnh này để PYTHONPATH
là mỗi khi tôi pip install
một gói, nó được cài đặt trong /Users/Zhengnan/Library/Python/2.7/lib/python/site-packages
và tôi không muốn sys.path.append
mỗi lần tôi chạy tập lệnh. Xin cho biết. Cảm ơn.
Xem http://stackoverflow.com/questions/135688/setting-environment-variables-in-os-x – cdarke