Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django-admin TypeError: __init__() got an unexpected keyword argument 'allow_abbrev'

I've upgraded Django to 2.1.4 (from 2.0.5) and I get the following error when I run the command line manage.py python3 manage.py createsuperuser

Here is the detailed error:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 314, in execute
    parser = CommandParser(usage='%(prog)s subcommand [options] [args]', add_help=False, allow_abbrev=False)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 48, in __init__
    super().__init__(**kwargs)
TypeError: __init__() got an unexpected keyword argument 'allow_abbrev'

I'm on Debian stretch using Python 3.5.3 and Django 2.1.4

like image 840
John Mathew Avatar asked Apr 10 '19 18:04

John Mathew


3 Answers

TL;DR

Uninstall argparse using pip, then proceed as normal. Pip installs an out-of-date version of argparse intended only for python < 3.2. Uninstalling it allows the newer version of argparse bundled as part of the standard python library to be used instead (which is what you want).

Argparse can often be installed via pip when installing an older package that lists argparse as a dependency (you don't want this on modern versions of python).


I was having this issue as well with python 3.7.4 and it turned out that argparse was installed on my system via pip which installs a completely outdated version of argparse. I believe this occurred when I installed a package that listed argparse as one of its dependencies.

Upon further investigation, the pip version installs from the old github repo where argparse development used to occur before it was absorbed into the standard python library.

A warning from the repo itself:

Important Note about the PyPi argparse package


argparse development happens in the python standard library nowadays, NOT HERE.

The PyPi argparse package is mostly for people who want to have argparse on older Pythons, like < 2.7 or < 3.2 because it was not in stdlib back then.

Thus, do not file issues, feature requests or pull requests for the PyPi version of argparse if they also apply to standard library argparse.

If we look at the definition of the ArgumentParser class in the github/PyPi repo we can see it does not include a definition for allow_abbrev.

Furthermore, we can see that the listed version is 1.4.0 here, along with a note that "we use our own version number independant of the one in stdlib and we release this on pypi".


On my own (problematic) install I can see that argparse is being called as if it was installed externally (i.e. via pip)

$ python -c 'import argparse; print(argparse.__file__)'
/<stuff>/python3.7/lib/python3.7/site-packages/argparse-1.4.0-py3.7.egg/argparse.py

And we can see the version matches what is distributed via PyPi

$ python -c 'import argparse; print(argparse.__version__)'
1.4.0

After uninstalling argparse via pip uninstall argparse I now get:

$ python -c 'import argparse; print(argparse.__file__)'
/<stuff>/Python/3.7.4-foss-2018b/lib/python3.7/argparse.py

$ python -c 'import argparse; print(argparse.__version__)'
1.1

#Note: I have python installed under /<stuff>/Python/3.7.4-foss-2018b/bin/python

This indicates I am now calling the appropriate ('internal') version of argparse shipped with python 3.7, which we can see from the official python source repo includes support for allow_abbrev. We can also see from the official repo that version 1.1 is the appropriate version number for the argparse that ships with python 3.7.

After performing the uninstall I no longer get the unexpected keyword argument 'allow_abbrev' error, and any packages that rely on argparse now function as expected.

In Summary

Pip installs an out of date version of argparse that causes this error. Pip installing argparse can often happen behind the scenes if you install an older package that has argparse listed as a dependency. To solve the issue simply uninstall argparse via pip and python will fall back on the newer, correct version of argparse that comes included as part of the standard library.

like image 186
Cole Avatar answered Oct 10 '22 07:10

Cole


We found that it helped to uninstall argparse from the system, using pip uninstall argparse. We subsequently installed argparse inside our virtual environment and still no issues. I admit to not understanding exactly why this helped, as the version of argparse (1.4.0) remained the same. We are using Python 3.7.3.

like image 35
Andrew Basile Avatar answered Oct 10 '22 07:10

Andrew Basile


I've had this error when updating an Elastic Beanstalk app with Django 2.1 (Python 3.6). The Django version and the dependencies were exactly the same, so no idea why this error appeared. We found out that the issue was that importing argparse took it from /opt/python/run/venv/local/lib/python3.6/site-packages/argparse.py as if it had been externally installed.

We just removed that file and everything went back to normal. If someone figures out why this happens, I'm interested.

like image 38
Eric Darchis Avatar answered Oct 10 '22 09:10

Eric Darchis