python系统注册表

2024-04-26 23:35:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我从堆栈溢出中得到了这段代码,但是当我试图执行命令时,我得到了一个错误。下面给出了代码和错误。如果有人能帮忙,我将不胜感激:

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()

我得到以下错误:

from __winreg import *
Traceback (most recent call last):

  File "<ipython-input-35-f6f8c1a0ffdd>", line 1, in <module>
    from __winreg import *

ModuleNotFoundError: No module named '__winreg'

Tags: fromimportreturnversion错误sysregprint
1条回答
网友
1楼 · 发布于 2024-04-26 23:35:32

正如Python 2 documentation for _winreg所说

The _winreg module has been renamed to winreg in Python 3. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.

检查你的Python版本。如果是python3,那么将模块引用重命名为winreg(不带下划线)。看Python 3 documentation。你知道吗

相关问题 更多 >