Python脚本问题集

1、indentationError:expected an indented block

缩进模块不一致的问题

2、AttributeError: ‘module’ object has no attribute’xxx’

这其实是.pyc文件存在问题:

问题定位:

查看import库的源文件,发现源文件存在且没有错误,同时存在源文件的.pyc文件

问题解决方法:

1). 命名py脚本时,不要与python预留字,模块名等相同

2). 删除该库的.pyc文件(因为py脚本每次运行时均会生成.pyc文件;在已经生成.pyc文件的情况下,若代码不更新,运行时依旧会走pyc,所以要删除.pyc文件),重新运行代码;或者找一个可以运行代码的环境,拷贝替换当前机器的.pyc文件即可)

3、为什么一些参数需要事先定义,而一些不用呢?不,所有的都需要有等于号(=)进行赋值,只不过有些在赋值前会有其他操作,但是等号是不能够省的。

为什么不能用连等号呢?否则不能判断?如 if a == b ==c,此时无法检验a == c 的情况。

4、IndexError:list index out of range:列表的长度不匹配,无法进行比较

5、typeerror:'list'object is not callable:表示list无法调用,一般是编写错误

6、indexerror:list assignment index out of range:list[index]index超出范围;list是一个空的,没有一个元素,进行list[0]就会出现该错误

7、indentationerror:unindent does not match any outer indentation level:缩排不统一

8、typeerror :xxx take exactly 2 arguments (3 given):方法接受了两个参数, 你传了三个

9、字典items()与iteritems()都是函数,调用标准格式和其它函数格式是一样的:变量.方法()

10、Python 字典(Dictionary)dirt.keys() 函数以列表返回一个字典所有的键

11:AttributeError: AttributeError: 'xxx' object has no attribute 'xxx' ——提示该属性不存在,着重了解其配置是否存在问题。

12:pymongo.errors.duplicatekeyerror e11000 duplicate key error collection

原因:_id不能重复,清数据库内容解决。

13、TypeError: sequence item 4: expected string or Unicode, NoneType found

原因:自动化脚本中msg返回null类型数据,导致了这个报错,分类处理后解决。

14、/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning.

SNIMissingWarning

/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.

InsecurePlatformWarning

/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.

InsecurePlatformWarning。。。

原因:因为SSL的问题,urllib3需要pyopenssl;这个问题在老的Python中经常遇到,在BAE中估计比较难控制,貌似只提供了requirements.txt安装的方式。

pip从https源安装模块时使用urllib3这个库,在python2.7.9之前的ssl模块比较老,会导致一些SSL连接的安全问题【1】。直接解决这个问题有两个方法:

  1. 升级Python到2.7.9之上
  2. 安装urllib3的安全依赖【2】

>> pip install pyopenssl ndg-httpsclient pyasn1

发布了7 篇原创文章 · 获赞 0 · 访问量 133

猜你喜欢

转载自blog.csdn.net/fasddf/article/details/103422099