我已经尽可能使用 argparse 编写了一个 python 程序。只需三行。
import argparse
if __name__ == "__main__":
print "Hello World"
出现以下错误:
File "/Users/pfarrell/sandbox/dbms/extractor/src/extractor.py", line 2, in import argparse ImportError: No module named argparse
这是我编写的第一个Python程序,所以我很容易相信我没有正确设置一些Python库路径等。
它在 OS-X 上运行
python --version
Python 2.7.1
pfarrell@way:~/sandbox/dbms/extractor/src$ python -c "import argparse; print argparse"
<module 'argparse' from '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.pyc'>
更多测试(感谢评论)
在 shell 中显式运行它可以工作,但在 netbeans 中运行它会失败:
仅外壳
python extractor.py -h
Hello World
usage: extractor.py [-h]
optional arguments:
-h, --help show this help message and exit
将其作为脚本运行会显示完全不同的路径。看起来 netbeans 设置需要一两次调整
./extractor.py -h
/Users/pfarrell/sandbox/dbms/extractor/src
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib- scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC
/Library/Python/2.7/site-packages
Hello World
在netbeans内部执行
/Users/pfarrell/sandbox/dbms/extractor/src
/Users/pfarrell/Library/Application Support/NetBeans/7.2.1/jython-2.5.1/Lib
/Users/pfarrell/Library/Application Support/NetBeans/7.2.1/jython-2.5.1/Lib/site-packages
/Users/pfarrell/sandbox/dbms/extractor/src
__classpath__
__pyclasspath__/
Hello World
最佳答案
谢谢大家。正如 @sberry 所建议的,这条路径存在多个问题。 我发现netbeans默认为jpython,这是一个较旧的2.5版本。它还可以使用标准的python 2.7.1,当我使用它时,一切都是幸福和快乐
非常感谢。
关于python - import argparse 失败,某些设置/路径问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13898468/