当前位置: 移动技术网 > IT编程>脚本编程>Python > Python AttributeError: 'Module' object has no attribute 'STARTF_USESHOWINDOW'

Python AttributeError: 'Module' object has no attribute 'STARTF_USESHOWINDOW'

2019年09月05日  | 移动技术网IT编程  | 我要评论

平顶山砍人事件,苑冉图片,斯内德植发

夫学须志也,才须学也,非学无以广才,非志无以成学。——诸葛亮
生活有度,自得慈铭 ——杜锦阳


今天新来的同事安装环境遇到个莫名其妙的问题:

attributeerror: 'module' object has no attribute 'startf_useshowindow' 

其它小伙伴好像都没遇到过,后来发现,应该是系统的问题,因为还出现了字节混乱的错误:
unicodeencodeerror:‘ascii’ code can't encode...

这个先不提,我们先来看看下面的错误: startf_useshowindow

深圳市米奇云科技有限公司

因公司信息,所以打上马赛克了。

百度了一会,发现网上解决方案都不靠谱。

出错原因:使用了subprocess模块,系统找不到这个模块。

你可以做个测试:在python下输出subprocess也会报这个错。

后来想到有可能系统环境的问题和模块代码引起,起初是替换了lib\site-packages\matplotlib\compat下的subprocess.py,后来想到这是子模块,于是再替换了lib\下的 subprocess.py ,再运行,一切正常。

国内外论坛都没找到相关的解释,后来去翻了源码才知道了原因,cmd是win下命令符,pyc是编译后运行的,和java一样,一次编译多处运行,如果出现这个错误的小伙伴可以找这几个地方替换下,或者直接拿可运行版本的丢进去覆盖下。

我们来翻看一下:

if mswindows:
    import threading
    import msvcrt
    import _subprocess
    class startupinfo:
        dwflags = 0
        hstdinput = none
        hstdoutput = none
        hstderror = none
        wshowwindow = 0
    class pywintypes:
        error = ioerror
else:
    import select
    _has_poll = hasattr(select, 'poll')
    import fcntl
    import pickle

    # when select or poll has indicated that the file is writable,
    # we can write up to _pipe_buf bytes without risk of blocking.
    # posix defines pipe_buf as >= 512.
    _pipe_buf = getattr(select, 'pipe_buf', 512)

此处是引入了 import _subprocess 模块,也就是说 subprocess.py -> _subprocess

然后定位到:

if mswindows:
        #
        # windows methods
        #

在这下面找到:

 def _execute_child(self, args, executable, preexec_fn, close_fds,
                           cwd, env, universal_newlines,
                           startupinfo, creationflags, shell, to_close,
                           p2cread, p2cwrite,
                           c2pread, c2pwrite,
                           errread, errwrite):
            """execute program (ms windows version)"""

            if not isinstance(args, types.stringtypes):
                args = list2cmdline(args)

            # process startup details
            if startupinfo is none:
                startupinfo = startupinfo()
            if none not in (p2cread, c2pwrite, errwrite):
                startupinfo.dwflags |= _subprocess.startf_usestdhandles
                startupinfo.hstdinput = p2cread
                startupinfo.hstdoutput = c2pwrite
                startupinfo.hstderror = errwrite

            if shell:
                startupinfo.dwflags |= _subprocess.startf_useshowwindow
                startupinfo.wshowwindow = _subprocess.sw_hide
                comspec = os.environ.get("comspec", "cmd.exe")
                args = '{} /c "{}"'.format (comspec, args)
                if (_subprocess.getversion() >= 0x80000000 or
                        os.path.basename(comspec).lower() == "command.com"):
                    # win9x, or using command.com on nt. we need to
                    # use the w9xpopen intermediate program. for more
                    # information, see kb q150956
                    # (http://web.archive.org/web/20011105084002/http://support.microsoft.com/support/kb/articles/q150/9/56.asp)
                    w9xpopen = self._find_w9xpopen()
                    args = '"%s" %s' % (w9xpopen, args)
                    # not passing create_new_console has been known to
                    # cause random failures on win9x.  specifically a
                    # dialog: "your program accessed mem currently in
                    # use at xxx" and a hopeful warning about the
                    # stability of your system.  cost is ctrl+c wont
                    # kill children.
                    creationflags |= _subprocess.create_new_console

看到这里,应该不难发现,create_new_console 是如何触发的。

再来看下main方法的测试入口:

"""
  karl-dujinyang
  qq:309933706
"""
if __name__ == "__main__":
    if mswindows:
        _demo_windows()
    else:
        _demo_posix()

mswindows 在我们文章开头代码中已经提及了,测试的可以拿到此处代码进行测试:

mswindows = (sys.platform == "win32")

_demo_windows 方法的定义:

def _demo_windows():
    #
    # example 1: connecting several subprocesses
    #
    print "looking for 'prompt' in set output..."
    p1 = popen("set", stdout=pipe, shell=true)
    p2 = popen('find "prompt"', stdin=p1.stdout, stdout=pipe)
    print repr(p2.communicate()[0])

    #dujinyang
    # example 2: simple execution of program
    #
    print "executing calc..."
    p = popen("calc")
    p.wait()


可以看出,这里是由 open->close 所引起的问题。如果出现这个错误的小伙伴可以找这几个地方替换下,或者直接拿可运行版本的丢进去覆盖下,也可以找我拿下源码覆盖,后面如果有时间我会上传到一份到csdn上。


|| 版权声明:本文为博主杜锦阳原创文章,转载请注明出处。

|| 版权声明:本文为博主杜锦阳原创文章,转载请注明出处。


作者:奥特曼超人dujinyang

来源:csdn

原文:

版权声明:本文为博主原创文章,转载请附上博文链接!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网