1. 程式人生 > >python中的lib的一些安裝方法

python中的lib的一些安裝方法

第一篇

Python模組安裝

1. 單檔案模組
直接把檔案拷貝到$python_dir/lib

2. 多檔案模組,帶setup.py
python setup.py install

3. egg檔案
1) 下載ez_setup.py,執行python ez_setup
2) easy_install *.egg
雖然Python的模組可以拷貝安裝,但是一般情況下推薦製作一個安裝包,即寫一個setup.py檔案來安裝。
setup.py檔案的使用:

% python setup.py build #編譯
% python setup.py install    #安裝
% python setup.py sdist     #製作分發包
% python setup.py bdist_wininst #製作windows下的分發包
% python setup.py bdist_rpm

setup.py檔案的編寫
setup.py中主要執行一個 setup函式,該函式中大部分是描述性東西,最主要的是packages引數,列出所有的package,可以用自帶的find_packages來動態獲取package。所以setup.py檔案的編寫實際是很簡單的。
簡單的例子:
setup.py檔案

 from setuptools import setup, find_packages
 setup(
        name = " mytest " ,
        version = " 0.10 " ,
        description = " My test module " ,
        author = " Robin Hood " ,
        url = " http://www.csdn.net " ,
        license = " LGPL " ,
        packages = find_packages(),
        scripts = [ " scripts/test.py " ],
        )

mytest.py

import sys
 def get():
      return sys.path

scripts/test.py

import os
 print os.environ.keys()  

setup中的scripts表示將該檔案放到 Python的Scripts目錄下,可以直接用。
OK,簡單的安裝成功,可以執行所列舉的命令生成安裝包,或者安裝該python包。

本機測試成功(win32-python25)!
==============================================================
setuptools它可以自動的安裝模組,只需要你提供給它一個模組名字就可以,並且自動幫你解決模組的依賴問題。一般情況下用setuptools給安裝的模組會自動放到一個字尾是.egg的目錄裡。

首先,安裝setuptools這個東西,先去下載一個指令碼: http://peak.telecommunity.com/dist/ez_setup.py 下載完後直接執行它就會幫你把setuptools給裝好。
之後,安裝模組的使用方法就是使用一個叫easy_install的命令,在Windows裡,這個命令在python安裝目錄下的scripts裡面,所以需要把scripts加到環境變數的PATH裡,這樣用起來就更方便,linux下不需要注意這個問題。
安裝軟體只需要執行:easy_install 模組名
===================================================
pyinstaller 來建立linux下的python獨立執行檔案

以下內容假定已安裝好Python 2.4/2.5
一、下載並編譯pyinstaller(只需做一次,以後可直接做第二步)
1.下載pyinstaller,現在的版本是1.3
(1)wget http://pyinstaller.hpcf.upr.edu/source/1.3/pyinstaller_1.3.tar.gz

2.解包進入原始碼目錄
(1)tar zxv pyinstaller_1.3.tar.gz
(2)cd pyinstaller-1.3/source/linux

3.編譯原始碼
(1)python Make.py  生成python的 .pyc檔案
如無錯誤,則出現如下提示(只有一行):
Now run "make" to build the targets: ../../support/loader/run ../../support/loader/run_d
(2)make 連線生成linux的 .o 檔案

4.生成編譯配置檔案
(1)python Configure.py  生成config.dat配置檔案

二、編譯獨立執行的python可執行檔案
1.生成spec檔案
python pyinstaller-1.3/Makespec.py --onefile --upx linuxlaptop.py

引數說明:
--onefile 生成單檔案
--upx          生成壓縮的檔案(可減小執行檔案體積,需先安裝upx軟體包)
2.生成最終的可執行檔案
python pyinstaller-1.3/Build.py linuxlaptop.spec

執行完成後將在當前目錄生成可執行的linuxlaptop檔案.
========================================================================
用cx_Freeze把python打包成exe可執行檔案

以前從來沒有試過把Python打包,昨天試了試,發現打包也是一件挺容易的事情.Python打包有很多種選擇.cx_Freeze是一種, py2exe也可以,還有一個pyinstaller.我用的是cx_Freeze,而py2exe因為sf實在太慢連不上.所以沒辦法,只好先用可以下載的.cx_Freeze的資料比較少,不過不是太痛苦,因為好像cx會比py2exe簡便易用,而且好像是跨平臺的,不錯不錯.
    cx_Freeze的用法就一個命令FreezePython.exe,打包也很快捷:
    FreezePython.exe  --install-dir="/your/path/to/install" app.py
    然後就會把app.py以及其依賴的所有模組,包和庫全部歸整後放到--install-dir 所指的路徑下.
    碰到兩個問題,一個是檔案編碼,一開始總是說找不到gb2312的編碼,後面google了一下,發現要這麼做:     
在app.py中新增from encodings import gbk 一句,然後就可以了.    
如果用到了gettext,要注意po檔案中的
    "Content-Type: text/plain; charset=gbk/n"
    "Content-Transfer-Encoding: cp936/n"     
這兩句要這樣寫,字符集要用gbk,不要用gb2312.
    另一個問題是控制檯隱藏:   
預設cx_Freeze打包後都是控制檯程式,就算你打包一個wxPython程式,也會有一個黑黑的控制檯在後面做背景,要去掉控制檯就要這樣:
    FreezePython.exe --install-dir="/your/install/path" --base-binary=Win32GUI.exe app.py
    加了--bash-binary 後就可以只執行前臺的介面了,不過如果程式出錯,會彈出一個錯誤對話方塊,說找不到traceback模組.
    這就要在app.py檔案中加一句:
    import traceback

    把錯誤反饋以對話方塊形式彈出.
    

第二篇

這篇文章主要介紹了Python安裝第三方庫的3種方法,本文講解了通過setuptools來安裝python模組、通過pip來安裝python模組、直接從網上下載下可執行檔案來安裝三種方法,需要的朋友可以參考下

【方法一】: 通過setuptools來安裝python模組

首先下載 http://peak.telecommunity.com/dist/ez_setup.py

NOTE: 最好下載個setuptools,本人是15.2版本,裡面包含了ez_setup

執行 python ez_setup.py

D:\work\installation\setuptools-15.2\setuptools-15.2>python ez_setup.py > 1.txt
Extracting in c:\users\admini~1\appdata\local\temp\tmpbxikxf
Now working in c:\users\admini~1\appdata\local\temp\tmpbxikxf\setuptools-15.2
Installing Setuptools
......
Copying setuptools-15.2-py2.7.egg to c:\python27\lib\site-packages
setuptools 15.2 is already the active version in easy-install.pth
Installing easy_install-script.py script to C:\Python27\Scripts
Installing easy_install.exe script to C:\Python27\Scripts
Installing easy_install-2.7-script.py script to C:\Python27\Scripts
Installing easy_install-2.7.exe script to C:\Python27\Scripts
 
 
Installed c:\python27\lib\site-packages\setuptools-15.2-py2.7.egg
Processing dependencies for setuptools==15.2
Finished processing dependencies for setuptools==15.2

執行 easy_install py

D:\work>easy_install py      #py 為第三方庫檔案
Searching for py
Best match: py 1.4.26
Adding py 1.4.26 to easy-install.pth file
 
Using c:\python27\lib\site-packages
Processing dependencies for py
Finished processing dependencies for py

【方法二】: 通過pip來安裝python模組

安裝 easy_install pip

D:\work>easy_install pip
Searching for pip
Best match: pip 6.1.1
Processing pip-6.1.1-py2.7.egg
pip 6.1.1 is already the active version in easy-install.pth
Installing pip-script.py script to C:\Python27\Scripts
Installing pip.exe script to C:\Python27\Scripts
Installing pip2.7-script.py script to C:\Python27\Scripts
Installing pip2.7.exe script to C:\Python27\Scripts
Installing pip2-script.py script to C:\Python27\Scripts
Installing pip2.exe script to C:\Python27\Scripts
 
Using c:\python27\lib\site-packages\pip-6.1.1-py2.7.egg
Processing dependencies for pip
Finished processing dependencies for pip

執行 pip install xlrd

Usage:   
 pip <command> [options]
 
Commands:
 install           Install packages.
 uninstall          Uninstall packages.
 freeze           Output installed packages in requirements format.
 list            List installed packages.
 show            Show information about installed packages.
 search           Search PyPI for packages.
 wheel            Build wheels from your requirements.
 zip             DEPRECATED. Zip individual packages.
 unzip            DEPRECATED. Unzip individual packages.
 help            Show help for commands.
 
General Options:
 -h, --help         Show help.
 --isolated         Run pip in an isolated mode, ignoring
               environment variables and user configuration.
 -v, --verbose        Give more output. Option is additive, and can be
               used up to 3 times.
 -V, --version        Show version and exit.
 -q, --quiet         Give less output.
 --log <path>        Path to a verbose appending log.
 --proxy <proxy>       Specify a proxy in the form
               [user:
[email protected]
]proxy.server:port.
 --retries <retries>     Maximum number of retries each connection should
               attempt (default 5 times).
 --timeout <sec>       Set the socket timeout (default 15 seconds).
 --exists-action <action>  Default action when a path already exists:
               (s)witch, (i)gnore, (w)ipe, (b)ackup.
 --trusted-host <hostname>  Mark this host as trusted, even though it does
               not have valid or any HTTPS.
 --cert <path>        Path to alternate CA bundle.
 --client-cert <path>    Path to SSL client certificate, a single file
               containing the private key and the certificate
               in PEM format.
 --cache-dir <dir>      Store the cache data in <dir>.
 --no-cache-dir       Disable the cache.
 --disable-pip-version-check
               Don't periodically check PyPI to determine
               whether a new version of pip is available for
               download. Implied with --no-index.

【方法三】:直接從網上下載下可執行檔案來安裝.

比如說,去 >>> pythonlibs <<< 網站,提供了很多Python非官方包下載,二進位制檔案,下載安裝方便.