我从堆栈溢出中得到了这段代码,但是当我试图执行命令时,我得到了一个错误。下面给出了代码和错误。如果有人能帮忙,我将不胜感激:
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'
正如Python 2 documentation for _winreg所说
检查你的Python版本。如果是python3,那么将模块引用重命名为
winreg
(不带下划线)。看Python 3 documentation。你知道吗相关问题 更多 >
编程相关推荐