Python AttributeError:no attribute 'STARTF_USESHOWINDOW'

夫?qū)W須志也,才須學(xué)也,非學(xué)無以廣才,非志無以成學(xué)。——諸葛亮
生活有度,自得慈銘 ——杜錦陽



今天新來的同事安裝環(huán)境遇到個莫名其妙的問題:

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

其它小伙伴好像都沒遇到過,后來發(fā)現(xiàn),應(yīng)該是系統(tǒng)的問題,因為還出現(xiàn)了字節(jié)混亂的錯誤:
UNICODEENCODEERROR:‘a(chǎn)scii’ code can't encode...

這個先不提,我們先來看看下面的錯誤: STARTF_USESHOWINDOW

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

因公司信息,所以打上馬賽克了。

百度了一會,發(fā)現(xiàn)網(wǎng)上解決方案都不靠譜。

出錯原因:使用了subprocess模塊,系統(tǒng)找不到這個模塊。

你可以做個測試:在python下輸出subprocess也會報這個錯。

后來想到有可能系統(tǒng)環(huán)境的問題和模塊代碼引起,起初是替換了Lib\site-packages\matplotlib\compat下的subprocess.py,后來想到這是子模塊,于是再替換了Lib\下的 subprocess.py ,再運行,一切正常。

國內(nèi)外論壇都沒找到相關(guān)的解釋,后來去翻了源碼才知道了原因,Cmd是WIN下命令符,pyc是編譯后運行的,和JAVA一樣,一次編譯多處運行,如果出現(xiàn)這個錯誤的小伙伴可以找這幾個地方替換下,或者直接拿可運行版本的丟進去覆蓋下。

我們來翻看一下:

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

看到這里,應(yīng)該不難發(fā)現(xiàn),CREATE_NEW_CONSOLE 是如何觸發(fā)的。

再來看下main方法的測試入口:

"""
  KARL-Dujinyang
  QQ:309933706
"""
if __name__ == "__main__":
    if mswindows:
        _demo_windows()
    else:
        _demo_posix()

mswindows 在我們文章開頭代碼中已經(jīng)提及了,測試的可以拿到此處代碼進行測試:

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 所引起的問題。如果出現(xiàn)這個錯誤的小伙伴可以找這幾個地方替換下,或者直接拿可運行版本的丟進去覆蓋下,也可以找我拿下源碼覆蓋,后面如果有時間我會上傳到一份到CSDN上。


|| 版權(quán)聲明:本文為博主杜錦陽原創(chuàng)文章,轉(zhuǎn)載請注明出處。


作者:奧特曼超人Dujinyang

來源:CSDN

原文:https://dujinyang.blog.csdn.net/

版權(quán)聲明:本文為博主杜錦陽原創(chuàng)文章,轉(zhuǎn)載請附上博文鏈接!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容