Python|自动安装python库利剑 Easy Install
自动安装python库利剑 ― 三人行服务、品牌电脑销售、系统集成、IT服务外包、软件开发、网络安全、数据恢复、网站建设
自动安装python库利剑
让easy_install构造自己的python小天地
在linux环境中玩、用python,常常需要安装额外的一些python lib
但是这些python库非常难于寻找,而且由于权限的问题,我们一般是无法往系统中/usr/local之类的目录
里面写东西的,而这些额外的lib又非得需要一个site-packages目录来安装
不可,虽然可以指定pure-lib之类的参数,但是很多时候还是会出错,尤其是现在很多python
lib都是使用的setuptools来生成安装包,更是如此要求了,
- [gavin@Korea downloads]$ ll /usr/local/python24/lib/python2.4/site-packages/
- total 24
- drwxr-xr-x 2 root root 4096 Dec 18 16:05 PIL
- -rw-r--r-- 1 root root 4 Dec 18 16:05 PIL.pth
- -rw-r--r-- 1 root root 119 Dec 18 14:59 README
这可麻烦了,每个文件都是root的,旁人只能看,
其实这儿有个很好的解决方法:在自己的目录下面,安装一个虚拟的python:
在自己的目录下面建立一些lib、include等等的目录,构成一个独立的python小天地,这样,系统范围内没有的python库,就可以自己动手安装在自己的小天地中
安装,既不需要root权限,又可以满足自己的需求,一举两得。
这些安装步骤,当然不需要我们重新造轮子,下载
http://peak.telecommunity.com/dist/virtual-python.py
这个脚本,使用你喜欢的python(有的环境提供多个python版本,比如DreamHost)
执行一下这个virtual-python.py, 就会自动在~/下面建立所需的目录(~/bin、/lib、
~/include),以及所需python版本,以及创建python所依赖的其他的.h头文件、.py库文件等等软链接,而且会在~/bin/下面copy一个可以执行的python文件,以后直接使用这个~/bin/python来执行py程序,它就会自动找到额外安装在小天地中的那些python库了。如果觉得把bin、lib、include放在~下面不好可以给 virtual-python.py指定一个�prefix参数:
- [gavin@Korea bin]$ python24 virtual-python.py --prefix=~/python-lib
- [gavin@Korea bin]$ pwd
- /home/gavin/python-lib/bin
- [gavin@Korea bin]$ ll
- total 2360
- -rwxrwxr-x 1 gavin gavin 2404367 Dec 18 16:53 python
会把那些bin、lib、include安装在~/python-lib下面,
这下,你就可以使用easy__install来安装自己额外需要的那些库了,
不过等等先,由于此时使用的easy_install是系统范围的,所以它会把东西安装在
/usr/local之类的目录下,所以我们得给我们自己的环境安装一个easy_install。
下载
- wget http://peak.telecommunity.com/dist/ez_setup.py
- ~/python-lib/bin/python ez_setup.py
- Downloading http://cheeseshop.python.org/packages/2.4/s/setuptools/setuptools-0.6c3-py2.4.egg
- Processing setuptools-0.6c3-py2.4.egg
- creating /home/gavin/python-lib/lib/python2.4/site-packages/setuptools-0.6c3-py2.4.egg
- Extracting setuptools-0.6c3-py2.4.egg to
- /home/gavin/python-lib/lib/python2.4/site-packages
- Adding setuptools 0.6c3 to easy-install.pth file
- Installing easy_install script to /home/gavin/python-lib/bin
- Installing easy_install-2.4 script to /home/gavin/python-lib/bin
-
- Installed /home/gavin/python-lib/lib/python2.4/site-packages/setuptools-0.6c3-py2.4.egg
- Processing dependencies for setuptools==0.6c3
- [gavin@Korea downloads]$ ll ~/python-lib/bin/
- total 2376
- -rwxr-xr-x 1 gavin gavin 298 Dec 18 17:02 easy_install
- -rwxr-xr-x 1 gavin gavin 306 Dec 18 17:02 easy_install-2.4
- -rwxrwxr-x 1 gavin gavin 2404367 Dec 18 16:53 python
好了,我自己的easy_install已经安装好了,就可以使用它来安装
自己想安装的所有东西了,而且不用担心权限的问题:
- [gavin@Korea downloads]$ ll ~/python-lib/lib/python2.4/site-packages/
我好像没有simplejson呢,ok,安装一个,先:
- [gavin@Korea downloads]$ ~/python-lib/bin/easy_install simplejson
- Searching for simplejson
- Reading http://www.python.org/pypi/simplejson/
- Reading http://undefined.org/python/#simplejson
- Reading http://www.python.org/pypi/simplejson/1.4
- Best match: simplejson 1.4
- Downloading http://cheeseshop.python.org/packages/2.4/s/simplejson/simplejson-1.4-py2.4.egg#md5=4f18e31fd095cd54e5015e7b7a147093
- Processing simplejson-1.4-py2.4.egg
- Moving simplejson-1.4-py2.4.egg to
- /home/gavin/python-lib/lib/python2.4/site-packages
- Adding simplejson 1.4 to easy-install.pth file
-
- Installed /home/gavin/python-lib/lib/python2.4/site-packages/simplejson-1.4-py2.4.egg
- Processing dependencies for simplejson
- [gavin@Korea downloads]$ ll ~/python-lib/lib/python2.4/site-packages/
- total 76
- -rw-rw-r-- 1 gavin gavin 241 Dec 18 17:07 easy-install.pth
- lrwxrwxrwx 1 gavin gavin 51 Dec 18 16:53 PIL ->
- /usr/local/python24/lib/python2.4/site-packages/PIL
- lrwxrwxrwx 1 gavin gavin 55 Dec 18 16:53 PIL.pth ->
- /usr/local/python24/lib/python2.4/site-packages/PIL.pth
- lrwxrwxrwx 1 gavin gavin 54 Dec 18 16:53 README ->
- /usr/local/python24/lib/python2.4/site-packages/README
- drwxrwxr-x 4 gavin gavin 4096 Dec 18 17:02 setuptools-0.6c3-py2.4.egg
- -rw-rw-r-- 1 gavin gavin 29 Dec 18 17:02 setuptools.pth
- -rw-rw-r-- 1 gavin gavin 35898 Dec 18 17:07 simplejson-1.4-py2.4.egg
- [gavin@Korea downloads]$
非常漂亮
- [gavin@Korea downloads]$ ~/python-lib/bin/python
- Python 2.4.4 (#1, Dec 18 2006, 14:54:46)
- [GCC 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)] on linux2
- Type "help", "copyright", "credits" or "license" for more information.
- >>> import simplejson
- >>> dir(simplejson)
- ['JSONDecoder', 'JSONEncoder', '__all__', '__builtins__', '__doc__',
- '__file__', '__loader__', '__name__', '__path__', '__version__',
- 'decoder', 'dump', 'dumps', 'encoder', 'load', 'loads', 'read',
- 'scanner', 'write']
- >>>
For basic use of easy_install, you need only supply the filename or URL of a source distribution or .egg file (Python Egg).
Example 1. Install a package by name, searching PyPI for the latest version, and automatically downloading, building, and installing it:
easy_install SQLObject
Example 2. Install or upgrade a package by name and version by finding links on a given "download page":
easy_install -f http://pythonpaste.org/package_index.html SQLObject
Example 3. Download a source distribution from a specified URL, automatically building and installing it:
easy_install http://example.com/path/to/MyPackage-1.2.3.tgz
Example 4. Install an already-downloaded .egg file:
easy_install /my_downloads/OtherPackage-3.2.1-py2.3.egg
Example 5. Upgrade an already-installed package to the latest version listed on PyPI:
easy_install --upgrade PyProtocols
Example 6. Install a source distribution that's already downloaded and extracted in the current directory (New in 0.5a9):
easy_install .
Example 7. (New in 0.6a1) Find a source distribution or Subversion checkout URL for a package, and extract it or check it out to ~/projects/sqlobject (the name will always be in all-lowercase), where it can be examined or edited. (The package will not be installed, but it can easily be installed with easy_install ~/projects/sqlobject. See Editing and Viewing Source Packages below for more info.):
easy_install --editable --build-directory ~/projects SQLObject详细文档可以参考官方网站:http://peak.telecommunity.com/DevCenter/EasyInstall
No comments:
Post a Comment