I have the need to add version control for my models in the django admin and for that reason I have installed django-reversion from here.My server does not have have internet connectivity so I could not use pip.I had to manually download the zip file from github,upload it on to my server,extract the contents and run python setup.py install to install reversion.
However,when I try to import it,I get the following error :
root#ns1 /home/project# python
Python 2.7.3 (default, Feb 27 2013, 22:57:49)
[GCC 4.1.1 20070105 (Red Hat 4.1.1-52)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.VERSION
(1, 6, 0, 'final', 0)
>>> import reversion
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/reversion/__init__.py", line 11, in <module>
from reversion.revisions import default_revision_manager, revision_context_manager, VersionAdapter
File "/usr/local/lib/python2.7/site-packages/reversion/revisions.py", line 10, in <module>
from django.contrib.contenttypes.models import ContentType
File "/usr/local/lib/python2.7/site-packages/django/contrib/contenttypes/models.py", line 1, in <module>
from django.db import models
File "/usr/local/lib/python2.7/site-packages/django/db/__init__.py", line 83, in <module>
signals.request_started.connect(reset_queries)
File "/usr/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 88, in connect
if settings.DEBUG:
File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 54, in __getattr__
self._setup(name)
File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 47, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
>>>
As per the github page,the current reversion version is 1.8.0 and should be compatible with django 1.6. What could be the problem ?
I know this is an old question, but in case anyone stumbles upon it: The problem here is that you're running the standard python interpreter, rather than the Django shell. You can do import django into a regular python interpreter to check the Django version, but it does NOT set all the various environment variables necessary to run django properly -- including importing django apps. In order to import a django app, you need to instead run the Django shell via
./ python manage.py shell
Related
Some day I lost access to Trac which is Python2.7 software. (I suppose, that deprecation of Python2.x is clue). There is Python3 unstable version, but I can't use it, because there's no plugins yes, which are key for my Trac installation.
So created virtual environment:
virtualenv -p /usr/bin/python2.7 env
And finally installation is succesful!
pip install trac
But... Stil can't run it by wsgi apache2:
Traceback (most recent call last):
File "/var/lib/trac/apache/trac.wsgi", line 13, in <module>
import trac.web.main
File "/var/lib/trac/env/lib/python2.7/site-packages/trac/web/__init__.py", line 19, in <module>
from trac.web.api import *
File "/var/lib/trac/env/lib/python2.7/site-packages/trac/web/api.py", line 18, in <module>
from BaseHTTPServer import BaseHTTPRequestHandler
I tried to install pip install http but still the same problem
my wsgi file:
import sys
sys.stdout = sys.stderr
import os
os.environ['PKG_RESOURCES_CACHE_ZIP_MANIFESTS'] = '1'
os.environ['TRAC_ENV_PARENT_DIR'] = '/var/lib/trac'
os.environ['PYTHON_EGG_CACHE'] = '/var/lib/trac/eggs'
#import trac.db.postgres_backend
#trac.db.postgres_backend.PostgreSQLConnection.poolable = False
import trac.web.main
application = trac.web.main.dispatch_request
How to change wsgi or how to install missing module?
Can anyone help me. I have just installed django-tables2 using easy_install and when i try to import django_tables2 as tables in forms.py it gives me error unresolved import: tables then i try to reinstall again django_tables2 and give me the result as shown below.
$ easy_install django_tables2
Searching for django-tables2
Best match: django-tables2 0.13.0
Processing django_tables2-0.13.0-py2.7.egg
django-tables2 0.13.0 is already the active version in easy-install.pth
Using c:\python27\lib\site-packages\django_tables2-0.13.0-py2.7.egg
Processing dependencies for django-tables2
Finished processing dependencies for django-tables2
Thank you very much for help.
updated question.
I am still trying to figure out this problem. I am posting a new error on why I cannot import. By using python manage.py shell i get this error. I have no idea what is that mean.
$ python
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import django_tables2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build\bdist.win32\egg\django_tables2\__init__.py", line 3, in <module>
File "build\bdist.win32\egg\django_tables2\tables.py", line 4, in <module>
File "c:\Python27\lib\site-packages\django\db\__init__.py", line 11, in <module>
if DEFAULT_DB_ALIAS not in settings.DATABASES:
File "c:\Python27\lib\site-packages\django\utils\functional.py", line 184, in inner
self._setup()
File "c:\Python27\lib\site-packages\django\conf\__init__.py", line 40, in _setup
raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
Can anyone help me?:( Help is very much appreciated. Thank you.
django_tables requires that you actually be running within the django-"primed" python environment. You can access this by going to your project's directory and typing
python manage.py shell
Then you can import django_tables2 with no problems.
For further info, take a look at the following:
https://docs.djangoproject.com/en/dev/ref/django-admin/
Note that this is not about the admin template site (despite the url), it's about the django-admin.py (system-wide) and manage.py (project-wide) "helper" applications. manage.py shell actually starts up a standard python shell with several pre-loaded environment variables and a few imports.
Im trying to make an app with geoDjango but I'm actually having some problem to put it running.
When I excecute the "python manage.py syncdb" command, I have the following error:
File "/media/datos/Proyectos/CualBondi/git/cualbondi/myapp/models.py", line 10, in <module>
from django.contrib.gis.db import models
File "/usr/local/lib/python2.6/dist-packages/django/contrib/gis/db/models/__init__.py", line 5, in <module>
from django.contrib.gis.db.models.aggregates import *
File "/usr/local/lib/python2.6/dist-packages/django/contrib/gis/db/models/aggregates.py", line 2, in <module>
from django.contrib.gis.db.models.sql import GeomField
File "/usr/local/lib/python2.6/dist-packages/django/contrib/gis/db/models/sql/__init__.py", line 2, in <module>
from django.contrib.gis.db.models.sql.query import GeoQuery
File "/usr/local/lib/python2.6/dist-packages/django/contrib/gis/db/models/sql/query.py", line 4, in <module>
from django.contrib.gis.db.models.fields import GeometryField
File "/usr/local/lib/python2.6/dist-packages/django/contrib/gis/db/models/fields/__init__.py", line 3, in <module>
from django.contrib.gis.db.backend import SpatialBackend, gqn
ImportError: cannot import name SpatialBackend
I give you here some extra information:
Django 1.3.1
Python 2.6
Ubuntu 10.04
PostgreSQL 8.4.8
Do you have any idea?
Thank you so much!
Maybe this helps, SpatialBackend seems deprecated. Read the warning below.
$ python manage.py shell
Python 2.7.2+ (default, Oct 4 2011, 20:06:09)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.contrib.gis.db.backend import SpatialBackend, gqn
/usr/lib/pymodules/python2.7/django/contrib/gis/db/backend/__init__.py:5: UserWarning: The `django.contrib.gis.db.backend` module was refactored and renamed to `django.contrib.gis.db.backends` in 1.2. All functionality of `SpatialBackend` has been moved to the `ops` attribute of the spatial database backend. A `SpatialBackend` alias is provided here for backwards-compatibility, but will be removed in 1.3.
warn('The `django.contrib.gis.db.backend` module was refactored and '
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: cannot import name gqn
I'm on django 1.3.0, there seems to work good, but only with that warning.
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?
I am developing a Django project with PyDev in Eclipse. For a while, PyDev's Django Shell worked great. Now, it doesn't:
>>> import sys; print('%s %s' % (sys.executable or sys.platform, sys.version))
C:\Python26\python.exe 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)]
>>>
>>> from django.core import management;import mysite.settings as settings;management.setup_environ(settings)
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: No module named mysite.settings
>>>
The dev server runs just fine. What could I be doing wrong?
The models module is also conspicuously absent:
>>> import mysite.myapp.models
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: No module named mysite.myapp.models
On the normal command line, outside of PyDev, the shell works fine.
Why could this be happening?
Seems like a simple path issue. What's the output of this:
import sys; print sys.path
I don't know anything about PyDev, but there's probably a setting somewhere to add paths to the PYTHONPATH setting. If not, you can do it directly in the shell:
sys.path.insert(0, '/path/to/directory/containing/mysite/')
I had a similar problem to this a while ago while moving my project from Django 1.3 and having the settings.py file at the root of my source and then moving it down into the application.
For example what happened was that I had the following:
rootOfSource/
- settings.py
- myapp
and I changed it to be:
rootOfSource/
- myapp
- myapp/settings.py
and I also changed my settings file to be the following:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
However when I debugged into the os.eviron I found that the DJANGO_SETTINGS_MODULE was not as expected, I then changed my manage.py to be the following:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
os.environ.__setitem__("DJANGO_SETTINGS_MODULE", "myapp.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Which then allowed me to run from PyDev.
Hope this helps.
I fixed this problem by going to the project properties -> PyDev Django and setting the Django settings module.