python学习进阶(一)

由于工作需要,用到不少python脚本,但是总没有时间好好仔细而系统的学习和研究一下。


后续,我将利用work间隙,在一年内把python系统的学习一下。


今天结合一个我工作中用到的python脚本,中断基础学习,针对实例,边工作边继续学习。


脚本用到的python命令如下:
python contents_xml.py find --contents-xml=contents.xml --name=boot.img --tags=download_file
需要返回boot.img的路径


该命令执行的效果是:从指定的xml文件中列出定义的路径来,如下


E:\project\python>python contents_xml.py find --contents-xml=contents.xml --name=boot.img --tags=download_file
../../../out/target/product/kuntao//boot.img


该命令在我的linux工作站工作正常,我将这个命令放到window环境出错(已经安装了python,解析xml文件出错)。
由于该命令需要解析xml文件,需要安装python的lxml库(lxml takes all the pain out of XML)
安装lxml的流程:

C:\Python27\Scripts>easy_install virtualenv
Searching for virtualenv
Reading https://pypi.python.org/simple/virtualenv/
Best match: virtualenv 15.0.2
Downloading https://pypi.python.org/packages/5c/79/5dae7494b9f5ed061cff9a8ab8d6e1f02db352f3facf907d9eb614fb80e9/virtualenv-15.0.2.tar.gz#md5=0e
94daf1292827ffdbba80a63
Processing virtualenv-15.0.2.tar.gz
Writing c:\users\lenovo\appdata\local\temp\easy_install-m7pipn\virtualenv-15.0.2\setup.cfg
Running virtualenv-15.0.2\setup.py -q bdist_egg --dist-dir c:\users\lenovo\appdata\local\temp\easy_install-m7pipn\virtualenv-15.0.2\egg-dist-tm
s
warning: no previously-included files matching '*' found under directory 'docs\_templates'
warning: no previously-included files matching '*' found under directory 'docs\_build'
Adding virtualenv 15.0.2 to easy-install.pth file
Installing virtualenv-script.py script to C:\Python27\Scripts
Installing virtualenv.exe script to C:\Python27\Scripts
Installing virtualenv.exe.manifest script to C:\Python27\Scripts


Installed c:\python27\lib\site-packages\virtualenv-15.0.2-py2.7.egg
Processing dependencies for virtualenv
Finished processing dependencies for virtualenv


C:\Python27\Scripts>easy_install.exe lxml-2.3-py2.7-win32.egg
Processing lxml-2.3-py2.7-win32.egg
creating c:\python27\lib\site-packages\lxml-2.3-py2.7-win32.egg
Extracting lxml-2.3-py2.7-win32.egg to c:\python27\lib\site-packages
Adding lxml 2.3 to easy-install.pth file


Installed c:\python27\lib\site-packages\lxml-2.3-py2.7-win32.egg
Processing dependencies for lxml==2.3
Finished processing dependencies for lxml==2.3


C:\Python27\Scripts>easy_install.exe lxml
Searching for lxml
Best match: lxml 2.3
Processing lxml-2.3-py2.7-win32.egg
Removing lxml 2.3 from easy-install.pth file
Adding lxml 2.3 to easy-install.pth file


Using c:\python27\scripts\lxml-2.3-py2.7-win32.egg
Processing dependencies for lxml
Finished processing dependencies for lxml


安装完后,我继续执行脚本,发现我的脚本执行还是出错,错误如下:

E:\>cd E:\project\python


E:\project\python>python contents_xml.py find --contents-xml=contents.xml --name=emmc_appsboot.mbn --tags=download_file
Traceback (most recent call last):
  File "contents_xml.py", line 4, in <module>
    from lxml.etree import ElementTree
  File "c:\python27\scripts\lxml-2.3-py2.7-win32.egg\lxml\etree.py", line 7, in <module>
  File "c:\python27\scripts\lxml-2.3-py2.7-win32.egg\lxml\etree.py", line 4, in __bootstrap__
  File "C:\Python27\lib\site-packages\pkg_resources.py", line 950, in resource_filename
    self, resource_name
  File "C:\Python27\lib\site-packages\pkg_resources.py", line 1645, in get_resource_filename
    self._extract_resource(manager, self._eager_to_zip(name))
  File "C:\Python27\lib\site-packages\pkg_resources.py", line 1681, in _extract_resource
    outf, tmpnam = _mkstemp(".$extract", dir=os.path.dirname(real_path))
  File "C:\Python27\lib\site-packages\pkg_resources.py", line 2865, in _mkstemp
    return tempfile.mkstemp(*args,**kw)
  File "C:\Python27\lib\tempfile.py", line 308, in mkstemp
    return _mkstemp_inner(dir, prefix, suffix, flags)
  File "C:\Python27\lib\tempfile.py", line 240, in _mkstemp_inner
    _set_cloexec(fd)
  File "C:\Python27\lib\tempfile.py", line 50, in _set_cloexec
    flags = _fcntl.fcntl(fd, _fcntl.F_GETFD, 0)
AttributeError: 'module' object has no attribute 'F_GETFD'


根据错误信息,我百度了很久找解决方案,最终发现问题的root cause如下:

我安装的python27的lib里的fcntl.py跟系统的有冲突(可能我过去调试修改过fcntl.py),导致问题。我把lib目录下的fcntl.py修改成fcntl_ex.py,然后运行命令ok


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值