Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

argparse module not working in Python

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?

like image 211
Darakian Avatar asked Jul 07 '11 04:07

Darakian


People also ask

What is Argparse module in Python?

argparse is the “recommended command-line parsing module in the Python standard library.” It's what you use to get command line arguments into your program.

Is Argparse in standard library?

argv. The argparse module also automatically generates help and usage messages and issues errors when users give the program invalid arguments. As of Python >= 2.7 and >= 3.2, the argparse module is maintained within the Python standard library.

What is action =' Store_true in Python?

parser.add_argument('-o', '--output', action='store_true', help="shows output") An argument is added with add_argument . The action set to store_true will store the argument as True , if present. The help option gives argument help. args = parser.parse_args()


1 Answers

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).

like image 82
DSM Avatar answered Sep 23 '22 19:09

DSM