ImportError: cannot import name force_text

2024/11/17 3:45:51

I have installed Python 2.7 and Django 1.4 in my CentOS machine and installed all dependencies for my existing project. When I run python manage.py runserver, I am getting the following traceback in my console

[root@localhost bv]# python manage.py runserver
Validating models...Unhandled exception in thread started by <bound method Command.inner_run of <django.contrib.staticfiles.management.commands.runserver.Command object at 0x8ddd7ec>>
Traceback (most recent call last):File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 91, in inner_runself.validate(display_num_errors=True)File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 266, in validatenum_errors = get_validation_errors(s, app)File "/usr/local/lib/python2.7/site-packages/django/core/management/validation.py", line 30, in get_validation_errorsfor (app_name, error) in get_app_errors().items():File "/usr/local/lib/python2.7/site-packages/django/db/models/loading.py", line 158, in get_app_errorsself._populate()File "/usr/local/lib/python2.7/site-packages/django/db/models/loading.py", line 67, in _populateself.load_app(app_name)File "/usr/local/lib/python2.7/site-packages/django/db/models/loading.py", line 88, in load_appmodels = import_module('.models', app_name)File "/usr/local/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module__import__(name)File "/usr/local/lib/python2.7/site-packages/debug_toolbar/models.py", line 6, in <module>from debug_toolbar.toolbar.loader import load_panel_classesFile "/usr/local/lib/python2.7/site-packages/debug_toolbar/toolbar/loader.py", line 12, in <module>from debug_toolbar.utils.settings import CONFIGFile "/usr/local/lib/python2.7/site-packages/debug_toolbar/utils/__init__.py", line 9, in <module>from django.utils.encoding import force_text
ImportError: cannot import name force_text
Answer

Upgrade to the latest Django 1.4 (as of 07-08-2014 - the "1.4.13").

I see that they have added a shortcut (force_text) in the django.utils.encoding module: https://github.com/django/django/blob/stable/1.4.x/django/utils/encoding.py

Note, that the shortcut wasn't there in the initial 1.4 version (https://github.com/django/django/blob/1.4/django/utils/encoding.py) and was only added in 1.4.2 (https://github.com/django/django/blob/1.4.2/django/utils/encoding.py).

As stated in the docs of django-debug-toolbar, current version (as of 07-08-2014 - the "1.2.1") works fine on Django 1.4 to 1.7.

Thus, make sure you use latest version of Django 1.4.x and django-debug-toolbar.

https://en.xdnf.cn/q/71264.html

Related Q&A

How can I select the pixels that fall within a contour in an image represented by a numpy array?

VI have a set of contour points drawn on an image which is stored as a 2D numpy array. The contours are represented by 2 numpy arrays of float values for x and y coordinates each. These coordinates are…

Get the value of specific JSON element in Python

Im new to Python and JSON, so Im sorry if I sound clueless. Im getting the following result from the Google Translate API and want to parse out the value of "translatedText":{"data"…

How does setuptools decide which files to keep for sdist/bdist?

Im working on a Python package that uses namespace_packages and find_packages() like so in setup.py:from setuptools import setup, find_packages setup(name="package",version="1.3.3.7"…

How to implement multivariate linear stochastic gradient descent algorithm in tensorflow?

I started with simple implementation of single variable linear gradient descent but dont know to extend it to multivariate stochastic gradient descent algorithm ?Single variable linear regression impo…

How to do os.execv() in Python in Windows without detaching from the console?

Im using Python 2.6 on Windows 7. I have Windows .cmd file which invokes Python to run the CherryPy Web server (version 3.1.2). I start this .cmd file by executing it at the prompt in a Windows CMD she…

Django queryset permissions

I am building a quite complex Django application to be used on top of and email scanning service. The Django application is written using Python 3.5+This application primarily uses Django Rest Framewor…

How to extract certain parts of a web page in Python

Target web page: http://www.immi.gov.au/skilled/general-skilled-migration/estimated-allocation-times.htmThe section I want to extract:<tr><td>Skilled &ndash; Independent (Residence) sub…

Accessing axis or figure in python ggplot

python ggplot is great, but still new, and I find the need to fallback on traditional matplotlib techniques to modify my plots. But Im not sure how to either pass an axis instance to ggplot, or get one…

Importing the same modules in different files

Supposing I have written a set of classes to be used in a python file and use them in a script (or python code in a different file). Now both the files require a set of modules to be imported. Should t…

Manually calculate AUC

How can I obtain the AUC value having fpr and tpr? Fpr and tpr are just 2 floats obtained from these formulas:my_fpr = fp / (fp + tn) my_tpr = tp / (tp + fn) my_roc_auc = auc(my_fpr, my_tpr)I know thi…