Django manage.py custom command on Heroku - python

I am trying to run a cron job with a Heroku hosted website. I am using a Django back-end. I have written a custom manage.py command that works locally (status_crawl.py). I have seen a few guides and posts on how to fix this, but the suggestions did not work (such as Running Django custom manage.py task on Heroku - Importing Issues and this guide).
Even weirder is that it is recognizing the other commands. (And I managed to get the basic task to work by using runscript from the django-extensions framework). It is still bothering me though.
My basic file structure likes like:
likes:
...
management:
__init__.py
commands:
__init__.py
status_crawl.py
The traceback is:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 272, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 77, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
ImportError: No module named commands.status_crawl
The other weird thing is that when I run 'heroku run python manage.py help' the command is showing up as an option. It's just not running when I try to run the script myself using the manage.py command.
Thanks!

Probably it's related to your PYTHONPATH variable on Heroku. This variable on Heroku may differ from your local PYTHONPATH. Try to import from the root of your project, like:
from likes.management.commands import status_crawl

Related

django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. django 1.8

I have a problem with django 1.8. The console shows:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/adriann/django/project_varincenti/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 351, in execute_from_command_line
utility.execute()
File "/home/adriann/django/project_varincenti/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 343, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/adriann/django/project_varincenti/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 177, in fetch_command
commands = get_commands()
File "/home/adriann/django/project_varincenti/env/local/lib/python2.7/site-packages/django/utils/lru_cache.py", line 101, in wrapper
result = user_function(*args, **kwds)
File "/home/adriann/django/project_varincenti/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 72, in get_commands
for app_config in reversed(list(apps.get_app_configs())):
File "/home/adriann/django/project_varincenti/env/local/lib/python2.7/site-packages/django/apps/registry.py", line 137, in get_app_configs
self.check_apps_ready()
File "/home/adriann/django/project_varincenti/env/local/lib/python2.7/site-packages/django/apps/registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
There are answers elsewhere on Stackoverflow about how this could be actually about app registry, but I believe it is more often just an indicator that something is wrong in the settings and Django didn't handle failure well.
I just had the exact same error by just having a bad logger filename in LOGGING settings -- the directory I was referring to did not exist, so logger initialisation failed.
It looks like you are using a Virtual Environment.
Perhaps you have an app included under INSTALLED_APPS in settings.py, but it isn't installed (e.g. via pip install django-multisite) in your virtual environment.
(I received this same error after starting to use a Virtual Environment.)
I actually had the same error today, which is strange as everything was working properly yesterday and I do not think I have changed anything.
I got the error when trying to start up the dev server. I had recently upgraded this project to 1.8.
The solution for me was: I changed my PYTHONPATH on the startup script (to make sure it also points to the 1.8 version) of the dev server and everything seemed to work perfectly again.

Change requires_model_validation in a project

I've uploaded from django 1.6 to django 1.7 and from python 3.2.3 to python 3.4.1.
Suddenly when I try to run the makemessages command to catch the translating messages I find this error:
django.core.exceptions.ImproperlyConfigured: Command Command defines both "requires_model_validation" and "requires_system_checks", which is illegal. Use only "requires_system_checks".
I've googled it and I don't find where or what I should edit. In fact I've never used this parameter so...
Any idea?
The traceback
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/pablo/Workspaces/milao/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/home/pablo/Workspaces/milao/lib/python3.4/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/pablo/Workspaces/milao/lib/python3.4/site-packages/django/core/management/__init__.py", line 238, in fetch_command
klass = load_command_class(app_name, subcommand)
File "/home/pablo/Workspaces/milao/lib/python3.4/site-packages/django/core/management/__init__.py", line 42, in load_command_class
return module.Command()
File "/home/pablo/Workspaces/milao/lib/python3.4/site-packages/django/core/management/base.py", line 228, in __init__
'"requires_system_checks".' % self.__class__.__name__)
django.core.exceptions.ImproperlyConfigured: Command Command defines both "requires_model_validation" and "requires_system_checks", which is illegal. Use only "requires_system_checks".
Finally I've found the answer and it's not related with django core at all. I was not using any command. However one of my installed apps was using it. In this case it's django vinaigrette.
I'll upload the patch ASAP to port this app to django 1.7.
Regards.
Thanks to #Paul Pepper for pointing me in the right direction. For me it was Django devserver causing the issue and updating to the latest version fixed it for me:
pip install -e git://github.com/dcramer/django-devserver.git#HEAD#egg=django-devserver
HEAD was commit e6c882fc11fba013b85d37f04db8f11b531eda9a at the time of this writing.

django could not import settings

I once wrote an django app, named superlists, and settings was in superlists.settings. It was for tutorial, and I was changing differnt settings, little realizing what I was doing.
And now when i starting new django project - it tells me it could not import superlists.settings
I reinstalled Pycharm, django, python (both versions 2.7 and 3.3), deleted all virtual envs, deleted everything that might be connected to it several times... IT IS STILL THERE! on a new, clean version of python, which even dont have django - there is DJANGO_SETTINGS_MODULE in sys.path pointing to superlists.settings. Each new project on a clean, with default settings, version of pycharm, tells me that it cannot import superlists.settings.
I delete DJANGO_SETTINGS_MODULE pointing to that settings from sys.path from cmd - exit then watch again - its there.
Where it takes it? I got no idea.
here is traceback:
Traceback (most recent call last):
File "D:/Python27/Lib/site-packages/django/bin/django-admin.py", line 5, in <module>
management.execute_from_command_line()
File "D:\Python27\Lib\site-packages\django\core\management\__init__.py", line 399, in execute_from_command_line
utility.execute()
File "D:\Python27\Lib\site-packages\django\core\management\__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\Python27\Lib\site-packages\django\core\management\__init__.py", line 261, in fetch_command
commands = get_commands()
File "D:\Python27\Lib\site-packages\django\core\management\__init__.py", line 107, in get_commands
apps = settings.INSTALLED_APPS
File "D:\Python27\Lib\site-packages\django\conf\__init__.py", line 54, in __getattr__
self._setup(name)
File "D:\Python27\Lib\site-packages\django\conf\__init__.py", line 49, in _setup
self._wrapped = Settings(settings_module)
File "D:\Python27\Lib\site-packages\django\conf\__init__.py", line 132, in __init__
% (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'superlists\settings.py' (Is it on sys.path? Is there an import error in the settings file?): Import by filename is not supported.
It turned out that I created new environment system variable DJANGO_SETTINGS_MODULE. So evident, but it almost cost me sanity.
Change the environment variable from
DJANGO_SETTINGS_MODULE=superlist.superlist.settings
To DJANGO_SETTINGS_MODULE=superlist.settings
or use an __init__.py in superlist

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/

Python/Django shell won't start

One of the great features of Django is that you can open a python interpreter set-up for use with your project. This can be used to analyse objects in a database and allows any python commands to be executed on your project. I find it essential for Django development. It is invoked in the project directory using this command:
$ python manage.py shell
I have just started developing a new project and for some reason the shell does not work. I have had a look online for the error and not found anything. I would greatly appreciate any help on this error:
Traceback (most recent call last):
File "manage.py", line 11, in
execute_manager(settings)
File "/Library/Python/2.6/site-packages/django/core/management/__init__.py", line 362, in execute_manager
utility.execute()
File "/Library/Python/2.6/site-packages/django/core/management/__init__.py", line 303, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 195, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 222, in execute
output = self.handle(*args, **options)
File "/Library/Python/2.6/site-packages/django/core/management/base.py", line 351, in handle
return self.handle_noargs(**options)
File "/Library/Python/2.6/site-packages/django/core/management/commands/shell.py", line 29, in handle_noargs
shell = IPython.Shell.IPShell(argv=[])
AttributeError: 'module' object has no attribute 'Shell'
Thanks in advance for your help!
It seems like IPython is installed wrongly somehow. Try starting the shell with:
./manage.py shell --plain
to start the standard Python shell, rather than IPython. If that works, then trying removing IPython completely and reinstalling it.
IPython 0.11 has a different API, for which a fix exists in the last Django versions.
For older Django versions, you can use IPython 0.10, which does work:
pip install ipython==0.10

Categories