Python Django "ImportError: No module named hashcompat" - python

I'm learning python + Django by reading 《beginning django e-commerce》, after I have installed django-db-log, when running $python manage.py runserver, there is a problem.
Unhandled exception in thread started by <function wrapper at 0x02C28DB0>
Traceback (most recent call last):
File "D:\Python27\lib\site-packages\django\utils\autoreload.py", line 93, in wrapper fn(*args, **kwargs)
File "D:\Python27\lib\site-packages\django\core\management\commands\runserver.py", line 92, in inner_run self.validate(display_num_errors=True)
File "D:\Python27\lib\site-packages\django\core\management\base.py", line 308, in validate num_errors = get_validation_errors(s, app)
File "D:\Python27\lib\site-packages\django\core\management\validation.py", line 34, in get_validation_errors for (app_name, error) in get_app_errors().items():
File "D:\Python27\lib\site-packages\django\db\models\loading.py", line 166, in get_app_errors self._populate() File "D:\Python27\lib\site-packages\django\db\models\loading.py", line 75, in _populate self.load_app(app_name)
File "D:\Python27\lib\site-packages\django\db\models\loading.py", line 96, in load_app models = import_module('.models', app_name)
File "D:\Python27\lib\site-packages\django\utils\importlib.py", line 35, in import_module __import__(name)
File "build\bdist.win32\egg\djangodblog\models.py", line 9, in <module>
File "build\bdist.win32\egg\djangodblog\manager.py", line 23, in <module>
File "build\bdist.win32\egg\djangodblog\helpers.py", line 5, in <module>
ImportError: No module named hashcompat

You are referring to an outdated manual. Module hashcompat was removed in Django 1.6, as you can read here.
Either install an older Django version (1.5 or previous) or get a more recent textbook. You can also manually fix your code by using Python builtins as advised in the removal notice, but for a beginner can be sort of a hassle :)

I solved it using hashlib. I just added:
try:
from hashlib import sha1 as sha_constructor
except ImportError:
from django.utils.hashcompat import sha_constructor

hashcompat is already deprecated in django 1.6, it is better to use hashlib instead

If you are still following that, the django version used in the book is 1.1 and if you are using higher version of django, then try removing the djangodblog from the app list in the setting.py file. It should work.

Related

ImportError: cannot import name caches

I am using Django 1.6. I want to start working site. When you use python manage.py shell get an error:
(test) D:\test>python manage.py shell
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_from_command_line(sys.argv)
File "D:\testvenv\lib\site-packages\django\core\management\__init__.py", line 399, in execute_from_command_line
utility.execute()
File "D:\testvenv\lib\site-packages\django\core\management\__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\testvenv\lib\site-packages\django\core\management\base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "D:\testvenv\lib\site-packages\django\core\management\base.py", line 280, in execute
translation.activate('en-us')
File "D:\testvenv\lib\site-packages\django\utils\translation\__init__.py", line 130, in activate
return _trans.activate(language)
File "D:\testvenv\lib\site-packages\django\utils\translation\trans_real.py", line 188, in activate
_active.value = translation(language)
File "D:\testvenv\lib\site-packages\django\utils\translation\trans_real.py", line 177, in translation
default_translation = _fetch(settings.LANGUAGE_CODE)
File "D:\testvenv\lib\site-packages\django\utils\translation\trans_real.py", line 159, in _fetch
app = import_module(appname)
File "D:\testvenv\lib\site-packages\django\utils\importlib.py", line 40, in import_module
__import__(name)
File "D:\testvenv\lib\site-packages\constance\backends\database\__init__.py", line 1, in <module>
from django.core.cache import caches
ImportError: cannot import name caches
Tell me what I have a problem. And if it can depend on your database (not sure that Django is connected to the DB - I do not know how to check)?
Added:
Code manag.py:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
By the looks of it you're using django-constance.
According to their change log, they haven't supported 1.6 since version 1.1
So either use an older version of that, or better yet - use a supported version of django.
According to the release notes, django.core.cache.caches was introduced in Django 1.7, so this application has never worked with Django 1.6.
If you have a running instance of that application, check which version of Django it actually uses.

Deploying mysql database on a django app to production server [duplicate]

This question already has answers here:
No module named MySQLdb
(33 answers)
Closed 6 years ago.
I am trying to deploy a django app on a shared hosting. and i am currently facing issues deploying the app especially the mysql database. I have created the mysql database on the production server. But when trying to run manage.py collectstatic command i am getting the below attached error. I am assuming it has something to do with the mysqldb library.
I don't know how to go about this. Deploying this mysql database
Which ip address should i put on the host part of the db config in the settings.py file. Is it my production server ip address?
Please bear with me i am new to django especially the deployment part. Can't find enough info on the django deployment documentations.
This is the error i am getting on my production server:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/core/management/__init__.py", line 327, in execute
django.setup()
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/opt/python27/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/contrib/auth/models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
class AbstractBaseUser(models.Model):
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/db/models/base.py", line 108, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/db/models/base.py", line 307, in add_to_class
value.contribute_to_class(cls, name)
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/db/models/options.py", line 263, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/db/__init__.py", line 36, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/db/utils.py", line 212, in __getitem__
backend = load_backend(db['ENGINE'])
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/db/utils.py", line 116, in load_backend
return import_module('%s.base' % backend_name)
File "/opt/python27/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 28, in <module>
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
This is a duplicate question. You have your answer in there >> No module named MySQLdb
As you see in your logs: Error loading MySQLdb module: No module named MySQLdb

django-scarface not working in python 2.7

My OS is OSX El Capitan.
Versions:
Python 2.7.10
Django 1.9.2
Django-Scarface 3.0
After I install scarface and add it to the installed apps, it gives this error and i can't run my application.
Unhandled exception in thread started by <function wrapper at 0x1043dbe60>
Traceback (most recent call last):
File "/Users/mcagataybarin/molocate-dev/env/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/mcagataybarin/molocate-dev/env/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "/Users/mcagataybarin/molocate-dev/env/lib/python2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/Users/mcagataybarin/molocate-dev/env/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/mcagataybarin/molocate-dev/env/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/mcagataybarin/molocate-dev/env/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/Users/mcagataybarin/molocate-dev/env/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/mcagataybarin/molocate-dev/env/lib/python2.7/site-packages/scarface/models.py", line 7, in <module>
from scarface.platform_strategy import get_strategies
File "/Users/mcagataybarin/molocate-dev/env/lib/python2.7/site-packages/scarface/platform_strategy.py", line 38
class PlatformStrategy(metaclass=ABCMeta):
^
SyntaxError: invalid syntax
After I've search for this error, I think that reason is my python version.
Still I'm not sure, therefore I'm asking to you.
Is there any way to fix this? If the reason is really my Python version, how can i update my python version without damaging my code and everything?
The problem is related to metaclass syntax change in Python 3.x (https://docs.python.org/3/whatsnew/3.0.html?highlight=metaclass#changed-syntax), django-scarface is 3.x oriented. I solved the problem (with Python 2.7.x) changing in platform_strategy.py (Line 38) to this:
class PlatformStrategy():
__metaclass__ = ABCMeta
I submitted a pull request in github project: https://github.com/dreipol/django-scarface/pull/11
In the packaging instructions Python 2.7 is supported https://github.com/dreipol/django-scarface/blob/master/setup.py#L30.
You could run the package tests https://github.com/dreipol/django-scarface/blob/master/scarface/tests.py inside a virtualenv with Python 2.7.10 to see if they pass. If they don't then there's nothing wrong with your code. If they do pass then the problem might not be in the package itself.

error while linking mysql and django

i am using aptana for creating a website using django. I have installed mysql but not able to link the database when i run this command *Python2.7 manage.py runserver* i get a message that Python2.7 not recognized and when i run this command Python manage.py runserver
i get a error message
C:\Users\George\Documents\Aptana Studio 3 Workspace\Firstwebsite>python manage.py runserver
Validating models...
Unhandled exception in thread started by <bound method Command.inner_run of <dja
ngo.contrib.staticfiles.management.commands.runserver.Command object at 0x028E5C
70>>
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\core\management\commands\runserver.py", line
92, in inner_runself.validate(display_num_errors=True)
`File "C:\Python27\lib\site-packages\django\core\management\base.py", line 280, in
validate num_errors = get_validation_errors(s, app)`
File "C:\Python27\lib\site-packages\django\core\management\validation.py", line 28, in
get_validation_errors from django.db import models, connection
File "C:\Python27\lib\site-packages\django\db\__init__.py", line 40, in <module>
backend = load_backend(connection.settings_dict['ENGINE'])
File "C:\Python27\lib\site-packages\django\db\__init__.py", line 34, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "C:\Python27\lib\site-packages\django\db\utils.py", line 93, in __getitem__
backend = load_backend(db['ENGINE'])
File "C:\Python27\lib\site-packages\django\db\utils.py", line 27, in load_backend
return import_module('.base', backend_name)
File "C:\Python27\lib\site-packages\django\utils\importlib.py", line 35, in
import_module__import__(name)
File "C:\Python27\lib\site-packages\django\db\backends\mysql\base.py", line 17, in
<module>
raise ImproperlyConfigured("Error loading MySQLdb module: %s" %
e)django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module
named MySQLdb
I am using Django 1.5 and python 2.7 and aptana 3
I am confused whether i am tying this commands in the correct directory.
any help !!!
Did you install MySQLdb? which is a system library. A quick search on google shows a couple sites you can download this from
One of which is (link pulled from here), another post that addresses this:
http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python
http://sourceforge.net/projects/mysql-python/files/mysql-python/
We would need to see your settings.py file to do more in depth review.
On the surface, it seems like you have not linked it to a database in the settings file. Here is a link that will help you get it installed:
http://decoding.wordpress.com/2012/01/23/how-to-setup-django-and-mysql-python-on-mac-os-x-lion/
(At least on a MAC/Unix. Windows is a different story)
After that, I would just follow any guide on linking the two.
(Some Generic one from a blog)
http://nisthaonweb.com/blog/2012/02/setting-up-django-with-mysql-with-and-without-mamp/

Why just starting the Django development server fail with "keywords must be strings"?

I'm starting to learn Django and have been following the online tutorial on the Django website.
Everything was working fine and I have a folder called mysite and then sub folders called mysite/ and manage.py like it says in the tutorial: https://docs.djangoproject.com/en/1.5/intro/tutorial01/#creating-a-project
However when I run the command
$ python manage.py runserver
I get this:
Validating models...
Unhandled exception in thread started by <bound method Command.inner_run of <django.contrib.staticfiles.management.commands.runserver.Command object at 0x10165aed0>>
Traceback (most recent call last):
File "/Library/Python/2.6/site-packages/django/core/management/commands/runserver.py", line 92, in inner_run
self.validate(display_num_errors=True)
File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 280, in validate
num_errors = get_validation_errors(s, app)
File "/Library/Python/2.6/site-packages/django/core/management/validation.py", line 35, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "/Library/Python/2.6/site-packages/django/db/models/loading.py", line 166, in get_app_errors
self._populate()
File "/Library/Python/2.6/site-packages/django/db/models/loading.py", line 72, in _populate
self.load_app(app_name, True)
File "/Library/Python/2.6/site-packages/django/db/models/loading.py", line 96, in load_app
models = import_module('.models', app_name)
File "/Library/Python/2.6/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Library/Python/2.6/site-packages/django/contrib/auth/models.py", line 21, in <module>
from django.contrib.contenttypes.models import ContentType
File "/Library/Python/2.6/site-packages/django/contrib/contenttypes/models.py", line 127, in <module>
class ContentType(models.Model):
File "/Library/Python/2.6/site-packages/django/db/models/base.py", line 97, in __new__
new_class.add_to_class('_meta', Options(meta, **kwargs))
TypeError: Error when calling the metaclass bases
__init__() keywords must be strings
According to the tutorial it should say this:
Validating models...
0 errors found
April 14, 2013 - 15:50:53
Django version 1.5, using settings 'mysite.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
I've tried to go to http://127.0.0.1:8000/ in my web browser but I just get te error cant connect.
Does anybody know where I am going wrong or what I need to do to rectify the issue?
I'm using Django 1.5 and Python 2.6.1.
According to the Django docs:
Django 1.5 works with any Python version from 2.6.5 to 2.7.
And according to this ticket, I would suggest you to upgrade your Python version. Or, if you can't upgrade it, you have another way, which is presented in the last comment of the ticket:
Use Django 1.4, which supports Python 2.5+

Categories