TypeError: translation() got an unexpected keyword argument 'codeset'

9.3k Views Asked by At

I'm following a Python tutorial on youtube and need to create a django website, however I am unable to start, because when I enter "python manage.py runserver" I get the "TypeError: translation() got an unexpected keyword argument 'codeset'" message. I've run back the video like 20 times to see if I've missed anything, but no, because it's just the beginning of the django website tutorial. I've also tried typing python3 instead of python and some other options I saw on Stack Overflow, but none are really exactly relevant to the error message I'm getting. Perhaps someone knows how to fix this?

I tried to start a development server by typing in "python manage.py runserver" which was supposed to start a django webserver at 127.0.0.1:8000 or something, but instead I got the error message specified in the title

code:

PS C:\Users\kaspa\PycharmProjects\PyShop> python manage.py runserver
Exception ignored in thread started by: <function check_errors.<locals>.wrapper at 0x00000145784C1F80>
Traceback (most recent call last):
  File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\commands\runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\autoreload.py", line 248, in raise_last_exception
    raise _exception[1]
  File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\__init__.py", line 337, in execute
    autoreload.check_errors(django.setup)()
  File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\apps\registry.py", line 112, in populate
    app_config.import_models()
  File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\apps\config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1206, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\contrib\auth\models.py", line 94, in <module>
    class Group(models.Model):
  File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\base.py", line 139, in __new__
    new_class.add_to_class(obj_name, obj)
  File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\base.py", line 304, in add_to_class
    value.contribute_to_class(cls, name)
  File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\fields\related.py", line 1583, in contribute_to_class
    self.remote_field.through = create_many_to_many_intermediary_model(self, cls)
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\fields\related.py", line 1051, in create_many_to_many_intermediary_model
    'verbose_name': _('%(from)s-%(to)s relationship') % {'from': from_, 'to': to},
                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
  File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\functional.py", line 149, in __mod__
    return str(self) % rhs
           ^^^^^^^^^
  File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\functional.py", line 113, in __text_cast
    return func(*self.__args, **self.__kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\translation\__init__.py", line 75, in gettext
    return _trans.gettext(message)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\translation\trans_real.py", line 286, in gettext
    _default = _default or translation(settings.LANGUAGE_CODE)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\translation\trans_real.py", line 199, in translation
    _translations[language] = DjangoTranslation(language)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\translation\trans_real.py", line 90, in __init__
    self._init_translation_catalog()
  File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\translation\trans_real.py", line 131, in _init_translation_catalog
    translation = self._new_gnu_trans(localedir)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\kaspa\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\utils\translation\trans_real.py", line 119, in _new_gnu_trans
    return gettext_module.translation(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: translation() got an unexpected keyword argument 'codeset

'

8

There are 8 best solutions below

0
William Laflamme On

I've also been following Mosh's Python course, ran into the same problem, came here for answers and then did some more research on my own.

I'm a total beginner, I might me wrong, but in the tutorial, Mosh makes us install django 2.1 instead of the current version of django. The error "translation() got an unexpected keyword argument 'codeset'" basically means that python thinks there shouldn't be an argument called 'codeset' after the gettext_module.translation( that is in the code created with/(used in?) django 2.1.

Turns out, in the python gettext documentation, "Changed in version 3.11: codeset parameter is removed.". So django 2.1 has some code that isn't reconized by python 3.11, which pops an error.

Solution:

1- Deleted my python "PyShop" project.

2- Closed PyCharm.

3- Created a new "PyShop" project.

4- In the terminal, typed "pip uninstall django".

5- Then installed the current version of django by typing "pip install django" in the terminal (not pip install django==2.1 as shown in the tutorial).

6- Created the "pyshop" folder by typing "django-admin startproject pyshop ." in the terminal.

7- Typed "python manage.py runserver" in the terminal.

And voila! No more error, because the current version of django doesn't create stuff with code that the current version of python doesn't recognize in it.

New to python and stack overflow, sorry for some things I maybe wrote wrong or something :)

0
Dror On

The error refers to the file trans_real.py. According to error message:

TypeError: translation() got an unexpected keyword argument 'codeset'

'codeset' is not a valid keyword. So comment it (place # at the beginning of the line).

It worked for me.

0
Madara Dassanayake On

You can uninstall the django 2.1 using pip uninstall django==2.1 and reinstall the django that is compatible with your current interpreter (Python3) using pip install django.

0
user21651828 On

Uninstall django 2.1 by running from cmd

pip uninstall django

Then reinstall latest ver of django by running from cmd

pip install django

Then create project by running-

django-admin startproject project_name

Then run

python manage.py runserver
1
William Luka On

just unstall the django 2.1 version ,and then restall the newest version of the django,which should be worked.

1
Suresh Kumawat On

Please type below command windows CMD

Steps in image

0
Jonirul Islam On

just install django instead of django==2.1 because old version has some limitation. so just use

pip install django

now you can see there is no error.

0
Sivanesh Dsp On

I got this same error and came up with this solution... Python version of above 3.7 doesn't support older versions of django so you are getting this error

Upgrading django to the latest version might solve this issue.

however, we need to upgrage every single package used in this project to sync with django. What I did is create virtual env in conda and install python 3.6 version. Now I run the project successfully

Step 1:

Install anaconda and open anaconda prompt

Step 2: Check existing python version

python --version

Step 3: Create a new conda environment

conda create --name myenv

Replace myenv with the name you want for your environment.

Step 4: Activate the new environment

conda activate myenv

Step 5: Install the Desired Python Version

conda install python=3.6

Step 6: Verify the Python Version

python --version

Now set your project path on anaconda prompt run this command:

python manage.py runserver