Linux配置python开发环境

版权声明:Gtlions Lai原创,转载请注明. https://blog.csdn.net/gtlions/article/details/8043907
由于之前需要用到第3方的扩展,但是centos5自带的python是2.4.3,这个版本很多第3放扩展工具无法支持,虽然后来手工升级到了2.7.2,但是还有有些动态文件无法自动创建链接成功,无奈之下只好选择了centos6,这个版本的自带的python是2.6.6,应该能被大部分的工具支持,在此记录下在rhl6环境下构建一个python开发环境的配置过程。
现在我想安装配置python、pyinstaller、fabric、pexpect、eclipse等工具。

0. 环境配置
[root@gtlionsdev ~]# hostname
gtlionsdev
[root@gtlionsdev ~]# cat /etc/issue
CentOS release 6.3 (Final)
Kernel \r on an \m

[root@gtlionsdev ~]# uname -a
Linux gtlionsdev 2.6.32-279.el6.i686 #1 SMP Fri Jun 22 10:59:55 UTC 2012 i686 i686 i386 GNU/Linux
[root@gtlionsdev ~]# python -V
Python 2.6.6
[root@gtlionsdev ~]# ldd /usr/bin/python
    linux-gate.so.1 =>  (0x00de4000)
    libpython2.6.so.1.0 => /usr/lib/libpython2.6.so.1.0 (0x00b84000)
    libpthread.so.0 => /lib/libpthread.so.0 (0x00f8e000)
    libdl.so.2 => /lib/libdl.so.2 (0x00605000)
    libutil.so.1 => /lib/libutil.so.1 (0x00110000)
    libm.so.6 => /lib/libm.so.6 (0x005c6000)
    libc.so.6 => /lib/libc.so.6 (0x00179000)
    /lib/ld-linux.so.2 (0x00fab000)
[root@gtlionsdev ~]# file /usr/bin/python
/usr/bin/python: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped

1. 安装配置pyinstaller
首先需要安装下lsb-sdk,解压后直接rpm -Uivh *.rpm即可。
[root@gtlionsdev ~]# cd /opt
[root@gtlionsdev opt]# ls
pyinstaller-2.0.tar.bz2  rh
[root@gtlionsdev opt]# tar -jxf pyinstaller-2.0.tar.bz2;mv pyinstaller-2.0 pyinstaller;cd pyinstaller;cd source/
[root@gtlionsdev source]# python waf configure build install
Linux-32bit detected
Building LSB bootloader.
Checking for program gcc or cc           : /usr/bin/gcc
Checking for program cpp                 : /usr/bin/cpp
Checking for program ar                  : /usr/bin/ar
Checking for program ranlib              : /usr/bin/ranlib
Checking for gcc                         : ok  
Checking for program python              : /usr/bin/python
Checking for Python version              : 2.6.6
Checking for library python2.6           : yes
Checking for program python2.6-config    : /usr/bin/python2.6-config
Checking for custom code                 : yes
Checking for program lsbcc               : not found
Checking for program /opt/lsb/bin/lsbcc  : not found
LSB (Linux Standard Base) tools >= 4.0 are required.
Try --no-lsb option if not interested in building LSB binary.
[root@gtlionsdev source]# python waf configure --no-lsb build install --如果没有lsb
Linux-32bit detected
Checking for program gcc or cc           : /usr/bin/gcc
Checking for program cpp                 : /usr/bin/cpp
Checking for program ar                  : /usr/bin/ar
Checking for program ranlib              : /usr/bin/ranlib
Checking for gcc                         : ok  
Checking for program python              : /usr/bin/python
Checking for Python version              : 2.6.6
Checking for library python2.6           : yes
Checking for program python2.6-config    : /usr/bin/python2.6-config
Checking for custom code                 : yes
Checking for library z                   : yes
Checking for function readlink           : yes
Checking for flags -m32                  : yes
Checking for flags -Wl,--as-needed       : yes
'configure' finished successfully (1.014s)
Waf: Entering directory `/opt/pyinstaller/source/build'
[ 1/20] cc: common/launch.c -> build/debug/common/launch_1.o
[ 2/20] cc: common/main.c -> build/debug/common/main_1.o
[ 3/20] cc: linux/getpath.c -> build/debug/linux/getpath_1.o
[ 4/20] cc: linux/utils.c -> build/debug/linux/utils_1.o
[ 5/20] cc: common/launch.c -> build/release/common/launch_2.o
[ 6/20] cc: common/main.c -> build/release/common/main_2.o
[ 7/20] cc: linux/getpath.c -> build/release/linux/getpath_2.o
[ 8/20] cc: linux/utils.c -> build/release/linux/utils_2.o
[ 9/20] cc: common/launch.c -> build/releasew/common/launch_3.o
[10/20] cc: common/main.c -> build/releasew/common/main_3.o
[11/20] cc: linux/getpath.c -> build/releasew/linux/getpath_3.o
[12/20] cc: linux/utils.c -> build/releasew/linux/utils_3.o
[13/20] cc: common/launch.c -> build/debugw/common/launch_4.o
[14/20] cc: common/main.c -> build/debugw/common/main_4.o
[15/20] cc: linux/getpath.c -> build/debugw/linux/getpath_4.o
[16/20] cc: linux/utils.c -> build/debugw/linux/utils_4.o
[17/20] cc_link: build/debug/common/launch_1.o build/debug/common/main_1.o build/debug/linux/getpath_1.o build/debug/linux/utils_1.o -> build/debug/run_d
[18/20] cc_link: build/release/common/launch_2.o build/release/common/main_2.o build/release/linux/getpath_2.o build/release/linux/utils_2.o -> build/release/run
[19/20] cc_link: build/releasew/common/launch_3.o build/releasew/common/main_3.o build/releasew/linux/getpath_3.o build/releasew/linux/utils_3.o -> build/releasew/runw
[20/20] cc_link: build/debugw/common/launch_4.o build/debugw/common/main_4.o build/debugw/linux/getpath_4.o build/debugw/linux/utils_4.o -> build/debugw/runw_d
Waf: Leaving directory `/opt/pyinstaller/source/build'
'build' finished successfully (3.672s)
Waf: Entering directory `/opt/pyinstaller/source/build'
* installing build/debug/run_d as ../../support/loader/Linux-32bit/run_d
* installing build/release/run as ../../support/loader/Linux-32bit/run
* installing build/releasew/runw as ../../support/loader/Linux-32bit/runw
* installing build/debugw/runw_d as ../../support/loader/Linux-32bit/runw_d
Waf: Leaving directory `/opt/pyinstaller/source/build'
'install' finished successfully (0.058s)
这样pyinstaller就安装成功了,先写个简单的程序然后测试下打包。

2. 第1支py程序
首先我简单的写了几行程序用来输出一行文字和显示本机磁盘的使用情况信息,并执行。
[root@gtlionsdev source]# mkdir -p /opt/pysrc;cd /opt/pysrc
[root@gtlionsdev pysrc]# vi test1.py
[root@gtlionsdev pysrc]# cat test1.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# Name:
# Filename : NewPython.py

# What:

# Version:
# Release:   2012-9-26
# Author:    Gtlions
# Copyright: Copyright (c) Gtlions 2012 <g.t.lions@gmail.com>   
# Licence:   <Gtlions's licence>
#-------------------------------------------------------------------------------
import os
print 'i will show the host disk info.'
os.system('df -h')

[root@gtlionsdev pysrc]# python test1.py
i will show the host disk info.
文件系统          容量  已用  可用 已用%% 挂载点
/dev/sda2             143G  3.0G  133G   3% /
tmpfs                 1.5G     0  1.5G   0% /dev/shm

3. pyinstaller打包程序
[root@gtlionsdev opt]# ls
pyinstaller  pyinstaller-2.0.tar.bz2  pysrc  rh
[root@gtlionsdev opt]# mkdir pyexe
[root@gtlionsdev opt]# python /opt/pyinstaller/pyinstaller.py -F --out=/opt/pyexe /opt/pysrc/test1.py
126 INFO: wrote /opt/pyexe/test1.spec
259 INFO: UPX is not available.
2999 INFO: checking Analysis
2999 INFO: building Analysis because out00-Analysis.toc non existent
3000 INFO: running Analysis out00-Analysis.toc
3066 INFO: Analyzing /opt/pyinstaller/support/_pyi_bootstrap.py
5257 INFO: Analyzing /opt/pyinstaller/PyInstaller/loader/archive.py
5363 INFO: Analyzing /opt/pyinstaller/PyInstaller/loader/carchive.py
5463 INFO: Analyzing /opt/pyinstaller/PyInstaller/loader/iu.py
5513 INFO: Analyzing /opt/pysrc/test1.py
5598 INFO: Hidden import 'encodings' has been found otherwise
5599 INFO: Looking for run-time hooks
5600 INFO: Analyzing rthook /opt/pyinstaller/support/rthooks/pyi_rth_encodings.py
6310 INFO: Warnings written to /opt/pyexe/build/pyi.linux2/test1/warntest1.txt
6318 INFO: checking PYZ
6318 INFO: rebuilding out00-PYZ.toc because out00-PYZ.pyz is missing
6319 INFO: building PYZ out00-PYZ.toc
8145 INFO: checking PKG
8145 INFO: rebuilding out00-PKG.toc because out00-PKG.pkg is missing
8146 INFO: building PKG out00-PKG.pkg
9906 INFO: checking EXE
9906 INFO: rebuilding out00-EXE.toc because test1 missing
9906 INFO: building EXE from out00-EXE.toc
9925 INFO: Appending archive to EXE /opt/pyexe/dist/test1

测试执行下打包好的程序。
[root@gtlionsdev opt]# /opt/pyexe/dist/test1
i will show the host disk info.
文件系统          容量  已用  可用 已用%% 挂载点
/dev/sda2             143G  3.2G  133G   3% /
tmpfs                 1.5G     0  1.5G   0% /dev/shm
可以看到这个程序已经可以运行了,这样就可以随便分发到其他主机运行了,下面是在别的机器运行的过程。
[root@db233 ~]# cat /etc/issue
Red Hat Enterprise Linux Server release 5 (Tikanga)
Kernel \r on an \m

[root@db233 ~]# ./test1
-bash: ./test1: 权限不够
[root@db233 ~]# chmod +x test1
[root@db233 ~]# ./test1
i will show the host disk info.
文件系统              容量  已用 可用 已用% 挂载点
/dev/mapper/VolGroup00-LogVol00
                      130G   51G   73G  42% /
/dev/sda1              99M   12M   82M  13% /boot
tmpfs                 471M     0  471M   0% /dev/shm


4. 安装fabric
[root@gtlionsdev opt]# curl -O https://raw.github.com/pypa/virtualenv/master/virtualenv.py
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  107k  100  107k    0     0  35020      0  0:00:03  0:00:03 --:--:-- 73944
[root@gtlionsdev opt]# python virtualenv.py fabric
New python executable in fabric/bin/python
Installing setuptools............................done.
Installing pip.....................done.
[root@gtlionsdev opt]# . fabric/bin/activate
(fabric)[root@gtlionsdev opt]# pip install fabric
............................
............................
安装完成记得把环境变量包含/opt/fabric/bin进PATH。

5. 第2支py程序
[root@gtlionsdev ~]# cd /opt/pysrc
[root@gtlionsdev pysrc]# vi fabfile.py
[root@gtlionsdev pysrc]# cat fabfile.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# Name:
# Filename : NewPython.py

# What:

# Version:
# Release:   2012-9-26
# Author:    Gtlions
# Copyright: Copyright (c) Gtlions 2012 <g.t.lions@gmail.com>   
# Licence:   <Gtlions's licence>
#-------------------------------------------------------------------------------
from fabric.api import *
env.hosts=['root@192.168.2.3','root@192.168.2.233','rbt@192.168.2.240']
env.passwords={'root@192.168.2.3':'hwttportal3','root@192.168.2.233':'hwttmcp','rbt@192.168.2.240':'rbtdev'}
def hostname():
    '''show hostname'''
        run('hostname')
def disk():
    '''show diskinfo'''
        run('df -h')
运行查看下结果。
[root@gtlionsdev pysrc]# fab hostname disk
[root@192.168.2.3] Executing task 'hostname'
[root@192.168.2.3] run: hostname
[root@192.168.2.3] out: db3

[root@192.168.2.233] Executing task 'hostname'
[root@192.168.2.233] run: hostname
[root@192.168.2.233] out: db233

[rbt@192.168.2.240] Executing task 'hostname'
[rbt@192.168.2.240] run: hostname
[rbt@192.168.2.240] out: vm1

[root@192.168.2.3] Executing task 'disk'
[root@192.168.2.3] run: df -h
[root@192.168.2.3] out: 文件系统              容量  已用 可用 已用% 挂载点
[root@192.168.2.3] out: /dev/cciss/c0d0p8     2.9G  933M  1.8G  34% /
[root@192.168.2.3] out: /dev/cciss/c0d0p9     609G  259G  319G  45% /usr
[root@192.168.2.3] out: /dev/cciss/c0d0p7     2.9G   70M  2.7G   3% /tmp
[root@192.168.2.3] out: /dev/cciss/c0d0p6     4.8G  4.1G  437M  91% /home
[root@192.168.2.3] out: /dev/cciss/c0d0p5     4.8G  180M  4.4G   4% /var/log
[root@192.168.2.3] out: /dev/cciss/c0d0p2      29G  6.6G   21G  25% /opt
[root@192.168.2.3] out: /dev/cciss/c0d0p1     2.9G   93M  2.6G   4% /boot
[root@192.168.2.3] out: tmpfs                 1.8G     0  1.8G   0% /dev/shm

[root@192.168.2.233] Executing task 'disk'
[root@192.168.2.233] run: df -h
[root@192.168.2.233] out: 文件系统              容量  已用 可用 已用% 挂载点
[root@192.168.2.233] out: /dev/mapper/VolGroup00-LogVol00
[root@192.168.2.233] out:                       130G   51G   73G  42% /
[root@192.168.2.233] out: /dev/sda1              99M   12M   82M  13% /boot
[root@192.168.2.233] out: tmpfs                 471M     0  471M   0% /dev/shm

[rbt@192.168.2.240] Executing task 'disk'
[rbt@192.168.2.240] run: df -h
[rbt@192.168.2.240] out: 文件系统              容量  已用 可用 已用% 挂载点
[rbt@192.168.2.240] out: /dev/sda1             6.8G  5.3G  1.2G  82% /
[rbt@192.168.2.240] out: tmpfs                 252M     0  252M   0% /dev/shm
[rbt@192.168.2.240] out: /dev/mapper/vgmain-lv_1
[rbt@192.168.2.240] out:                       9.9G  6.4G  3.0G  69% /usr/app
[rbt@192.168.2.240] out: 192.168.2.3:/usr/uploadrootdir
[rbt@192.168.2.240] out:                       609G  259G  319G  45% /usr/uploadrootdir


Done.
Disconnecting from rbt@192.168.2.240... done.
Disconnecting from 192.168.2.3... done.
Disconnecting from 192.168.2.233... done.        

再次打包这个新程序,查看下fab和pyinstaller结合情况如何:
[root@gtlionsdev pysrc]# python /opt/pyinstaller/pyinstaller.py -F --out=/opt/pyexe /opt/pysrc/fabfile.py
20 INFO: wrote /opt/pyexe/fabfile.spec
72 INFO: UPX is not available.
2177 INFO: checking Analysis
2177 INFO: building Analysis because out00-Analysis.toc non existent
2178 INFO: running Analysis out00-Analysis.toc
2208 INFO: Analyzing /opt/pyinstaller/support/_pyi_bootstrap.py
4357 INFO: Analyzing /opt/pyinstaller/PyInstaller/loader/archive.py
4461 INFO: Analyzing /opt/pyinstaller/PyInstaller/loader/carchive.py
4559 INFO: Analyzing /opt/pyinstaller/PyInstaller/loader/iu.py
4608 INFO: Analyzing /opt/pysrc/fabfile.py
4615 INFO: Hidden import 'encodings' has been found otherwise
4615 INFO: Looking for run-time hooks
4616 INFO: Analyzing rthook /opt/pyinstaller/support/rthooks/pyi_rth_encodings.py
5196 INFO: Warnings written to /opt/pyexe/build/pyi.linux2/fabfile/warnfabfile.txt
5204 INFO: checking PYZ
5205 INFO: rebuilding out00-PYZ.toc because out00-PYZ.pyz is missing
5205 INFO: building PYZ out00-PYZ.toc
6229 INFO: checking PKG
6230 INFO: rebuilding out00-PKG.toc because out00-PKG.pkg is missing
6230 INFO: building PKG out00-PKG.pkg
7922 INFO: checking EXE
7923 INFO: rebuilding out00-EXE.toc because fabfile missing
7923 INFO: building EXE from out00-EXE.toc
7924 INFO: Appending archive to EXE /opt/pyexe/dist/fabfile
执行打包好的程序:
[root@gtlionsdev pysrc]# /opt/pyexe/dist/fabfile hostname disk
Traceback (most recent call last):
  File "<string>", line 15, in <module>
  File "/opt/pyinstaller/PyInstaller/loader/iu.py", line 409, in importHook
    raise ImportError("No module named %s" % fqname)
ImportError: No module named fabric
[root@gtlionsdev pysrc]# ll /opt/pyexe/dist/fabfile
-rwxr-xr-x. 1 root root 2350822 9?. 28 19:34 /opt/pyexe/dist/fabfile
[root@gtlionsdev pysrc]# cd /opt/pyexe/dist/
[root@gtlionsdev dist]# ./fabfile
Traceback (most recent call last):
  File "<string>", line 15, in <module>
  File "/opt/pyinstaller/PyInstaller/loader/iu.py", line 409, in importHook
    raise ImportError("No module named %s" % fqname)
ImportError: No module named fabric
[root@gtlionsdev dist]# ./fabfile disk
Traceback (most recent call last):
  File "<string>", line 15, in <module>
  File "/opt/pyinstaller/PyInstaller/loader/iu.py", line 409, in importHook
    raise ImportError("No module named %s" % fqname)
ImportError: No module named fabric
[root@gtlionsdev dist]# ./fabfile hostname
Traceback (most recent call last):
  File "<string>", line 15, in <module>
  File "/opt/pyinstaller/PyInstaller/loader/iu.py", line 409, in importHook
    raise ImportError("No module named %s" % fqname)
ImportError: No module named fabric
[root@gtlionsdev dist]# fab fabfile hostname

Fatal error: Couldn't find any fabfiles!

Remember that -f can be used to specify fabfile path, and use -h for help.

Aborting.
[root@gtlionsdev dist]# fab -f fabfile hostname

Fatal error: Couldn't find any fabfiles!

Remember that -f can be used to specify fabfile path, and use -h for help.

Aborting.
[root@gtlionsdev dist]# fab -f ./fabfile hostname

Fatal error: Couldn't find any fabfiles!

Remember that -f can be used to specify fabfile path, and use -h for help.

Aborting.
[root@gtlionsdev dist]# fab -f ./fabfile -l

Fatal error: Couldn't find any fabfiles!

Remember that -f can be used to specify fabfile path, and use -h for help.

Aborting.
[root@gtlionsdev dist]# fab -f ./fabfile fabfile

Fatal error: Couldn't find any fabfiles!

Remember that -f can be used to specify fabfile path, and use -h for help.

Aborting.
矮油,明显各种失败了,去google了好长时间,没发现有解决方案,只好放弃。看来如果需要封装貌似没办法用fab,只好转战pexpect了。

6. 安装配置pexpect
[root@gtlionsdev opt]# tar -zxf pexpect-2.3.tar.gz;cd pexpect-2.3;python setup.py install
running install
running build
running build_py
running install_lib
running install_egg_info
Removing /usr/lib/python2.6/site-packages/pexpect-2.3-py2.6.egg-info
Writing /usr/lib/python2.6/site-packages/pexpect-2.3-py2.6.egg-info
这样就完成安装了。

7. 安装配置expect
[root@dev expect]# ls
expect5.45.tar.gz  tcl8511-src.zip
[root@dev expect]# tar -zxf expect5.45.tar.gz;unzip -q tcl8511-src.zip
[root@dev expect]# cd expect5.45;./configure
checking for correct TEA configuration... ok (TEA 3.9)
configure: configuring expect 5.45
checking for Tcl configuration... configure: error: Can't find Tcl configuration definitions
[root@dev expect5.45]# cd ../tcl8.5.11/unix/;./configure;make;make install
安装tcl之后返回继续安装expect,./configure;make;make install

8. 安装配置PyH
[root@dev opt]# wget http://pyh.googlecode.com/files/PyH-0.1.1.tar.gz
--2012-09-30 14:53:54--  http://pyh.googlecode.com/files/PyH-0.1.1.tar.gz
正在解析主机 pyh.googlecode.com... 74.125.128.82
正在连接 pyh.googlecode.com|74.125.128.82|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:2086 (2.0K) [application/x-gzip]
正在保存至: “PyH-0.1.1.tar.gz”

100%[=====================================================================================================================================================>] 2,086       --.-K/s   in 0.007s  

2012-09-30 14:53:55 (295 KB/s) - 已保存 “PyH-0.1.1.tar.gz” [2086/2086])

[root@dev opt]# tar -zxf PyH-0.1.1.tar.gz
[root@dev opt]# cd PyH-0.1.1
[root@dev PyH-0.1.1]# python setup.py install
9. 安装配置eclipse
eclipse安装太简单了,下载下来解压就ok了,主要是配置下pydev就ok了,pydev安装升级源:http://pydev.org/updates。
这样一个开发环境配置的就差不多了。
-The End-



没有更多推荐了,返回首页