windows pip安装报错 ‘d‘ 不是内部或外部命令,也不是可运行的程序

    ERROR: Command errored out with exit status 1:
     command: 'd:\python\python36\python.exe' -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\aaa\\AppData\\Local\\Temp\\pip-install-mk9_v1hf\\prettyparse_436943cf7e254ab0952ec707a1eff11e\\setup.py'"'"'; __file__='"'"'C:\\Users\\aaa\\AppData\\Local\\Temp\\pip-install-mk9_v1hf\\prettyparse_436943cf7e254ab0952ec707a1eff11e\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base 'C:\Users\aaa\AppData\Local\Temp\pip-pip-egg-info-md6hdya9'
         cwd: C:\Users\aaa\AppData\Local\Temp\pip-install-mk9_v1hf\prettyparse_436943cf7e254ab0952ec707a1eff11e\
    Complete output (2 lines):
    'd' 不是内部或外部命令,也不是可运行的程序
    或批处理文件。


 
在windows pip安装时,例如  pip3 install prettyparse==0.1.4, 总是会报:'d' 不是内部或外部命令,也不是可运行的程序。 
非常纳闷。我的环境变量设置应该没问题,ping等其他cmd命令执行都没问题 就只有pip装包时 会报这个错。非常明显地(虽然这个明显在后来看也是错的),win系统在执行command: 'd:\python\python36\python.exe' -c 'import io ...' 时, 把这个长串 'd:\python\python36\python.exe' 拆成一个一个字母去执行了;如果我的python装在c盘,估计会报: 'c' 不是内部或外部命令,也不是可运行的程序。  

这个问题其他人遇见的机率很小很小。 记录一下,对自己是个小动力 对别人可能是个小参考。
刚开始时 有点不知如何下手,搜了半天只看到跟我一样的问题,但没有答案。 后来定位到了python安装目录/Lib/subprocess.py 这个文件, 有那么点很远的似曾相识感觉。加了一些打印日志 反复调试 但仍然没有找到问题所在。 
问题的转机来自某天的突然想起,好蠢啊 再找台OK的win机器对比打印日志 不就行了?   好吧..一番折腾后,找到了..想起了 大概一两个月前  不记得是为了解决一个什么问题,修改了subprocess.py 中Popen类的init参数 shell=True(默认为False), 还修改了list2cmdline函数最后的return语句为 ' '.join(result) (默认应该为: return ''.join(result))  这两个修改 应该是为了弄清当时一个什么问题 做的调试修改,修改后没有正面效果 也没有负面效果 就像透明的,所以忘了还原回去了..然后完全忘光了..
 
 代码中,在这一段后面加个打印:

             if not isinstance(args, str):
                args = list2cmdline(args)
                print("gggggggggggg args is:", args)

 则可以看到:
 pip3 install prettyparse==0.1.4
C:\Users\aaa>pip3 install prettyparse==0.1.4
Looking in indexes: http://pypi.tuna.tsinghua.edu.cn/simple
Collecting prettyparse==0.1.4
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/af/6c/bb169a14082e7c0edbe24f7684e8e1d20e556508b2c25086d3ac1a9a371b/prettyparse-0.1.4.tar.gz (3.9 kB)
gggggggggggg args is: d : \ p y t h o n \ p y t h o n 3 6 \ p y t h o n . e x e   - c   " i m p o r t   i o ,   o s ,   s y s ,   s e t u p t o o l s ,   t o k e n i z e ;   s y s . a r g v [ 0 ]   =   ' C : \ \ U s e r s \ \ a a a \ \ A p p D a t a \ \ L o c a l \ \ T e m p \ \ p i p - i n s t a l l - t 9 t 0 k x o e \ \ p r e t t y p a r s e _ 0 5 5 4 3 9 6 d 9 6 b 5 4 9 c 5 a 9 f 8 b 4 4 c 2 f a f 9 1 7 0 \ \ s e t u p . p y ' ;   _ _ f i l e _ _ = ' C : \ \ U s e r s \ \ a a a \ \ A p p D a t a \ \ L o c a l \ \ T e m p \ \ p i p - i n s t a l l - t 9 t 0 k x o e \ \ p r e t t y p a r s e _ 0 5 5 4 3 9 6 d 9 6 b 5 4 9 c 5 a 9 f 8 b 4 4 c 2 f a f 9 1 7 0 \ \ s e t u p . p y ' ; f   =   g e t a t t r ( t o k e n i z e ,   ' o p e n ' ,   o p e n ) ( _ _ f i l e _ _ )   i f   o s . p a t h . e x i s t s ( _ _ f i l e _ _ )   e l s e   i o . S t r i n g I O ( ' f r o m   s e t u p t o o l s   i m p o r t   s e t u p ;   s e t u p ( ) ' ) ; c o d e   =   f . r e a d ( ) . r e p l a c e ( ' \ r \ n ' ,   ' \ n ' ) ; f . c l o s e ( ) ; e x e c ( c o m p i l e ( c o d e ,   _ _ f i l e _ _ ,   ' e x e c ' ) ) "   e g g _ i n f o   - - e g g - b a s e   C : \ U s e r s \ a a a \ A p p D a t a \ L o c a l \ T e m p \ p i p - p i p - e g g - i n f o - s 2 l c e 0 1 w
    ERROR: Command errored out with exit status 1:
     command: 'd:\python\python36\python.exe' -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\aaa\\AppData\\Local\\Temp\\pip-install-t9t0kxoe\\prettyparse_0554396d96b549c5a9f8b44c2faf9170\\setup.py'"'"'; __file__='"'"'C:\\Users\\aaa\\AppData\\Local\\Temp\\pip-install-t9t0kxoe\\prettyparse_0554396d96b549c5a9f8b44c2faf9170\\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base 'C:\Users\aaa\AppData\Local\Temp\pip-pip-egg-info-s2lce01w'
         cwd: C:\Users\aaa\AppData\Local\Temp\pip-install-t9t0kxoe\prettyparse_0554396d96b549c5a9f8b44c2faf9170\
    Complete output (2 lines):
    'd' 不是内部或外部命令,也不是可运行的程序
    或批处理文件。
    ----------------------------------------
WARNING: Discarding https://pypi.tuna.tsinghua.edu.cn/packages/af/6c/bb169a14082e7c0edbe24f7684e8e1d20e556508b2c25086d3ac1a9a371b/prettyparse-0.1.4.tar.gz#sha256=05f6ef18df8ea8af3c9fd80020dda4eba05548825a4d41cd303b859dccdc5860 (from https://pypi.tuna.tsinghua.edu.cn/simple/prettyparse/). Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
ERROR: Could not find a version that satisfies the requirement prettyparse==0.1.4 (from versions: 0.1.0, 0.1.1, 0.1.2, 0.1.3, 0.1.4, 0.2.0, 1.0.0, 1.1.0, 1.2.0)
ERROR: No matching distribution found for prettyparse==0.1.4
WARNING: You are using pip version 21.2.2; however, version 21.2.4 is available.
You should consider upgrading via the 'd:\python\python36\python.exe -m pip install --upgrade pip' command.
 
  本来是要运行 d:\python\python36\python.exe  活生生被拆成 d : \ p y ... 当然会报错 'd' 不是内部或外部命令,也不是可运行的程序 了

如果只是这个return有问题,shell参数仍是False 仍然不是此种报错,所以看看这个参数:
Popen类的注释: shell: If true, the command will be executed through the shell.
搜到的区别: "shell=True参数会让subprocess.call接受字符串类型的变量作为命令,并调用shell去执行这个字符串,当shell=False时,subprocess.call只接受数组变量作为命令,并将数组的第一个元素作为命令,剩下的全部作为该命令的参数。"
代码中,if shell 会设置一些startupinfo的参数  ( STARTF_USESHOWWINDOW //使用wShowWindow 成员; STARTF_USESTDHANDLES //使用hStdInput 、hStdOutput 和hStdError 成员   这些具体如果将来用到再细研究吧)
 

            # Process startup details
            if startupinfo is None:
                startupinfo = STARTUPINFO()
            if -1 not in (p2cread, c2pwrite, errwrite):
                startupinfo.dwFlags |= _winapi.STARTF_USESTDHANDLES
                startupinfo.hStdInput = p2cread
                startupinfo.hStdOutput = c2pwrite
                startupinfo.hStdError = errwrite

            if shell:
                startupinfo.dwFlags |= _winapi.STARTF_USESHOWWINDOW
                startupinfo.wShowWindow = _winapi.SW_HIDE
                comspec = os.environ.get("COMSPEC", "cmd.exe")
                args = '{} /c "{}"'.format (comspec, args)

以及 处理第一个元素: 

            if shell:
                args = ["/bin/sh", "-c"] + args
                if executable:
                    args[0] = executable

shell为False时 只接受数组变量作为命令, 数组的第一个元素为命令 为True时,接受字符串类型的变量作为命令,所以才会出现这个问题。 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值