夫学须志也,才须学也,非学无以广才,非志无以成学。——诸葛亮

生活有度,自得慈铭 ——杜锦阳


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

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

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

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

Python AttributeError: 'Module' object has no attribute 'STARTF_USESHOWINDOW'的更多相关文章

  1. Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法

    最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attrib ...

  2. python 脚本运行时报错: AttributeError: 'module' object has no attribute ***

    最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attrib ...

  3. python文件名不要跟模块名相同,报错AttributeError: 'module' object has no attribute 'Differ'

    python中的文件都会生成pyc文件,包括模块也是这样,所以调用模块的时候,实际上会调用模块.pyc文件:在这个前提下,如果将文件名命名成跟模块名一样,在同一目录下就会生成一个跟模块名一样的pyc文 ...

  4. python问题:AttributeError: 'module' object has no attribute 'SSL_ST_INIT'

    AttributeError: 'module' object has no attribute 'SSL_ST_INIT' 问题背景: 下载工具wydomain,安装依赖包以后,出现下图问题. 几经 ...

  5. python问题:AttributeError: 'module' object has no attribute 'SSL_ST_INIT'(转)

    原文地址:http://www.cnblogs.com/zhaijiahui/p/7344778.html AttributeError: 'module' object has no attribu ...

  6. 【Python】【亲测好用】安装第三方包报错:AttributeError:'module' object has no attribute 'main'

    安装/卸载第三包可能出现如下问题及相应解决办法: 在pycharm编辑中,使用anconda2更新.卸载第三方包时,出现如下错误: AttributeError:'module' object has ...

  7. AttributeError: 'module' object has no attribute 'Thread'

    $ python thread.py starting at: 2015-08-05 00:24:24Traceback (most recent call last):  File "th ...

  8. 安装pandas报错(AttributeError: 'module' object has no attribute 'main')

    在pycharm中安装pandas出现报错:AttributeError: 'module' object has no attribute 'main', 刚开始以为是pip的版本太旧了,于是乎将其 ...

  9. AttributeError: 'module' object has no attribute 'enableTrace'

    Traceback (most recent call last): File "Long-lived-connection.py", line 29, in <module ...

随机推荐

  1. 这 3 个 Set 集合的实现有点简单,那来做个总结吧

    Set 接口是 Java Collections Framework 中的一员,它的特点是:不能包含重复的元素,允许且最多只有一个 null 元素.Java 中有三个常用的 Set 实现类: Hash ...

  2. C# 委托(delegate)、泛型委托和Lambda表达式

    目录 # 什么是委托 # 委托声明.实例化和调用 1.声明 2.委托的实例化 3.委托实例的调用 4.委托完整的简单示例 #泛型委托 1.Func委托 2.Action委托 3.Predicate委托 ...

  3. 10个常用的linux的命令

    以下就是今天我们要介绍的Linux命令:  man  touch, cat and less  sort and grep  cut  sed  tar  find  diff  uniq  chmo ...

  4. 有助于提高"锁"性能的几点建议

    有助于提高"锁"性能的几点建议 1.减少锁持有时间 public synchronized void syncMethod() { othercode1(); mutextMeth ...

  5. Jenkins Java 反序列化远程执行代码漏洞(CVE-2017-1000353)

    Jenkins Java 反序列化远程执行代码漏洞(CVE-2017-1000353) 一.漏洞描述 该漏洞存在于使用HTTP协议的双向通信通道的具体实现代码中,jenkins利用此通道来接收命令,恶 ...

  6. 荔枝FM前端面试题

    最近接到了荔枝FM的面试通知,遗憾的是没有拿到offer,但是这次面试呢,还是收获很大的,下面就来给大家说说我遇到的面试题 一面 一面是直接发了一套面试题到邮箱,开启了防作弊的,限时20分钟做完,下面 ...

  7. Linux服务部署Yapi项目(安装Node Mongdb Git Nginx等)

    Linux服务部署Yapi 一,介绍与需求 1,我的安装环境:CentOS7+Node10.13.0+MongoDB4.0.10. 2,首先安装wget,用于下载node等其他工具 yum insta ...

  8. [科研民工笔记1]安装Ubuntu到U盘

    主要是台式机只有一块固态,暂时没有加固态的打算,所以就买了一个64g的闪迪cz73,装了Ubuntu.以后可能考虑加一块硬盘. 前期准备:VMware.官网下载安装包(这里以16.04为例) 第一部分 ...

  9. C语言之左移和右移运算符

    C语言中的左移和右移运算符移位后的结果老是忘记,最近在刷有关位操作的题目,正好整理下:   1. 左移运算符(<<) 左移运算符是用来将一个数的各二进制位左移若干位,移动的位数由右操作数指 ...

  10. koa2+vue实现登陆以及是否登陆控制

    这里我们先说说登陆以及登陆状态控制需要的插件jsonwebtoken,jsonwebtoken就可以实现token的生成与反向解密出用户数据.安装步骤: npm install jsonwebtoke ...