This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: AttributeError: 'module' object has no attribute 'ArgumentParser'
Type: behavior Stage: resolved
Components: Extension Modules Versions: Python 2.7
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: Arfrever, David Friedman, bsduni, eric.araujo, ezio.melotti, r.david.murray
Priority: normal Keywords:

Created on 2014-03-03 09:05 by bsduni, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (6)
msg212620 - (view) Author: bsduni (bsduni) Date: 2014-03-03 09:05
Hi!

I have installed Python 2.7.6 fully (all the modules selected). I attempted run some Python codes that were developed by a past staff, and am stuck with an issue with argparse. 

For ease of explanation I am using the example given in this website at http://docs.python.org/2/howto/argparse.html.

When I create a simple program prog.py with codes below:

import argparse
parser = argparse.ArgumentParser()
parser.parse_args()

and execute with $ python prog.py, the following is the error message I receive:
Traceback (most recent call last):
  File "prog.py", line 2, in <module>
    parser = argparse.ArgumentParser()
AttributeError: 'module' object has no attribute 'ArgumentParser'

I also tried to add argparse additionally using pip install and the problem remains the same.

Would be grateful if someone could help to overcome this AttributeError.
msg212622 - (view) Author: Arfrever Frehtes Taifersar Arahesis (Arfrever) * (Python triager) Date: 2014-03-03 09:25
I guess that there is argparse module in current directory, which overrides argparse module from standard library.
Use e.g. print(argparse) or print(argparse.__file__) to find it.
A correct location would be something like /usr/lib64/python2.7/argparse.pyc.
msg212686 - (view) Author: bsduni (bsduni) Date: 2014-03-03 23:03
Unfortunately it is not that case, ie, no argparse in my current working directory /home/bsduni/pytests/ (on a FreeBSD 9.2 system).

The following are the locations I can find argparse in my system:

/usr/local/lib/python2.7/argparse.py
/usr/local/lib/python2.7/argparse.pyo
/usr/local/lib/python2.7/argparse.pyc

/usr/ports/lang/python27/work/Python-2.7.6/Doc/howto/argparse.rst
/usr/ports/lang/python27/work/Python-2.7.6/Doc/library/argparse.rst
/usr/ports/lang/python27/work/Python-2.7.6/Lib/argparse.py

/usr/ports/lang/python27/work/stage/usr/local/lib/python2.7/argparse.py
/usr/ports/lang/python27/work/stage/usr/local/lib/python2.7/argparse.pyc
/usr/ports/lang/python27/work/stage/usr/local/lib/python2.7/argparse.pyo

/usr/local/lib/python2.7/site-packages/IPython/external/argparse

/usr/local/lib/python2.7/site-packages/argparse.py
/usr/local/lib/python2.7/site-packages/argparse.pyc
/usr/local/lib/python2.7/site-packages/argparse-1.1-py2.7.egg-info
/usr/local/lib/python2.7/site-packages/argparse
/usr/local/lib/python2.7/site-packages/argparse_actions
/usr/local/lib/python2.7/site-packages/argparse_actions-0.4.4-py2.7.egg-info
/usr/local/lib/python2.7/site-packages/argparse_config
/usr/local/lib/python2.7/site-packages/argparse_config-0.5.1-py2.7.egg-info
/usr/local/lib/python2.7/site-packages/argparse.extra-0.0.2-py2.7.egg-info
/usr/local/lib/python2.7/site-packages/argparse.extra-0.0.2-py2.7-nspkg.pth


The the files in /usr/local/lib/python2.7/site-packages/ are from the 'pip install' which I tried when argparse didnot work after the installation of Python in full (ie, with argparse files in /usr/local/lib/python2.7/)
msg212688 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-03-03 23:20
Please show the output from

   print(argparse.__file__)
msg218162 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2014-05-09 12:51
Closing for lack of feedback.
Feel free to re-open if/when more info are available.
msg373913 - (view) Author: David Friedman (David Friedman) Date: 2020-07-18 19:22
I know this is 6 years too late, but I had this problem a few minutes ago on Python2.7.  Googling didn't find me anything relevant except this bug entry.  However, I found the cause myself: I had a test file named argparse.py (and an argparse.pyc) in the current directory (i.e. $PYTHONPATH) which was overriding the one in the main python library path. Once I renamed my argparse.py and argparse.pyc to something else, python found the real argparse and that message (the same one you saw) disappeared.
History
Date User Action Args
2022-04-11 14:57:59adminsetgithub: 65039
2020-07-18 19:22:58David Friedmansetnosy: + David Friedman
messages: + msg373913
2014-05-09 12:51:21ezio.melottisetstatus: pending -> closed
nosy: + ezio.melotti
messages: + msg218162

2014-04-16 18:20:19r.david.murraysetstatus: open -> pending
resolution: works for me
stage: resolved
2014-03-07 21:47:06eric.araujosetnosy: + eric.araujo
2014-03-03 23:20:09r.david.murraysetnosy: + r.david.murray
messages: + msg212688
2014-03-03 23:03:53bsdunisetmessages: + msg212686
2014-03-03 09:25:44Arfreversetnosy: + Arfrever
messages: + msg212622
2014-03-03 09:05:48bsdunicreate