pywin32不能安装解决方案

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014680513/article/details/51005650

0x00 源

今天在开发者头条上看到了一篇用python做一个不要app的PPT遥控器,我也很想尝试去修改着玩玩,顺便拿来用一用,但是在安装pywin32的时候遇到了一点问题,所以发帖纪录一下问题以及解决方案。

0x01 问题

python for windows extensionspywin32下载地址
pywin32


双击打开exe文件就可以进入安装界面,然后点击下一步,它会自动定位你的python安装在什么地方,但是我的安装过程显示

Python version 2.7 required, which was not found in the registry.

并且还不支持手动添加目录,然后发现是python2.7没有添加到注册表的原因。

0x02 解答

感谢博客园的@ymin给了正确的解决方案。

新建一个register.py 文件,把以下代码贴进去。

# script to register Python 2.0 or later for use with win32all
# and other extensions that require Python registry settings
#
# written by Joakim Loew for Secret Labs AB / PythonWare
#
# source:
# http://www.pythonware.com/products/works/articles/regpy20.htm
#
# modified by Valentine Gogichashvili as described in http://www.mail-archive.com/[email protected]/msg10512.html

import sys

from _winreg import *

# tweak as necessary
version = sys.version[:3]
installpath = sys.prefix

regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)
installkey = "InstallPath"
pythonkey = "PythonPath"
pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (
    installpath, installpath, installpath
)

def RegisterPy():
    try:
        reg = OpenKey(HKEY_CURRENT_USER, regpath)
    except EnvironmentError as e:
        try:
            reg = CreateKey(HKEY_CURRENT_USER, regpath)
            SetValue(reg, installkey, REG_SZ, installpath)
            SetValue(reg, pythonkey, REG_SZ, pythonpath)
            CloseKey(reg)
        except:
            print "*** Unable to register!"
            return
        print "--- Python", version, "is now registered!"
        return
    if (QueryValue(reg, installkey) == installpath and
        QueryValue(reg, pythonkey) == pythonpath):
        CloseKey(reg)
        print "=== Python", version, "is already registered!"
        return
    CloseKey(reg)
    print "*** Unable to register!"
    print "*** You probably have another Python installation!"

if __name__ == "__main__":
    RegisterPy()

保存之后进入cmd,切换到存储该py文件的目录,执行python registed.py即可重新运行exe文件进行pywin32的安装。

另外这是我找到的pywin32的英文说明文档,希望对大家有点帮助。
pywin32帮助文档



0x03 续

后来在pycharm中 import win32api 时报错说找不到该模块,然后我又在cmd中 import win32api 后又报错

dll load faild:%1 不是有效的win32程序

然后我发现pywin32的安装程序的名称中的32和64位并不是针对电脑的,而是python,我的电脑是64位的而python是32位的,所以我在控制面板里删除了原先安装的64位pywin32,然后安装了32位的pywin32。
除此之外,在高级设置中添加环境变量python\Lib\site-packages之后,重启下pycharm就可以在pycharm中 import win32api 了。

猜你喜欢

转载自blog.csdn.net/u014680513/article/details/51005650