.py转换.exe遇到的问题

版权声明:欢迎朋友们的观摩,标注原创的文章,如应用,请标明出处,谢谢。 https://blog.csdn.net/qq_37012770/article/details/81163039

Python小白一个

想把写好的python项目给朋友试试,要.py转换成.exe才行,遂按照教程,种种摸爬滚打后,集众家之所长,成功转换,这里就遇到的问题做一个总结。(学习大佬们的方法很受用,如有侵权联系小弟以删之。)

1、最开始的方法用的是下载py2exe,但在cmd 里执行的时候,一直提示 “初始化失败”,遂放弃。

后来知道 为什么不使用py2exe?

因为 py2exe打包完后只能在相同系统下运行;打包同时需要打包dll文件等各种文件

pyInstall打包完以后只有一个exe运行文件

2、所以用pyInstaller的方法

在pip 中直接安装就好了

3、还要安装pywin32,安装这个时遇到了问题

pywin32下载地址
[https://sourceforge.net/projects/pywin32/files/pywin32/Build%20221/]
这里写图片描述

按理说下载的.exe文件直接安装,其默认下一步,他会自己找到python的本地目录,但出现了一个问题

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

解决

桌面创建文件: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中进入桌面目录,执行python registed.py
即可重新运行pywin32的安装
(补充,pywin32必须安装成对应的版本,python是32位的,电脑是64位的,安装的pywin32必须是32位才行)

猜你喜欢

转载自blog.csdn.net/qq_37012770/article/details/81163039