When I start the local dev server (./manage runserver) the settings.py is run four times. I noticed that, because a error/debug message is printed four times.
Any ideas how this can come? I don't even have an idea where to start looking. It's important for me because I'm struggling with the setup of Sentry, which doesn't report any errors of the site the way it is installed now.
Update:
I checked the imported module for import settings, there are none in my apps.
Then I added
import traceback; traceback.print_stack(); print
to settings.py. The result is:
File "./manage.py", line 5, in <module>
import settings # Assumed to be in the same directory.
File "(...)/myapp/settings.py", line 4, in <module>
import traceback; traceback.print_stack(); print
File "./manage.py", line 12, in <module>
execute_manager(settings)
(...)
File "(...)/site-packages/django/conf/__init__.py", line 73, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "(...)/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "(...)/myapp/../myapp/settings.py", line 4, in <module>
import traceback; traceback.print_stack(); print
File "./manage.py", line 5, in <module>
import settings # Assumed to be in the same directory.
File "(...)/myapp/settings.py", line 4, in <module>
import traceback; traceback.print_stack(); print
File "./manage.py", line 12, in <module>
execute_manager(settings)
(...)
File "(...)/site-packages/django/conf/__init__.py", line 73, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "(...)/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "(...)/myapp/../myapp/settings.py", line 4, in <module>
import traceback; traceback.print_stack(); print
Is that intended behaviour? If not, how can I find my error?
It's being accessed via different entries in sys.path. You should never attempt to import settings yourself; import django.conf.settings instead.
This behavior actually occurs in brand new Django 1.3 Projects. When the development server is started for the first time, it does four imports:
$ django-admin.py startproject test_project
$ cd test_project/
# settings.py
print "importing settings.py"
...
$ python manage.py runserver
importing settings.py
importing settings.py
importing settings.py
importing settings.py
Validating models...
0 errors found
Django version 1.3, using settings 'test_project.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
If you edit a file with the development server running, it only does two imports:
$ touch settings.py
Output:
importing settings.py
importing settings.py
Validating models...
0 errors found
Django version 1.3, using settings 'test_project.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
I'm not sure why it does this, or if it does it on non-development servers. But the behavior does appear to be normal.
If you track imports in settings.py, it doesn't persist values across the multiple imports:
# settings.py
import_count = 0
import_count += 1
print "import count: %d" % import_count
Fresh start:
$ python manage.py runserver
import count: 1
import count: 1
import count: 1
import count: 1
Validating models...
Changed a file:
import count: 1
import count: 1
Validating models...
So maybe it's just reading the file to configure itself, and verify that everything is OK, before importing it for the final time and actually using it?
Related
Our Django application uses Django Rest Framework, Oauth2 and double settings - one to develop and other to production, and a general one called base.py. The development.py is currently set up on manage.py file. We are also using django-cors-headers package.
This exception starts yesterday when I use ./manage.py runserver or another command. I tried to fix it. I read many posts from here (Stackoverflow) and in other websites, but I did not make it.
Could someone help me, please?
Traceback (most recent call last):
File "./manage.py", line 5, in <module>
from settings import base, development
File "/home/ijdev/Área de Trabalho/izio/izio-bank/settings/base.py", line 12, in <module>
from corsheaders.defaults import default_methods as cors_default_methods
File "/home/ijdev/Área de Trabalho/izio/Izioenv/lib/python3.7/site-packages/corsheaders/defaults.py", line 14, in <module>
CORS_ALLOW_HEADERS = getattr(settings, 'CORS_ALLOW_HEADERS', default_headers)
File "/home/ijdev/Área de Trabalho/izio/Izioenv/lib/python3.7/site-packages/django/conf/__init__.py", line 79, in __getattr__
self._setup(name)
File "/home/ijdev/Área de Trabalho/izio/Izioenv/lib/python3.7/site-packages/django/conf/__init__.py", line 64, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting CORS_ALLOW_HEADERS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
In your base settings you have
from corsheaders.defaults import default_methods as cors_default_methods
This causes a circular import because corsheaders tries to import settings.
To solve the problem, remove the import from settings.
I'm new to django (using python 2.7) and I was just trying to use heroku for the first time but I always get the following error:
remote: -----> Preparing static assets
remote: Collectstatic configuration error. To debug, run:
remote: $ heroku run python manage.py collectstatic --noinput
When I run that command, an import error regarding the django registration redux library shows up. I've had this problem before in Django and I fixed it by placing RequestSite under 'requests' and Site under 'models'. That solved the problem but the error still shows up in Heroku.
(venv) C:\Users\Carolina\Desktop\Coding\venv\project1>heroku run python manage.p
y collectstatic --noinput
Running python manage.py collectstatic --noinput on acla-acla... up, run.3645
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 350, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/_
_init__.py", line 324, in execute
django.setup()
File "/app/.heroku/python/lib/python2.7/site-packages/django/__init__.py", lin
e 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/app/.heroku/python/lib/python2.7/site-packages/django/apps/registry.py"
, line 115, in populate
app_config.ready()
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/admin/app
s.py", line 22, in ready
self.module.autodiscover()
File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/admin/__i
nit__.py", line 26, in autodiscover
autodiscover_modules('admin', register_to=site)
File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/module_load
ing.py", line 50, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "/app/.heroku/python/lib/python2.7/importlib/__init__.py", line 37, in im
port_module
__import__(name)
File "/app/.heroku/python/lib/python2.7/site-packages/registration/admin.py",
line 2, in <module>
from django.contrib.sites.models import RequestSite
ImportError: cannot import name RequestSite
The thing is - that line doesn't exist. I went to venv/lib/site-packages/registration/admin.py and line 2 is this one:
from django.contrib import admin
from django.contrib.sites.requests import RequestSite # HERE
from django.contrib.sites.models import Site
from django.utils.translation import ugettext_lazy as _
from registration.models import RegistrationProfile
from registration.users import UsernameField
class RegistrationAdmin(admin.ModelAdmin):
actions = ['activate_users', 'resend_activation_email']
list_display = ('user', 'activation_key_expired')
raw_id_fields = ['user']
search_fields = ('user__{0}'.format(UsernameField()), 'user__first_name', 'user__last_name')
def activate_users(self, request, queryset):
"""
Activates the selected users, if they are not already
activated.
"""
for profile in queryset:
RegistrationProfile.objects.activate_user(profile.activation_key)
activate_users.short_description = _("Activate users")
def resend_activation_email(self, request, queryset):
"""
Re-sends activation emails for the selected users.
Note that this will *only* send activation emails for users
who are eligible to activate; emails will not be sent to users
whose activation keys have expired or who have already
activated.
"""
if Site._meta.installed:
site = Site.objects.get_current()
else:
site = RequestSite(request)
for profile in queryset:
if not profile.activation_key_expired():
profile.send_activation_email(site)
resend_activation_email.short_description = _("Re-send activation emails")
admin.site.register(RegistrationProfile, RegistrationAdmin)
This is what I get with pip freeze, just in case:
Django==1.9
django-crispy-forms==1.5.2
django-registration==2.0.3
django-registration-redux==1.2
django-tinymce==2.2.0
Pillow==3.0.0
requests==2.9.0
South==1.0.2
stripe==1.27.1
wheel==0.24.0
Anyone knows why this is happening? Thanks in advance!
EDIT ----
Ok, so the problem was the one mentioned by Daniel Roseman. The library is broken in pypi and I had to tell heroku to install it from github (where the package is fixed).
So, I went to my requirements.txt file and replaced this line:
django-registration-redux==1.2
with this one:
-e git://github.com/macropin/django-registration.git#egg=django-registration==1.2
(I also removed 'django-registration==2.0.3' because it is an old version of django-registration-redux and was creating problems).
Hope this helps people with the same issue!
It sounds like you edited the code for your locally installed copy of django-registration-redux. But that won't have any effect on Heroku, since the library will be installed directly from PyPI, according to the version in your requirements.txt.
If the library is really broken, you will need to fork it and point your requirements.txt to your fixed version. However, looking at the code on GitHub, it doesn't actually seem to be broken; you just need to update the version you are pointing to.
I am trying to use Pycharm Community Edition to improve on my code in my Django application, but I cannot run all of my Django code that I'd like. I keep getting this traceback...
Traceback (most recent call last):
File "C:/Users/Jaysp_000/firstSite/PROJECTone/blog_static/views.py", line 1, in <module>
from django.views.decorators.csrf import csrf_exempt
File "C:\Python34\lib\site-packages\django\views\decorators\csrf.py", line 3, in <module>
from django.middleware.csrf import CsrfViewMiddleware, get_token
File "C:\Python34\lib\site-packages\django\middleware\csrf.py", line 14, in <module>
from django.utils.cache import patch_vary_headers
File "C:\Python34\lib\site-packages\django\utils\cache.py", line 26, in <module>
from django.core.cache import caches
File "C:\Python34\lib\site-packages\django\core\cache\__init__.py", line 34, in <module>
if DEFAULT_CACHE_ALIAS not in settings.CACHES:
File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 48, in __getattr__
self._setup(name)
File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 42, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
This error seems to involve the django.views.decortors.csrf.csrf_exempt that I imported to my views.py file. I've tried other files, and they have given me no issues. There is something in particular about this import, but I don't know what.
from django.views.decortors.csrf import csrf_exempt
#csrf_exempt
def handle_hook(request):
from django.http import HttpResponse
from django.core.management import call_command
result = call_command('update_blog', verbosity = 0)
return HttpResponse(result)
The same kind of issue shows up when I am trying to run the code on the python shell (I use 3.4) and when I import django.http.request as request. I type in handle_hook(request), and I get the same kind of error.
Im being told that I must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings, but I haven't a clue on how to do that. I've looked around and I am not certain if those methods specifically speak to my issue. Any clues?
Go to the Run Menu, select Edit Configurations..., then select the run configuration for you tests.
Select the environment variables button. You'll see one existing variable, which is PYTHONUNBUFFERED
Under that add (for example) DJANGO_SETTINGS_MODULE=mysitename.settings
I have a python django webserver that I am trying to use memcached to make it faster.
I have downloaded and installed memcached and started it a user called virtual as follows:
/usr/local/bin/memcached -u virtual &
on the django setting.py, I have put the memcached server as this:
MEMCACHE_HOSTS = ['192.168.101.1:11211']
I can do telnet 192.168.101.1 11211 and stats, I do see some statistics there etc.
How do I really know if my django server utilizing the memcached? Is there directory that I can look at or some files to confirm?
Thank you for any insight.
content of manage.py:
#!/usr/bin/env python
import os
import sys
from django.core.management import execute_from_command_line
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
from the command line:
phthon
from django.core.management import execute_from_command_line
and when I do this:
from django.core.cache import cache
I get these errors:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib64/python2.6/site-packages/django/core/cache/__init__.py", line 69, in <module>
if DEFAULT_CACHE_ALIAS not in settings.CACHES:
File "/usr/local/lib64/python2.6/site-packages/django/conf/__init__.py", line 54, in __getattr__
self._setup(name)
File "/usr/local/lib64/python2.6/site-packages/django/conf/__init__.py", line 47, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
You can test cache in Django's shell (python manage.py shell):
>>> from django.core.cache import cache
>>> cache.set('foo', 'bar', 600)
>>> cache.get('foo')
'bar'
If cache.get() returns the set value it means that cache is working as it should. Otherwise it will return None.
An other option is to start memcached with $ memcached -vv, since it will log all the cache accesses to the terminal. Or alternatively you can install monitoring tool for memcached (i.e. memcache-top) and check that something is happening in memcached while using your app.
I followed the tutorial Tango with Django to build up my Django project. And everything runs OK.
The file structure:
tango/
rango/
tango/
wsgi.py
settings.py
manage.py
Now I am trying to deploy the project on the CherryPy server, following this tutorial. The default content of wsgi.py is as follows:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tango.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Now in the same folder as wsgi.py, I create the server.py:
from wsgi import application
import cherrypy
if __name__ == '__main__':
# Mount the application
cherrypy.tree.graft(application, "/")
# Unsubscribe the default server
cherrypy.server.unsubscribe()
# Instantiate a new server object
serve = cherrypy._cpserver.Server()
# Configure the server object
server.socket_host = "0.0.0.0"
server.socket_port = 8080
server.thread_pool = 30
# Subscribe this server
server.subscribe()
# Start the server engine (Option 1 *and* 2)
cherrypy.engine.start()
cherrypy.engine.block()
My question:
If I run server.py within Eclipse (Right click server.py --> Run As --> Python Run), everything works just find. However, if I enter the command $ python server.py in a terminal, the following error messages show up:
Traceback (most recent call last):
File "server.py", line 1, in <module>
from wsgi import application
File "<tangoProject>/tango/wsgi.py", line 14, in <module>
application = get_wsgi_application()
File "<virtualenv>/local/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
django.setup()
File "<virtualenv>/local/lib/python2.7/site-packages/django/__init__.py", line 20, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "<virtualenv>/local/lib/python2.7/site-packages/django/conf/__init__.py", line 46, in __getattr__
self._setup(name)
File "<virtualenv>/local/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
self._wrapped = Settings(settings_module)
File "<virtualenv>/local/lib/python2.7/site-packages/django/conf/__init__.py", line 98, in __init__
% (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'tango.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named tango.settings
Note, in the above I used <djangoProject> and <virtualenv> to specify the directories of the project and virtualenv, respectively.
It seemed that the server is not able to find tango/settings.py file. How do I fix it?
In your server.py before importing from wsgi add:
import sys
sys.path.append('/the/path/to/your/project')
Then, the line importing from wsgi change it to:
from tango.wsgi import application