Windows 7下Python 2.7与NLTK环境配置及常见问题解决

Ruby
Ruby
发布时间:2026-10-13 | 7人浏览过
原创

本文详细记录了在Windows 7 64位系统中安装Python 2.7及自然语言工具包NLTK的完整流程。重点解决了安装过程中常见的“Python version 2.7 required, which was not found in the registry”错误,提供了通过register.py脚本修复注册表的解决方案,并逐步指导完成Setuptools、Pip、PyYAML及NLTK的安装与验证。

本文分享了在Windows环境下配置Python 2.7及NLTK(Natural Language Toolkit)的详细步骤与注意事项。

测试环境为Windows 7 Ultimate 64-bits系统。

Python 2.7的安装过程。

首先访问Python官网下载Python 2.7版本的安装包。

执行下载后的安装程序进行安装。

接下来进行NLTK库的安装配置。

第一步,从官方渠道下载NLTK安装包并完成初步安装。

在安装过程中,可能会遇到报错信息:“Python version 2.7 required, which was not found in the registry”,提示系统未找到Python 2.7的注册信息。

针对上述注册表缺失错误的解决方法如下。

(1) 新建一个名为register.py的Python脚本文件,将后续提供的代码完整粘贴至该文件中,并将其保存至D盘根目录。

# 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/distutils-sig@python.org/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()

(2) 打开“开始”菜单,选择“运行”,输入cmd打开命令提示符。在命令行中输入D:\register.py并按回车执行。若屏幕显示“Python 2.7 is already registered!”,则表明注册表配置已成功修复。

3、继续安装setuptools工具包。 4、安装Pip:在“开始”菜单打开“运行”,输入命令D:\Program Files\Python\Scripts\easy_install pip以完成Pip的安装。 5、安装PyYAML和NLTK:在“开始”菜单打开“运行”,输入命令D:\Program Files\Python\Scripts\pip install pyyaml nltk以安装这两个依赖库。 6、测试安装结果:通过“开始”->“所有程序”->“Python 2.7”启动IDLE (Python GUI)。在交互界面中输入import nltk并执行,若无报错信息输出,则说明安装成功。

以上即为Python 2.7与NLTK环境配置的完整内容,希望能为读者的学习提供参考,欢迎继续支持本站获取更多技术资讯。

相关标签:

本站声明

本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。

Python 相关产品和应用

全部下载

专题列表