Hướng dẫn where is python library path? - đường dẫn thư viện python ở đâu?

Trong Ruby, đường dẫn thư viện được cung cấp trong

import sys
print (sys.path)
3, trong Perl, trong
import sys
print (sys.path)
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?

Show

Hướng dẫn where is python library path? - đường dẫn thư viện python ở đâu?

Ken Williams

21.8K9 Huy hiệu vàng79 Huy hiệu bạc142 Huy hiệu Đồng9 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:23

Kyle 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 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:

import sys
sys.path.append('/home/user/python-libs')

Đã trả lời ngày 25 tháng 9 năm 2008 lúc 19:02Sep 25, 2008 at 19:02

Hướng dẫn where is python library path? - đường dẫn thư viện python ở đâu?

0

Tôi nghĩ rằng bạn đang tìm kiếm sys.path

import sys
print (sys.path)

Kuilin

1191 Huy hiệu bạc7 Huy hiệu đồng1 silver badge7 bronze badges

Đã trả lời ngày 25 tháng 9 năm 2008 lúc 18:25Sep 25, 2008 at 18:25

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 badges

Đã trả lời ngày 25 tháng 9 năm 2008 lúc 18:25Sep 25, 2008 at 18:25

Jack M.Jack M.John Millikin

28.7K6 Huy hiệu vàng54 Huy hiệu bạc67 Huy hiệu Đồng39 gold badges211 silver badges222 bronze badges

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

John Millikinjohn MillikinFeb 25 at 3:47

Hướng dẫn where is python library path? - đường dẫn thư viện python ở đâu?

Phim thương hiệu vàng 194K39CodeFarmer

Đã trả lời ngày 25 tháng 2 lúc 3:4722 silver badges31 bronze badges

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)
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.

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 (sys.path) 6

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

import sys
print (sys.path)
6.

Sử dụng Ubuntu 14.04 Python mặc định của tô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

Vì vậy, Python sẽ tìm thấy bất kỳ gói nào đã được cài đặt cho các vị trí đó.

Làm thế nào sys.path được điền

Như các tài liệu giải thích,

import sys
print (sys.path)
6 được điền bằng thư mục làm việc hiện tại, tiếp theo là các thư mục được liệt kê trong biến môi trường
import sys
print (sys.path)
9 của bạn, sau đó là các đường dẫn mặc định phụ thuộc vào cài đặt, được điều khiển bởi 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
0.

Bạn có thể đọc thêm về

import sys
print (sys.path)
6 trong các tài liệu Python.

Giả sử biến môi trường

import sys
print (sys.path)
9 của bạn không được đặt,
import sys
print (sys.path)
6 sẽ bao gồm thư mục làm việc hiện tại cộng với bất kỳ thao tác nào được thực hiện bởi 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
0.

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
0 được tự động nhập khi bạn bắt đầu Python, bạn có thể đọc thêm về cách nó thao tác với
import sys
print (sys.path)
6 của bạn trong các tài liệu Python.

Nó có một chút liên quan.

Bạn có thể thao túng import sys print (sys.path) 6

Bạn có thể thao tác

import sys
print (sys.path)
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ụ:

import sys, os

# This won't work - there is no hi module
import hi 
Traceback (most recent call last):
  File "", line 1, in <module>
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

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 9

Khi bạn nhập mô -đun, bạn thường có thể kiểm tra thuộc tính

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
9 của mô -đun để xem mô -đun nằm trong hệ thống tệp của bạn:

> import numpy
> numpy.__file__
'/usr/local/lib/python2.7/dist-packages/numpy/__init__.pyc'

Tuy nhiên, tài liệu Python nói rằng:

Thuộc tính tệp không có mặt cho các mô -đun C được liên kết tĩnh vào trình thông dịch; Đối với các mô -đun mở rộng được tải động từ một thư viện được chia sẻ, đó là tên đường dẫn của tệp thư viện được chia sẻ.file attribute is not present for C modules that are statically linked into the interpreter; for extension modules loaded dynamically from a shared library, it is the pathname of the shared library file.

Vì vậy, ví dụ, điều này không hoạt động:

> import sys
> sys.__file__
Traceback (most recent call last):
  File "", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'

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-packages
1 đượ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 2

Python phơi bày toàn bộ hệ thống

import sys
print (sys.path)
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-packages
2. Đ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-packages
5 có thể được sử dụng để tìm một mô -đun:

> import imp
> imp.find_module('numpy')
(None, '/usr/local/lib/python2.7/dist-packages/numpy', ('', '', 5))

Bạn cũng có thể

import sys
print (sys.path)
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-packages
7. Đâ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-packages
2 thay vì thao tác
import sys
print (sys.path)
6:

import sys, os, imp

# 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')

# Load the hi module using imp
hi = imp.load_source('hi', my_module_file)

# Now this works, and prints hi!
import hi 
print hi.a # a is 10!
print type(hi) # it's a module!

Chuyển

import sys, os

# This won't work - there is no hi module
import hi 
Traceback (most recent call last):
  File "", line 1, in <module>
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
0 đế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-packages
7 chỉ cần đặt thuộc tính
import sys, os

# This won't work - there is no hi module
import hi 
Traceback (most recent call last):
  File "", line 1, in <module>
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
2 của mô -đun.

Ubuntu Python

Bâ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

import sys
print (sys.path)
6 từ cả Python Ubuntu, cư trú tại
import sys, os

# This won't work - there is no hi module
import hi 
Traceback (most recent call last):
  File "", line 1, in <module>
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 và Python mới được cài đặt, cư trú tại
import sys, os

# This won't work - there is no hi module
import hi 
Traceback (most recent call last):
  File "", line 1, in <module>
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, 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):

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

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)

import sys
print (sys.path)
0

Hóa ra điều quan trọng đối với tôi là

import sys, os

# This won't work - there is no hi module
import hi 
Traceback (most recent call last):
  File "", line 1, in <module>
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
8 so với
import sys, os

# This won't work - there is no hi module
import hi 
Traceback (most recent call last):
  File "", line 1, in <module>
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
9. Sử dụng Python của Ubuntu, các gói của tôi đã được cài đặt thành
> import numpy
> numpy.__file__
'/usr/local/lib/python2.7/dist-packages/numpy/__init__.pyc'
0, trong khi Python mới tôi đã cài đặt các gói dự kiến ​​sẽ được cài đặt vào
> import numpy
> numpy.__file__
'/usr/local/lib/python2.7/dist-packages/numpy/__init__.pyc'
1. Tôi chỉ phải thao tác biến môi trường
import sys
print (sys.path)
9 để trỏ đến
import sys, os

# This won't work - there is no hi module
import hi 
Traceback (most recent call last):
  File "", line 1, in <module>
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
8 để 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)
6 như thế nào?

Vậy làm thế nào để phân phối Ubuntu của Python biết sử dụng

> import numpy
> numpy.__file__
'/usr/local/lib/python2.7/dist-packages/numpy/__init__.pyc'
0 trong
import sys
print (sys.path)
6? Nó được mã hóa cứng vào 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
0 của họ! Đầu tiên, hãy tìm nơi mã 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
0 sống:

import sys
print (sys.path)
1

Dưới đây là một đoạn trích từ

> import numpy
> numpy.__file__
'/usr/local/lib/python2.7/dist-packages/numpy/__init__.pyc'
9 của Ubuntu Python, mà tôi đã nhìn trộm bằng cách mở
> import sys
> sys.__file__
Traceback (most recent call last):
  File "", line 1, in <module>
AttributeError: 'module' object has no attribute '__file__'
0 trong một trình soạn thảo văn bản. Đầu tiên, một bình luận ở đầu:

Đối với Debian và các công cụ phái sinh, hệ thống này được tăng cường với các thư mục cho các gói được phân phối trong phân phối. Các addon cục bộ đi vào/usr/local/lib/python/dist-packages, addon debian cài đặt vào/usr/{lib, chia sẻ}/python/dist-packages. /usr/lib/python/trang web không được sử dụng.

Ok vì vậy bạn có nó. Họ giải thích cách phân phối Python Debian là khác nhau.

Và bây giờ, đối với mã thực hiện thay đổi này:

import sys
print (sys.path)
2

Đó là tất cả ở đó, nếu bạn đủ điên rồ để đào sâu này.

Đi đầu