argparse module not working in Python

眉间皱痕 提交于 2019-12-18 03:50:16

问题


I'm trying to get the argparse module working in Python. My problem is that on a fresh install, I get the following:

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 is:

import argparse

Clearly, I'm missing something. Can anyone help?


回答1:


Usually this symptom is the result of shadowing a builtin module with one of your own. And from the error message:

File "/home/jon/Pythons/realmine/argparse.py", line 3, in <module>

it looks like you have your own module argparse.py, which is causing the problem, because it's the one which test.py is trying to import, and which lacks ArgumentParser. Rename your argparse.py to something else (and remove any .py[c/o] files).



来源:https://stackoverflow.com/questions/6605851/argparse-module-not-working-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!