使用pyinstaller打包为可执行程序后, iptables模块执行时提示 AttributeError: 'NoneType' object has no attribute 'throw_exception'
2017-02-07 18:14
1141 查看
问题主要出现在使用python的python-iptables的包时, pip install python-iptables后, 直接python test-iptables.py是正常的
10005
<type 'unicode'>
10001
<type 'unicode'>
10010
<type 'unicode'>
10008
<type 'unicode'>
10003
<type 'unicode'>
10007
<type 'unicode'>
10012
<type 'unicode'>
10011
<type 'unicode'>
10006
<type 'unicode'>
10002
<type 'unicode'>
10009
<type 'unicode'>
10014
<type 'unicode'>
10004
<type 'unicode'>
10013
<type 'unicode'>
但是当使用pyinstaller -F test-iptables.py 打包成可执行程序, 执行打包好的程序提示
Traceback (most recent call last):
File "test-iptables.py", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyimod03_importers.py", line 389, in load_module
exec(bytecode, module.__dict__)
File "iptc/__init__.py", line 10, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyimod03_importers.py", line 389, in load_module
exec(bytecode, module.__dict__)
File "iptc/ip4tc.py", line 13, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyimod03_importers.py", line 389, in load_module
exec(bytecode, module.__dict__)
File "iptc/xtables.py", line 709, in <module>
AttributeError: 'NoneType' object has no attribute 'throw_exception'
Failed to execute script test-iptables
通过堆栈可以看到, File "iptc/xtables.py", line 709, in <module> 没有throw_exception这个属性
通过find / -name xtables.py找到 pip 安装的包路径: /usr/local/lib/python2.7/dist-packages/iptc/xtables.py
查看该文件709行
通过代码得知, _lib_xtwrapper是通过 find_library("xtwrapper")得到的
查看 find_library方法的代码,最终定位到同目录下的 util.py文件中的_do_find_library()方法:
将p的值打印出来,看有什么区别, 在直接python test-iptables.py的情况下 输出:
最终还将拼接上/libxtwrapper.so得到最终路径, 并加载该so
该so的路径在/usr/local/lib/python2.7/dist-packages/libxtwrapper.so, 所以最终可以找到该文件, 没有问题
但是当打包成可执行程序再执行时, 输出如下:
可以看到, sys.path的路径中只有最终释放出的/tmp/目录, 所以自然不会找到libxtwrapper.so从而返回None值, 自然报错
我的解决方法是将libxtwrapper.so当做资源一起打包进可执行程序, 修改一下util.py文件中的_do_find_library()方法源码, 让其可以找到释放出来的libxtwrapper.so文件
目录结构:
root@xbox-virtual-machine:/home/xbox/Desktop/pack# tree
.
├── pack.spec
├── resources
│ ├── x64
│ │ └── libxtwrapper.so
│ └── x86
│ └── libxtwrapper.so
└── test-iptables.py
关于打包可以参考这篇文章: http://www.cnblogs.com/dacainiao/p/5918845.html
打包完成后再次运行没有报错, 功能正常:
import iptc table = iptc.Table(iptc.Table.NAT) for chain in table.chains: for rule in chain.rules: for match in rule.matches: print match.parameters['dport'] print type(match.parameters['dport'])
10005
<type 'unicode'>
10001
<type 'unicode'>
10010
<type 'unicode'>
10008
<type 'unicode'>
10003
<type 'unicode'>
10007
<type 'unicode'>
10012
<type 'unicode'>
10011
<type 'unicode'>
10006
<type 'unicode'>
10002
<type 'unicode'>
10009
<type 'unicode'>
10014
<type 'unicode'>
10004
<type 'unicode'>
10013
<type 'unicode'>
但是当使用pyinstaller -F test-iptables.py 打包成可执行程序, 执行打包好的程序提示
Traceback (most recent call last):
File "test-iptables.py", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyimod03_importers.py", line 389, in load_module
exec(bytecode, module.__dict__)
File "iptc/__init__.py", line 10, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyimod03_importers.py", line 389, in load_module
exec(bytecode, module.__dict__)
File "iptc/ip4tc.py", line 13, in <module>
File "/usr/local/lib/python2.7/dist-packages/PyInstaller/loader/pyimod03_importers.py", line 389, in load_module
exec(bytecode, module.__dict__)
File "iptc/xtables.py", line 709, in <module>
AttributeError: 'NoneType' object has no attribute 'throw_exception'
Failed to execute script test-iptables
通过堆栈可以看到, File "iptc/xtables.py", line 709, in <module> 没有throw_exception这个属性
通过find / -name xtables.py找到 pip 安装的包路径: /usr/local/lib/python2.7/dist-packages/iptc/xtables.py
查看该文件709行
通过代码得知, _lib_xtwrapper是通过 find_library("xtwrapper")得到的
查看 find_library方法的代码,最终定位到同目录下的 util.py文件中的_do_find_library()方法:
将p的值打印出来,看有什么区别, 在直接python test-iptables.py的情况下 输出:
最终还将拼接上/libxtwrapper.so得到最终路径, 并加载该so
该so的路径在/usr/local/lib/python2.7/dist-packages/libxtwrapper.so, 所以最终可以找到该文件, 没有问题
但是当打包成可执行程序再执行时, 输出如下:
可以看到, sys.path的路径中只有最终释放出的/tmp/目录, 所以自然不会找到libxtwrapper.so从而返回None值, 自然报错
我的解决方法是将libxtwrapper.so当做资源一起打包进可执行程序, 修改一下util.py文件中的_do_find_library()方法源码, 让其可以找到释放出来的libxtwrapper.so文件
目录结构:
root@xbox-virtual-machine:/home/xbox/Desktop/pack# tree
.
├── pack.spec
├── resources
│ ├── x64
│ │ └── libxtwrapper.so
│ └── x86
│ └── libxtwrapper.so
└── test-iptables.py
关于打包可以参考这篇文章: http://www.cnblogs.com/dacainiao/p/5918845.html
打包完成后再次运行没有报错, 功能正常:
相关文章推荐
- 使用cx_freeze工具打包时提示 AttributeError 'module' object has no attribute '_fix_up_module':
- PIL中分离通道发生“AttributeError: 'NoneType' object has no attribute 'bands'”
- Python使用suds调用webservice报错解决方法:AttributeError: 'Document' object has no attribute 'set'
- Python python __def__ Exception AttributeError: "'NoneType' object has no attribute
- PIL中分离通道发生“AttributeError: 'NoneType' object has no attribute 'bands'”
- AttributeError: 'NoneType' object has no attribute 'append'
- AttributeError: 'NoneType' object has no attribute 'bytes' python3.4 win64
- python httplib2 - AttributeError: 'NoneType' object has no attribute 'makefile'
- 转 Exception exceptions.AttributeError: "'NoneType' object has no attribute 'population'" 错误解决
- 执行Python程序时,报AttributeError: 'function' object has no attribute 'name'
- yum--AttributeError: 'module' object has no attribute 'HTTPSHandler'
- python机器学习实战3.2提示错误AttributeError: type object 'str' has no attribute '_name_'
- httplib:AttributeError: 'module' object has no attribute 'HTTPConnection'
- pb2.text_format.Merge(f.read(), self.solver_param) AttributeError: 'module' object has no attribute 'text_format'
- 使用PyOpenGL,调用glutInit时TypeError: 'NoneType' object is not callable
- scrapy:使用response.follow()方法时出现AttributeError: 'HtmlResponse' object has no attribute 'follow'
- ERROR 程序出错,错误原因:'bytes' object has no attribute 'read'
- AttributeError: 'module' object has no attribute 'xxx'
- python 中出现的AttributeError: 'module' object has no attribute '_handlerList'
- flask_route错误:AttributeError: 'function' object has no attribute 'route'