Python的強大,其中一個重要原因是Python有很豐富的庫(模塊)從而可以比較方便地處理各種各樣的問題。Python的第三方modules一般都安裝在一些固定的路徑,如下:
Unix(Linux): prefix/lib/pythonX.Y/site-packages 默認路徑:/usr/local/lib/pythonX.Y/site-packages
Windows: prefix\Lib\site-packages 默認路徑:C:\PythonXY\Lib\site-packages
另外,在Unix-like系統上,Python自身build-in的模塊一般位於:/usr/lib/pythonX.Y/site-packages
從源代碼安裝模塊的命令一般為:setup.py install
當然,可以根據需要改變默認的第三方模塊安裝路徑,在命令中可以加上參數:–user, or –home, or –prefix and –exec-prefix, or –install-base and –install-platbase 等來指定安裝路徑。
需要注意的是:模塊的安裝路徑一定要在 sys.path 這個List中,才能在腳本中可以正常地 import 進來。
關於模塊的裝, Python官方參考文檔是:http://docs.python.org/3.3/install/index.html#how-installation-works
另外,在Debian系列(包括Ubuntu) 的Linux上,一般采用 dist-packages 而不是采用 site-packages 目錄;Debian項目的網站上,也對此作了說明,詳見:http://wiki.debian.org /Python#Deviations_from_upstream
下面是我的系統上看到的Python模塊的一些路徑:
# 在一台RHEL6.3 x86-64系統上
[root@jay-linux ~]# cat /etc/issue
Red Hat Enterprise Linux Server release 6.3 (Santiago) Kernel \r on an \m [root@jay-linux ~]# ls /usr/lib/python2.6/site-packages/ [root@jay-linux ~]# ls /usr/lib64/python2.6/site-packages/ [root@jay-linux ~]# ls /usr/local/lib64/python2.6/site-packages/ # 切換到一台Ubuntu x86-64系統上 master@jay-intel:~$ cat /etc/issue Ubuntu 12.04.1 LTS \n \l master@jay-intel:~$ ls /usr/lib/python2.7/dist-packages/ master@jay-intel:~$ ls /usr/local/lib/python2.7/dist-packages/ easy-install.pth mysql_connector_repackaged-0.3.1-py2.7.egg master@jay-intel:~$ python3 Python 3.2.3 (default, Oct 19 2012, 20:10:41) [gcc 4.6.3] on linux2 type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.path ['', '/usr/local/lib/python3.2/dist-packages/mysql_connector_repackaged-0.3.1-py3.2.egg', '/usr/lib/python3.2', '/usr/lib/python3.2/plat-linux2', '/usr/lib/python3.2/lib-dynload', '/usr/local/lib/python3.2/dist-packages', '/usr/lib/python3/dist-packages'] >>>
