Python AttributeError: 'Module' object has no attribute 'STARTF_USESHOWINDOW'
夫學須志也,才須學也,非學無以廣才,非志無以成學。——諸葛亮 生活有度,自得慈銘 ——杜錦陽
<hr/> 今天新來的同事安裝環境遇到個莫名其妙的問題:
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()
<br/>
可以看出,這裡是由 OPEN->CLOSE
所引起的問題。如果出現這個錯誤的小夥伴可以找這幾個地方替換下,或者直接拿可執行版本的丟進去覆蓋下,也可以找我拿下原始碼覆蓋,後面如果有時間我會上傳到一份到CSDN上。
<br/>
|| 版權宣告:本文為博主杜錦陽原創文章,轉載請註明出處。
作者:奧特曼超人Dujinyang
來源:CSDN
原文:https://dujinyang.blog.csdn.net/
版權宣告:本文為博主原創文章,轉載請