感谢支持
我们一直在努力

CentOS中使用Python遇到的几个问题

用Python搞了一个从excel中读取cobbler节点信息并加入cobbler中的脚本,运行的过程中出了不少问题,这里记录下来,方便日后查找!

相关阅读:

在 CentOS 6.4上安装Python 2.7.5 http://www.linuxidc.com/Linux/2013-06/85968.htm

《Python核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm

一、CentOS中安装python,yum install python,我通过这个命令安装python,默认安装的是2.6的版本,但是当我把python 2.6安装完成后,发现cobbler的WEB管理页面无法打开了,查看apahce的错误日志:

[Sun Jul 07 20:13:01 2013] [error] [client 192.168.21.94] ImportError: cannot import name urandom
[Sun Jul 07 20:13:02 2013] [error] [client 192.168.21.94] File does not exist: /var/www/html/favicon.ico
[Sun Jul 07 20:31:09 2013] [error] [client 192.168.21.94] mod_wsgi (pid=10816): Target WSGI script ‘/usr/share/cobbler/web/cobbler.w
sgi’ cannot be loaded as Python module.
[Sun Jul 07 20:31:09 2013] [error] [client 192.168.21.94] mod_wsgi (pid=10816): Exception occurred processing WSGI script ‘/usr/shar
e/cobbler/web/cobbler.wsgi’.
[Sun Jul 07 20:31:09 2013] [error] [client 192.168.21.94] Traceback (most recent call last):
[Sun Jul 07 20:31:09 2013] [error] [client 192.168.21.94]  File “/usr/share/cobbler/web/cobbler.wsgi”, line 9, in <module>
[Sun Jul 07 20:31:09 2013] [error] [client 192.168.21.94]    import django.core.handlers.wsgi
[Sun Jul 07 20:31:09 2013] [error] [client 192.168.21.94]  File “/usr/lib/python2.6/site-packages/django/core/handlers/wsgi.py”, li
ne 10, in <module>
[Sun Jul 07 20:31:09 2013] [error] [client 192.168.21.94]    from django import http
[Sun Jul 07 20:31:09 2013] [error] [client 192.168.21.94]  File “/usr/lib/python2.6/site-packages/django/http/__init__.py”, line 12
3, in <module>
[Sun Jul 07 20:31:09 2013] [error] [client 192.168.21.94]    from django.utils.http import cookie_date
[Sun Jul 07 20:31:09 2013] [error] [client 192.168.21.94]  File “/usr/lib/python2.6/site-packages/django/utils/http.py”, line 7, in
 <module>
[Sun Jul 07 20:31:09 2013] [error] [client 192.168.21.94]    from email.Utils import formatdate
[Sun Jul 07 20:31:09 2013] [error] [client 192.168.21.94]  File “/usr/lib64/python2.6/email/__init__.py”, line 79, in __getattr__
[Sun Jul 07 20:31:09 2013] [error] [client 192.168.21.94]    __import__(self.__name__)
[Sun Jul 07 20:31:09 2013] [error] [client 192.168.21.94]  File “/usr/lib64/python2.6/email/utils.py”, line 27, in <module>
[Sun Jul 07 20:31:09 2013] [error] [client 192.168.21.94]    import random
[Sun Jul 07 20:31:09 2013] [error] [client 192.168.21.94]  File “/usr/lib64/python2.6/random.py”, line 47, in <module>
[Sun Jul 07 20:31:09 2013] [error] [client 192.168.21.94]    from os import urandom as _urandom

缺少urandom模块,python2.6版本里默认没有这个模块,随即将python升级到2.7版本。

升级过程:

1、下载Python 2.7源码:

wget http://www.python.org/ftp/python/2.7.2/Python-2.7.2.tgz

下载

2、解压源码包:

tar -zxvf Python-2.7.2.tgz

3、编译:

cd Python-2.7.2

./configure  –prefix=/usr

4、make&&make install

安装完成后测试web界面可以正常打开了,但是使用yum安装软件的时候会发现不能使用,原因是yum与python有依赖关系,yum是急于python写的,运行yum命令时会调用pyton;

vi /usr/bin/yum

将#!/usr/bin/pyton 改成:#!/usr/bin/pyton2.6,改成你系统默认的版本即可,不然他会调用高版本的python软件包,但是是找不到的,需要做相关配置才行,这里为了方便就直接改了就可以使用了。

二、在安装了Python2.7后执行cobbler的相关命令也会有问题

[root@cobbler scripts]# ./addnode2.py cobbler.xls
add test1 aa:bb:cc:dd 19:2.:16:8.:0.:2
add test2 aa:bb:cc:dd 19:2.:16:8.:0.:3
[root@cobbler scripts]# Traceback (most recent call last):
  File “/usr/bin/cobbler”, line 17, in <module>
    import cobbler.cli as app
ImportError: No module named cobbler.cli
Traceback (most recent call last):
  File “/usr/bin/cobbler”, line 17, in <module>
    import cobbler.cli as app
ImportError: No module named cobbler.cli

[root@cobbler scripts]# cobbler get-loaders
Traceback (most recent call last):
  File “/usr/bin/cobbler”, line 17, in <module>
    import cobbler.cli as app
ImportError: No module named cobbler.cli

这也是由于python版本导致的,同样:vi /usr/bin/cobbler

将调用的python版本修改为2.6即可。

三、补充:关于安装EasyInstall 遇到的问题:

下载:

wget http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg

安装:

sh  setuptools-0.6c11-py2.7.egg

报错:

Traceback (most recent call last): 
File “<string>”, line 1, in <module>
zipimport.ZipImportError: can’t decompress data; zlib not available

解决方法:

1、安装依赖zlib、zlib-devel

2、重新编译安装Python

./configure
编辑Modules/Setup文件
找到下面这句,去掉注释
#zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
重新编译安装:make & make install

更多CentOS相关信息见CentOS 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=14

赞(0) 打赏
转载请注明出处:服务器评测 » CentOS中使用Python遇到的几个问题
分享到: 更多 (0)

听说打赏我的人,都进福布斯排行榜啦!

支付宝扫一扫打赏

微信扫一扫打赏