30

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
  • 5
    How can test.py contain a single line when the error references line 3? Commented Jul 7, 2011 at 4:53

1 Answer 1

77

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

Sign up to request clarification or add additional context in comments.

9 Comments

interestingly, I made exactly the same bozo move and this SO thread solved my problem in seconds.
Haha, who do you tell that? Nice to see I’m not the only moron around. :D
Its interesting how many people are doing the same sandbox naming
add me to this list of morons ;)
he he i did the same and needed to come stackoverflow now the feeling is the same. lol
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.