Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法
最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attribute 'xxx'"。这其实是.pyc文件存在问题。
问题定位:
查看import库的源文件,发现源文件存在且没有错误,同时存在源文件的.pyc文件
问题解决方法:
1. 命名py脚本时,不要与python预留字,模块名等相同
2. 删除该库的.pyc文件(因为py脚本每次运行时均会生成.pyc文件;在已经生成.pyc文件的情况下,若代码不更新,运行时依旧会走pyc,所以要删除.pyc文件),重新运行代码;或者找一个可以运行代码的环境,拷贝替换当前机器的.pyc文件即可
示例:
如下Python代码,执行时报错"AttributeError: 'module' object has no attribute 'urlopen'",更新Python27\Lib\urllib2.pyc文件后,即可正常运行。
import urllib2
url = "http://www.baidu.com"
f = urllib2.urlopen(url, timeout=5).read()
print len(f)
附录:
pyc文件介绍
pyc文件,是python编译后的字节码(bytecode)文件。只要你运行了py文件,python编译器就会自动生成一个对应的pyc字节码文件。这个pyc字节码文件,经过python解释器,会生成机器码运行(这也是为什么pyc文件可以跨平台部署,类似于java的跨平台,java中JVM运行的字节码文件)。下次调用直接调用pyc,而不调用py文件。直到你这个py文件有改变。python解释器会检查pyc文件中的生成时间,对比py文件的修改时间,如果py更新,那么就生成新的pyc。
在CentOS6.5上安装软件包时,提示“configure:error:no acceptable cc found in $path”。
原因:
这是缺少GCC编译器造成的,安装即可。
解决办法:
找到gcc的rpm包,安装即可。或者使用yum命令安装:
# yum install gcc -y
Linux下使用Python的Tkinter库出现的No module named _tkinter问题
在Linux下使用Tkinter库,出现如下问题
File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 39, in
import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter
Python的版本信息如下:
>>> import sys
>>> sys.version
'2.7 (r27:82500, Oct 11 2011, 13:51:21) \n[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)]'
经过艰苦绝伦的Google,查明问题出现原因,Tk/Tcl的组件安装不完全
问题解决:
下载Tk和Tcl,从www.scriptics.com上的链接下载tcl和tk的安装文件
现在最新的版本是8.4.4
http://prdownloads.sourceforge.net/tcl/tcl8.4.4-src.tar.gz
http://prdownloads.sourceforge.net/tcl/tk8.4.4-src.tar.gz
安装:
tar -zvxf tcl8.4.4.tar.gz
cd tcl8.4..4/
cd unix
./configure
make
make install
此时键入命令tclsh84,就可以使用tcl了
step3 安装tk与安装tcl类似
tar -zvxf tk8.4.4.tar.gz
cd tk8.4.4/
cd unix
./configure
make
make install
执行wish84就可以使用tk了。
注:安装tcl和tk的时候,make install 需要管理员的权限
安装完成了,重新make Python,然后,Tkinter库可以正常调用了
>>> from Tkinter import *
>>>
查看已经Python已经安装的模块
解决方案,就是在python环境中输入:
help(‘modules’) 就能列出所有已经安装的模块了。
No module named setuptools 解决方案
分类: Python/Ruby
ImportError: No module named setuptools 解决方案
shell中输入:
wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz
tar zxvf setuptools-0.6c11.tar.gz
cd setuptools-0.6c11
python setup.py build
python setup.py install
python 安装 setuptools Compression requires the (missing) zlib module 的解决方案
creating 'dist/setuptools-0.6c11-py2.7.egg' and adding 'build/bdist.linux-x86_64/egg' to it
Traceback (most recent call last):
File "setup.py", line 94, in <module>
scripts = scripts,
File "/usr/local/lib/python2.7/distutils/core.py", line 152, in setup
dist.run_commands()
File "/usr/local/lib/python2.7/distutils/dist.py", line 953, in run_commands
self.run_command(cmd)
File "/usr/local/lib/python2.7/distutils/dist.py", line 972, in run_command
cmd_obj.run()
File "/opt/oracle/sor/install/setuptools-0.6c11/setuptools/command/install.py", line 76, in run
self.do_egg_install()
File "/opt/oracle/sor/install/setuptools-0.6c11/setuptools/command/install.py", line 96, in do_egg_install
self.run_command('bdist_egg')
File "/usr/local/lib/python2.7/distutils/cmd.py", line 326, in run_command
self.distribution.run_command(command)
File "/usr/local/lib/python2.7/distutils/dist.py", line 972, in run_command
cmd_obj.run()
File "/opt/oracle/sor/install/setuptools-0.6c11/setuptools/command/bdist_egg.py", line 236, in run
dry_run=self.dry_run, mode=self.gen_header())
File "/opt/oracle/sor/install/setuptools-0.6c11/setuptools/command/bdist_egg.py", line 527, in make_zipfile
z = zipfile.ZipFile(zip_filename, mode, compression=compression)
File "/usr/local/lib/python2.7/zipfile.py", line 681, in __init__
"Compression requires the (missing) zlib module"
RuntimeError: Compression requires the (missing) zlib module
#################################################################################################
yum install zlib
yum install zlib-devel
安装完成后,重新编译 python2.7【不需要删除,只需要重新编译,make,安装就行了】
#################################################################################################
然后重新安装setuptools:
cd setuptools-0.6c11
Python setup.py install
安装MySQL-python-1.2.3c1出现“error: command 'gcc' failed with exit status 1”错误
具体报错信息如下:
_mysql.c: 在文件层:
_mysql.c:2330: 错误:‘_mysql_ConnectionObject’没有名为‘open’的成员
_mysql.c:2337: 错误:‘_mysql_ConnectionObject’没有名为‘converter’的成员
_mysql.c:2344: 错误:‘_mysql_ConnectionObject’没有名为‘connection’的成员
_mysql.c:2351: 错误:‘_mysql_ConnectionObject’没有名为‘connection’的成员
_mysql.c:2358: 错误:‘_mysql_ConnectionObject’没有名为‘connection’的成员
_mysql.c:2421: 错误:‘_mysql_ResultObject’没有名为‘converter’的成员
_mysql.c:2421: 错误:初始值设定元素不是常量
_mysql.c:2421: 错误:(在‘_mysql_ResultObject_memberlist[0].offset’的初始化附近)
_mysql.c: 在函数‘_mysql_ConnectionObject_getattr’中:
_mysql.c:2443: 错误:‘_mysql_ConnectionObject’没有名为‘open’的成员
error: command 'gcc' failed with exit status 1
环境为:
系统版本:ORALCE_RHEL6
Python版本:2.7.8
Mysql-python的site.cfg文件内容如下:
[root@oracle MySQL-python-1.2.3c1]# cat site.cfg
[options]
# embedded: link against the embedded server library
# threadsafe: use the threadsafe client
# static: link against a static library (probably required for embedded)
embedded = False
threadsafe = False
static = False
# The path to mysql_config.
# Only use this if mysql_config is not on your PATH, or you have some weird
# setup that requires it.
mysql_config =/usr/bin/mysql_config
# The Windows registry key for MySQL.
# This has to be set for Windows builds to work.
# Only change this if you have a different version.
registry_key = SOFTWARE\MySQL AB\MySQL Server 5.0
已经安装:
[root@oracle Packages]# rpm -ivh python-devel-2.6.6-51.el6.i686.rpm
Preparing... ########################################### [100%]
1:python-devel ########################################### [100%]
[root@oracle Packages]# rpm -ivh MySQL-python-1.2.3-0.3.c1.1.el6.i686.rpm
Preparing... ########################################### [100%]
1:MySQL-python ########################################### [100%]
还是存在error: command 'gcc' failed with exit status 1
现在应该如何解决呢?
——————
最后还是被我自己解决了,在windows下已经安装过MySQLdb了,但是在linux下安装不成功,很懊恼。
现在写下解决方法(还是走了很多弯路,好年轻啊!!!):
安装所有的依赖:
yum install python-devel mysql-devel zlib-devel openssl-devel
下载:MySQL-python-1.2.3.tar.gz
windows版本MySQL-python-1.2.3.win32-py2.7
解压,并安装即可。
怎么样使用yum来安装mysql
linux下使用yum安装mysql,以及启动、登录和远程访问。
1、安装
查看有没有安装过:
yum list installed mysql*
rpm -qa | grep mysql*
查看有没有安装包:
yum list mysql*
安装mysql客户端:
yum install mysql
安装mysql 服务器端:
yum install mysql-server
yum install mysql-devel
2、启动&&停止
数据库字符集设置
mysql配置文件/etc/my.cnf中加入default-character-set=utf8
启动mysql服务:
service mysqld start或者/etc/init.d/mysqld start
开机启动:
chkconfig -add mysqld,查看开机启动设置是否成功chkconfig --list | grep mysql*
mysqld 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
停止:
service mysqld stop
3、登录
创建root管理员:
mysqladmin -u root password 123456
登录:
mysql -u root -p输入密码即可。
忘记密码:
service mysqld stop
mysqld_safe --user=root --skip-grant-tables
mysql -u root
use mysql
update user set password=password("new_pass") where user="root";
flush privileges;
4、远程访问
开放防火墙的端口号
mysql增加权限:mysql库中的user表新增一条记录host为“%”,user为“root”。
5、Linux MySQL的几个重要目录
数据库目录
/var/lib/mysql/
配置文件
/usr/share /mysql(mysql.server命令及配置文件)
相关命令
/usr/bin(mysqladmin mysqldump等命令)
启动脚本
/etc/rc.d/init.d/(启动脚本文件mysql的目录)
mysql创建用户的方法分成三种:INSERT USER表的方法、CREATE USER的方法、GRANT的方法。
一、账号名称的构成方式
账号的组成方式:用户名+主机(所以可以出现重复的用户名,跟其他的数据库不一样)
用户名:16字符以内.
主机名:可以用主机名和IP地址,也可以用通配符
通配符说明:172.18.10.%(IP地址为172.18.10段的所有IP地址都可以访问)
二、通过CREATE USER命令进行创建用户
脚本:CREATE USER 'username'@'host' [IDENTIFIED BY 'PASSWORD'] 其中密码是可选项;
例子:CREATE USER 'john'@'192.168.189.71' IDENTIFIED BY "123";
CREATE USER 'john'@'192.168.189.%' IDENTIFIED BY "123";
CREATE USER 'john'@' %' ;
说明:该方法创建出来的用户只有连接数据库的权限,需要后续继续授权;
三、通过GRANT命令创建用户
个人习惯一般用这种方法进行创建用户,当数据库存在用户的时候GRANT会对用户进行授权,但当数据库不存在该用户的时候,就会创建相应的用户并进行授权。(说明上面那步是多余的)
脚本:
GRANT <ALL|priv1,priv2,.....privn> ON
[object] [IDENTIFIED BY 'password']
[WITH GRANT OPTION];
MAX_QUERIES_PER_HOUR count
MAX_UPDATES_PER_HOUR count
MAX_CONNECTIONS_PER_HOUR count
MAX_USER_CONNECTIONS count
说明:priv代表权限select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file等14个权限
例子:mysql>grant select,insert,update,delete,create,drop on test.hr to john@192.168.10.1 identified by '123';
说明:给主机为192.168.10.1的用户john分配可对数据库test的hr表进行select,insert,update,delete,create,drop等操作的权限,并设定口令为123。
mysql>grant all privileges on test.* to joe@192.168.10.1 identified by '123';
说明:给主机为192.168.10.1的用户john分配可对数据库test所有表进行所有操作的权限,并设定口令为123。
mysql>grant all privileges on *.* to john@192.168.10.1 identified by '123';
说明:给主机为192.168.10.1的用户john分配可对所有数据库的所有表进行所有操作的权限,并设定口令为123。
mysql>grant all privileges on *.* to john@localhost identified by '123';
说明:用户john分配可对所有数据库的所有表进行所有操作的权限,并设定口令为123。
四、直接向mysql.user表插入记录(该方法个人很少用)
因为数据库的用户信息都是保存在mysql.user这张表的,所以直接对该表进行插入语句,即可完成用户的创建;
mysql> insert into user (host,user,password) values ('%','john',password('123'));
五、完成用户的创建后,请记得刷新系统权限表;
mysql>flush privileges;
总结:虽然创建用户的方法有三种,个人还是倾向于第二种方法,一步到位,简单明了;
其他的两种方法只是有助于理解数据库的原理而已;
Linux升级Python提示Tkinter模块找不到解决
一、安装tkinter
在Linux中python默认是不安装Tkinter模块,
|
1
2
3
4
5
6
7
8
9
|
[root@li250-193 ~]# pythonPython 2.6.6 (r266:84292, Feb 22 2013, 00:00:18) [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import TkinterTraceback (most recent call last): File "<stdin>", line 1, in <module>ImportError: No module named Tkinter>>> |
我们安装Tkinter模块
|
1
2
3
4
5
6
7
8
|
[root@li250-193 ~]# yum -y install tkinter...[root@li250-193 ~]# pythonPython 2.6.6 (r266:84292, Feb 22 2013, 00:00:18) [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import Tkinter>>> |
二、升级Python
Linux的Python版本默认都不叫低
查看Python版本
|
1
2
|
[root@li250-193 ~]# python -VPython 2.6.6 |
DOWN新版本
|
1
|
[root@li250-193 ~]# wget http://www.python.org/ftp/python/2.7.4/Python-2.7.4.tgz |
解压安装
|
1
2
3
4
5
6
7
8
|
[root@li250-193 ~]# tar -xf Python-2.7.4.tgz [root@li250-193 ~]# cd Python-2.7.4[root@li250-193 Python-2.7.4]# ./configure...[root@li250-193 Python-2.7.4]# make...[root@li250-193 Python-2.7.4]# make install... |
看看新版本Python是否可以使用Tkinter?
|
1
2
3
4
5
6
7
8
9
10
11
|
[root@li250-193 Python-2.7.4]# ./pythonPython 2.7.4 (default, Apr 12 2013, 08:03:09) [GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import Tkinter Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/root/Python-2.7.4/Lib/lib-tk/Tkinter.py", line 39, in <module> import _tkinter # If this fails your Python may not be configured for TkImportError: No module named _tkinter>>> |
提示找不到tkinter模块?看看旧版的是不是正常
|
1
2
3
4
5
6
|
[root@li250-193 Python-2.7.4]# python Python 2.6.6 (r266:84292, Feb 22 2013, 00:00:18) [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import Tkinter>>> |
旧版的没问题,难道需要yum install tkinter一次?
|
1
2
3
4
5
6
7
8
9
|
[root@li250-193 Python-2.7.4]# yum install tkinterLoaded plugins: fastestmirror, securityLoading mirror speeds from cached hostfile * base: mirror.team-cymru.org * extras: mirror.team-cymru.org * updates: mirror.team-cymru.orgSetting up Install ProcessPackage tkinter-2.6.6-36.el6.x86_64 already installed and latest versionNothing to do |
提示已安装,看来不是tkinter的问题,看看tkinter模块在哪里?
|
1
2
|
[root@li250-193 Python-2.7.4]# find /usr -name *tkinter.so/usr/lib64/python2.6/lib-dynload/_tkinter.so |
找到一个,在2.6旧版本的目录下,估计是因为新版本库指向问题。于是认真读了README说明。重新配置安装
三、正确安装新版Python
首先修改Setup.dist文件
|
1
|
[root@li250-193 Python-2.7.4]# vim Modules/Setup.dist |
找到下面这几行,把前面的井号去掉打开它
_tkinter _tkinter.c tkappinit.c -DWITH_APPINIT \-L/usr/local/lib \-I/usr/local/include \-ltk8.5 -ltcl8.5 \-lX11
以上第四行
-ltk8.5 -ltcl8.5 默认是 8.2 ,请你系统实际tcl/tk版本修改
|
1
2
3
4
5
|
[root@li250-193 Python-2.7.4]# rpm -qa | grep ^tktk-8.5.7-5.el6.x86_64tkinter-2.6.6-36.el6.x86_64[root@li250-193 Python-2.7.4]# rpm -qa | grep ^tcltcl-8.5.7-6.el6.x86_64 |
我系统中装的是8.5,所以这里我改成了8.5
保存退出
安装tck-devel、tk-devel
|
1
|
[root@li250-193 Python-2.7.4]# yum -y install tcl-devel tk-devel |
开始配置安装
|
1
2
3
4
5
6
7
|
[root@li250-193 Python-2.7.4]# ldconfig[root@li250-193 Python-2.7.4]# ./configure...[root@li250-193 Python-2.7.4]# make...[root@li250-193 Python-2.7.4]# make install... |
看下新版Python是否可以使用tkinter模块
|
1
2
3
4
5
6
|
[root@li250-193 Python-2.7.4]# ./pythonPython 2.7.4 (default, Apr 12 2013, 08:49:11) [GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import Tkinter>>> |
已经没问题,旧版再看看
|
1
2
3
4
5
6
|
[root@li250-193 Python-2.7.4]# /usr/bin/python2.6Python 2.6.6 (r266:84292, Feb 22 2013, 00:00:18) [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import Tkinter>>> |
也没问题
如果直接敲入python -V查看版本是不是最新的,如果不是可以这么干:
which出python命令路径
|
1
2
|
[root@li250-193 Python-2.7.4]# which python/usr/local/bin/python |
cp 过去
|
1
|
[root@li250-193 Python-2.7.4]# cp python /usr/local/bin/python |
四、升级Python引起yum版本无法使用的问题解决
不少童鞋安装后就
cp python /usr/bin/python
导致yum时就提示
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
[root@lee ~]# yumThere was a problem importing one of the Python modulesrequired to run yum. The error leading to this problem was: No module named yumPlease install a package which provides this module, orverify that the module is installed correctly.It's possible that the above module doesn't match thecurrent version of Python, which is:2.7.4 (default, Apr 9 2013, 17:12:56) [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)]If you cannot solve this problem yourself, please go to the yum faq at: http://yum.baseurl.org/wiki/Faq [root@lee ~]# |
因为yum头部默认制定python脚本的路径就是
#! /usr/bin/python
你把旧版的python替换后就是用不了,不知道为何新版Python不能被yum识别,目前唯一最好解决的方法就是修改yum头部声明
改成
#! /usr/bin/python2.6
即可,这里的python2.6是我centos默认版本,大家的默认版本是多少请按实际情况修改即可
浙公网安备 33010602011771号