Python中的argparse模块无法使用
我正在尝试在Python中使用argparse模块。我的问题是,在新安装的环境中,我遇到了以下情况:
File "test.py", line 3, in <module>
import argparse
File "/home/jon/Pythons/realmine/argparse.py", line 3, in <module>
parser = argparse.ArgumentParser(description='Short sample app')
AttributeError: 'module' object has no attribute 'ArgumentParser'
test.py
的内容是:
import argparse
显然,我缺少了一些东西。有人能帮我吗?
1 个回答
77
通常,这种情况是因为你自己写的模块覆盖了一个内置的模块。根据错误信息:
File "/home/jon/Pythons/realmine/argparse.py", line 3, in <module>
看起来你有一个自己写的模块叫做 argparse.py,这个模块导致了问题,因为 test.py 正在尝试导入它,而这个模块里面没有 ArgumentParser。你可以把你的 argparse.py 改个名字(同时删除任何 .py[c/o] 文件)。