Hướng dẫn python library path linux - đường dẫn thư viện python linux
Trong Ruby, đường dẫn thư viện được cung cấp trong Show
3, trong Perl, trong 4 - làm thế nào để bạn có được danh sách các đường dẫn mà Python tìm kiếm các mô -đun khi bạn nhập?Nội dung chính ShowShow
Ken Williams 21.8K9 Huy hiệu vàng79 Huy hiệu bạc142 Huy hiệu Đồng9 gold badges79 silver badges142 bronze badges9 gold badges79 silver badges142 bronze badges Hỏi ngày 25 tháng 9 năm 2008 lúc 18:23Sep 25, 2008 at 18:23Sep 25, 2008 at 18:23 Kyle Burtonkyle BurtonKyle BurtonKyle Burton 26.2k9 Huy hiệu vàng 50 Huy hiệu bạc60 Huy hiệu Đồng9 gold badges50 silver badges60 bronze badges9 gold badges50 silver badges60 bronze badges 2 Bạn cũng có thể bổ sung cho đường dẫn này với biến môi trường PythonPath khi chạy, ngoài ra còn:
Đã trả lời ngày 25 tháng 9 năm 2008 lúc 19:02Sep 25, 2008 at 19:02Sep 25, 2008 at 19:02 0 Tôi nghĩ rằng bạn đang tìm kiếm sys.path
Kuilin 1191 Huy hiệu bạc7 Huy hiệu đồng1 silver badge7 bronze badges1 silver badge7 bronze badges Đã trả lời ngày 25 tháng 9 năm 2008 lúc 18:25Sep 25, 2008 at 18:25Sep 25, 2008 at 18:25 Jack M.Jack M.Jack M.Jack M. 28.7K6 Huy hiệu vàng54 Huy hiệu bạc67 Huy hiệu Đồng6 gold badges54 silver badges67 bronze badges6 gold badges54 silver badges67 bronze badges Đã trả lời ngày 25 tháng 9 năm 2008 lúc 18:25Sep 25, 2008 at 18:25Sep 25, 2008 at 18:25 Jack M.Jack M.Jack M.John Millikin 28.7K6 Huy hiệu vàng54 Huy hiệu bạc67 Huy hiệu Đồng6 gold badges54 silver badges67 bronze badges39 gold badges211 silver badges222 bronze badges
Jack M.Jack M.John MillikinFeb 25 at 3:47 28.7K6 Huy hiệu vàng54 Huy hiệu bạc67 Huy hiệu Đồng39 gold badges211 silver badges222 bronze badgesCodeFarmer John Millikinjohn MillikinFeb 25 at 3:4722 silver badges31 bronze badges Phim thương hiệu vàng 194K39CodeFarmer Tôi vừa gặp phải một tình huống mà tôi đã biên soạn và cài đặt Python 2.7.9 từ nguồn trên Ubuntu, nhưng Python không thể tìm thấy các gói tôi đã cài đặt trước đó. Điều này tự nhiên đặt ra câu hỏi - làm thế nào để Python biết nơi tìm các gói khi bạn gọi import sys print (sys.path) 6 5? Bài đăng này áp dụng cụ thể cho Python 2.7.9, nhưng tôi đoán Python 3X hoạt động rất giống nhau. 6.Trong bài đăng này, lần đầu tiên tôi mô tả cách Python tìm thấy các gói, và sau đó tôi sẽ hoàn thành với khám phá mà tôi đã thực hiện liên quan đến Python mặc định vận chuyển với Ubuntu và cách nó khác với Vanilla Python trong cách tìm các gói. > import sys > print '\n'.join(sys.path) /usr/lib/python2.7 /usr/lib/python2.7/plat-x86_64-linux-gnu /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 Nhập khẩu Python làm việc bằng cách tìm kiếm các thư mục được liệt kê trong Làm thế nào sys.path được điềnBạn có thể thao túng import sys print (sys.path) 6 Thuộc tính mô -đun python -c "import sys; print('\n'.join(sys.path))" /usr/local/Cellar//3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python39.zip /usr/local/Cellar//3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9 /usr/local/Cellar//3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload /usr/local/lib/python3.9/site-packages 9Mô -đun > import sys > print '\n'.join(sys.path) /usr/lib/python2.7 /usr/lib/python2.7/plat-x86_64-linux-gnu /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 2 Ubuntu PythonUbuntu Python (import sys, os # This won't work - there is no hi module import hi Traceback (most recent call last): File "", line 1, in ImportError: No module named hi # Create a hi module in your home directory. home_dir = os.path.expanduser("~") my_module_file = os.path.join(home_dir, "hi.py") with open(my_module_file, 'w') as f: f.write('print "hi"\n') f.write('a=10\n') # Add the home directory to sys.path sys.path.append(home_dir) # Now this works, and prints hi! import hi print hi.a 4): Python được tổng hợp từ nguồn (import sys, os # This won't work - there is no hi module import hi Traceback (most recent call last): File "", line 1, in ImportError: No module named hi # Create a hi module in your home directory. home_dir = os.path.expanduser("~") my_module_file = os.path.join(home_dir, "hi.py") with open(my_module_file, 'w') as f: f.write('print "hi"\n') f.write('a=10\n') # Add the home directory to sys.path sys.path.append(home_dir) # Now this works, and prints hi! import hi print hi.a 5)Ken Williams 21.8K9 Huy hiệu vàng79 Huy hiệu bạc142 Huy hiệu Đồng9 gold badges79 silver badges142 bronze badgesNó có một chút liên quan. Bạn có thể thao túng import sys print (sys.path) 6Bạn có thể thao tác 6 trong phiên Python và điều này sẽ thay đổi cách Python tìm thấy các mô -đun. Ví dụ: 8Thuộc tính mô -đun python -c "import sys; print('\n'.join(sys.path))" /usr/local/Cellar//3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python39.zip /usr/local/Cellar//3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9 /usr/local/Cellar//3.9.10/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload /usr/local/lib/python3.9/site-packages 9Khi bạn nhập mô -đun, bạn thường có thể kiểm tra thuộc tính 9 của mô -đun để xem mô -đun nằm trong hệ thống tệp của bạn: 0Tuy nhiên, tài liệu Python nói rằng:
Vì vậy, ví dụ, điều này không hoạt động: 1 Nó có ý nghĩa rằng mô -đun > import sys > print '\n'.join(sys.path) /usr/lib/python2.7 /usr/lib/python2.7/plat-x86_64-linux-gnu /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-packages1 được liên kết tĩnh với trình thông dịch - về cơ bản nó là một phần của trình thông dịch! Mô -đun > import sys > print '\n'.join(sys.path) /usr/lib/python2.7 /usr/lib/python2.7/plat-x86_64-linux-gnu /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 2Python phơi bày toàn bộ hệ thống 5 thông qua mô -đun > import sys > print '\n'.join(sys.path) /usr/lib/python2.7 /usr/lib/python2.7/plat-x86_64-linux-gnu /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-packages2. Điều đó thật tuyệt khi tất cả những thứ này được phơi bày để chúng ta lạm dụng, nếu chúng ta muốn. > import sys > print '\n'.join(sys.path) /usr/lib/python2.7 /usr/lib/python2.7/plat-x86_64-linux-gnu /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-packages5 có thể được sử dụng để tìm một mô -đun: 6Bạn cũng có thể 5 và nguồn Python tùy ý làm mô -đun bằng cách sử dụng > import sys > print '\n'.join(sys.path) /usr/lib/python2.7 /usr/lib/python2.7/plat-x86_64-linux-gnu /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-packages7. Đây là cùng một ví dụ trước đây, ngoại trừ nhập mô -đun của chúng tôi bằng cách sử dụng > import sys > print '\n'.join(sys.path) /usr/lib/python2.7 /usr/lib/python2.7/plat-x86_64-linux-gnu /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-packages2 thay vì thao tác 6: 1Chuyển 80 đến > import sys > print '\n'.join(sys.path) /usr/lib/python2.7 /usr/lib/python2.7/plat-x86_64-linux-gnu /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-packages7 chỉ cần đặt thuộc tính 82 của mô -đun.Ubuntu PythonBây giờ trở lại vấn đề thiếu các gói sau khi cài đặt một phiên bản Python mới được biên dịch từ nguồn. Bằng cách so sánh 6 từ cả Python Ubuntu, cư trú tại 84 và Python mới được cài đặt, cư trú tại 8 5, tôi có thể sắp xếp mọi thứ:Ubuntu Python (import sys, os # This won't work - there is no hi module import hi Traceback (most recent call last): File "", line 1, in ImportError: No module named hi # Create a hi module in your home directory. home_dir = os.path.expanduser("~") my_module_file = os.path.join(home_dir, "hi.py") with open(my_module_file, 'w') as f: f.write('print "hi"\n') f.write('a=10\n') # Add the home directory to sys.path sys.path.append(home_dir) # Now this works, and prints hi! import hi print hi.a 4): 8Python được tổng hợp từ nguồn (import sys, os # This won't work - there is no hi module import hi Traceback (most recent call last): File "", line 1, in ImportError: No module named hi # Create a hi module in your home directory. home_dir = os.path.expanduser("~") my_module_file = os.path.join(home_dir, "hi.py") with open(my_module_file, 'w') as f: f.write('print "hi"\n') f.write('a=10\n') # Add the home directory to sys.path sys.path.append(home_dir) # Now this works, and prints hi! import hi print hi.a 5) 0Hóa ra điều quan trọng đối với tôi là 88 so với 89. Sử dụng Python của Ubuntu, các gói của tôi đã được cài đặt thành 00, trong khi Python mới tôi đã cài đặt các gói dự kiến sẽ được cài đặt vào 01. Tôi chỉ phải thao tác biến môi trường 9 để trỏ đến 88 để có quyền truy cập vào gói được cài đặt trước đó được đóng gói với phiên bản Python mới được cài đặt.Ubuntu đã thao túng import sys
print (sys.path)
|