您的位置:首页 > 编程语言 > Python开发

python中的lib的一些安装方法

2016-01-12 12:11 726 查看
第一篇

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:passwd@]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非官方包下载,二进制文件,下载安装方便.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: