无法在 Windows 上为 python 2.5 导入 argparse 1.2.1

Posted

技术标签:

【中文标题】无法在 Windows 上为 python 2.5 导入 argparse 1.2.1【英文标题】:Unable to import argparse 1.2.1 for python 2.5 on windows 【发布时间】:2012-08-23 13:33:42 【问题描述】:

所以,我认为我已经正确安装了 setuptools 和 argparse 1.2.1。

C:\Python25_64bit>python ez_setup.py argparse
Searching for argparse
Best match: argparse 1.2.1
Processing argparse-1.2.1-py2.5.egg
argparse 1.2.1 is already the active version in easy-install.pth

Using c:\python25_64bit\lib\site-packages\argparse-1.2.1-py2.5.egg
Processing dependencies for argparse
Finished processing dependencies for argparse

C:\Python25_64bit>python
Python 2.5.4 (r254:67916, Dec 23 2008, 15:19:34) [MSC v.1400 64 bit (AMD64)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import argparse
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named argparse
>>> quit()

但是,如您在上面看到的,我似乎无法导入它。我还尝试将 egg 直接添加到我的 Eclipse PyDev 项目中,但这也无济于事。如您所见,我的环境中没有设置任何内容。或许,这就是问题所在?

C:\Python25_64bit>echo %PYTHONPATH%
%PYTHONPATH%

C:\Python25_64bit>echo %PYTHONHOME%
%PYTHONHOME%

【问题讨论】:

我一直认为 Lib/site-packages 是导入搜索路径的一部分。这是不正确的吗? C:\Python25_64bit\Lib\site-packages&gt;dir ... Directory of C:\Python25_64bit\Lib\site-packages 08/28/2012 05:41 PM &lt;DIR&gt; . 08/28/2012 05:41 PM &lt;DIR&gt; .. 08/27/2012 10:55 AM 21,499 argparse-1.2.1-py2.5.egg 【参考方案1】:

希望您在安装时注意并注意argparse 的安装位置。 在您的脚本中(从命令行或从您的 IDE 运行)包括在顶部:

import sys
for p in sys.path:
    print repr(p)

查看python正在搜索的所有路径。使用以下方法将路径添加到 argparse:

sys.path.insert(0, '/your/path/to/argparse')

或将安装移动到路径中的某个位置。

环境变量可以为空,sys.path 的元素中也有编译。

【讨论】:

以上是关于无法在 Windows 上为 python 2.5 导入 argparse 1.2.1的主要内容,如果未能解决你的问题,请参考以下文章