I'm using django 1.6b and python 3.3 and I get this import error,
./manage.py runserver
Validating models...
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x104ae4d40>
Traceback (most recent call last):
File "/opt/boxen/pyenv/versions/side-project/lib/python3.3/site-packages/django/utils/autoreload.py", line 93, in wrapper
fn(*args, **kwargs)
File "/opt/boxen/pyenv/versions/side-project/lib/python3.3/site-packages/django/core/management/commands/runserver.py", line 97, in inner_run
self.validate(display_num_errors=True)
File "/opt/boxen/pyenv/versions/side-project/lib/python3.3/site-packages/django/core/management/base.py", line 308, in validate
num_errors = get_validation_errors(s, app)
File "/opt/boxen/pyenv/versions/side-project/lib/python3.3/site-packages/django/core/management/validation.py", line 34, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "/opt/boxen/pyenv/versions/side-project/lib/python3.3/site-packages/django/db/models/loading.py", line 196, in get_app_errors
self._populate()
File "/opt/boxen/pyenv/version/side-project/lib/python3.3/site-packages/django/db/models/loading.py", line 78, in _populate
self.load_app(app_name)
File "/opt/boxen/pyenv/versions/side-project/lib/python3.3/site-packages/django/db/models/loading.py", line 99, in load_app
models = import_module('.models', app_name)
File "/opt/boxen/pyenv/versions/side-project/lib/python3.3/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Users/moi/Projects/sos/dealerships/models.py", line 4, in <module>
from django.contrib.gis.db import models # as geomodels
File "/opt/boxen/pyenv/versions/side-project/lib/python3.3/site-packages/django/contrib/gis/db/models/__init__.py", line 8, in <module>
from django.contrib.gis.db.models.manager import GeoManager
File "/opt/boxen/pyenv/versions/side-project/lib/python3.3/site-packages/django/contrib/gis/db/models/manager.py", line 2, in <module>
from django.contrib.gis.db.models.query import GeoQuerySet
File "/opt/boxen/pyenv/versions/side-project/lib/python3.3/site-packages/django/contrib/gis/db/models/query.py", line 6, in <module>
from django.contrib.gis.db.models.fields import get_srid_info, PointField, LineStringField
File "/opt/boxen/pyenv/versions/side-project/lib/python3.3/site-packages/django/contrib/gis/db/models/fields.py", line 4, in <module>
from django.contrib.gis import forms
File "/opt/boxen/pyenv/versions/side-project/lib/python3.3/site-packages/django/contrib/gis/forms/__init__.py", line 2, in <module>
from .fields import (GeometryField, GeometryCollectionField, PointField,
File "/opt/boxen/pyenv/versions/side-project/lib/python3.3/site-packages/django/contrib/gis/forms/fields.py", line 11, in <module>
from django.contrib.gis.geos import GEOSException, GEOSGeometry, fromstr
ImportError: cannot import name GEOSException
This is the only code I have in place for gis,
from django.contrib.gis.db import models as geomodels
from django.utils.translation import ugettext as _
from django.contrib.localflavor.us.models import USStateField
class UsLocation(geomodels.Model):
address_1 = geomodels.CharField(_("address"), max_length=128)
address_2 = geomodels.CharField(_("address cont'd"), max_length=128, blank=True)
city = geomodels.CharField(_("city"), max_length=64, default="Kansas City")
state = USStateField(_("state"), default="KO")
zip_code = geomodels.CharField(_("zip code"), max_length=5, default="16874")
For now I'm going to not use the gis helpers for my address model but I'm not sure what's causing this import exception. I checked the django 1.5/1.6 releases and they have the same lines. I wonder if it has something to do with me using 1.6b w/ python 3.3.2 ?
Make sure you have installed the Geospatial libraries. I have tested Python 3.3.2 and Django 1.6 beta 2 in a virtual env. The import from contrib.gis works fine.
>>> import django
>>> django.VERSION
(1, 6, 0, 'beta', 2)
>>> from django.contrib.gis.db import models as geomodels
Note that contrib.localflavor has been removed from Django 1.6.
>>> from django.contrib.localflavor.us.models import USStateField
Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: No module named 'django.contrib.localflavor'
You can install the django-localflavor package with:
pip install django-localflavor
then change your import to:
from localflavor.us.models import USStateField
I have just faced the same problem. Unfortunately, ImportError: cannot import name GEOSException is not the best error message when Django cannot find GEOS library.
I have Django 1.6.1 final and Python 3.3 too. OS is Windows 7 64-bit. Python, GEOS are x86-64 too.
As recommended at Django docs, the line:
GEOS_LIBRARY_PATH = r'C:\Program Files\OSGeo4W64\bin\geos_c.dll'
in my Django project settings solved this issue.
Related
when i want do an import django i get an error
ubuntu 18.04 with a reverse proxy (nginx) and uwsgi (mode emperor actived) in virtual env with python 3.6.3 and latest django 2.2.5
test.py:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testproject.settings")
import django
print("test")'
when i run python3 test.py i get :
(venv) :~/testproject/testproject/testproject$ python3.6 test.py
Traceback (most recent call last):
File "test.py", line 3, in <module>
from django.http import HttpResponse
File "/home/lukas/testproject/venv/lib/python3.6/site-packages/django/__init__.py", line 1, in <module>
from django.utils.version import get_version
File "/home/lukas/testproject/venv/lib/python3.6/site-packages/django/utils/version.py", line 4, in <module>
import subprocess
File "/usr/lib/python3.6/subprocess.py", line 140, in <module>
import threading
File "/usr/lib/python3.6/threading.py", line 7, in <module>
from traceback import format_exc as _format_exc
File "/usr/lib/python3.6/traceback.py", line 5, in <module>
import linecache
File "/home/lukas/testproject/venv/lib/python3.6/linecache.py", line 11, in <module>
import tokenize
File "/home/lukas/testproject/venv/lib/python3.6/tokenize.py", line 35, in <module>
from token import *
File "/home/lukas/testproject/testproject/testproject/token.py", line 1, in <module>
from django.contrib.auth.tokens import PasswordResetTokenGenerator
File "/home/lukas/testproject/venv/lib/python3.6/site-packages/django/contrib/auth/__init__.py", line 4, in <module>
from django.apps import apps as django_apps
File "/home/lukas/testproject/venv/lib/python3.6/site-packages/django/apps/__init__.py", line 2, in <module>
from .registry import apps
File "/home/lukas/testproject/venv/lib/python3.6/site-packages/django/apps/registry.py", line 426, in <module>
apps = Apps(installed_apps=None)
File "/home/lukas/testproject/venv/lib/python3.6/site-packages/django/apps/registry.py", line 46, in __init__
self.ready_event = threading.Event()
AttributeError: module 'threading' has no attribute 'Event'
i have the same error on python3 shell when i do import django whereas django have been installed with pip3 install and production mode is ok and developement mode with runserver is ok too.
can you help me, i m lost...
Do you have, by any chance, a token module in you django project?
As the python path is modified to prefer local modules rather than other pre-defined modules, the from token import * instruction will import from your module instead of the python lib one.
Try renaming your local modules to avoid collision with builtin python modules.
After upgrading my Ubuntu desktop to 18.04 from 16.04, the django's virtualenv refuses to start:
(.djangoenv) mw#desktop:~/theapp$ python manage.py runserver
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
File "/home/mw/.djangoenv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 10, in <module>
from django.apps import apps
File "/home/mw/.djangoenv/local/lib/python2.7/site-packages/django/apps/__init__.py", line 1, in <module>
from .config import AppConfig # NOQA
File "/home/mw/.djangoenv/local/lib/python2.7/site-packages/django/apps/config.py", line 4, in <module>
from django.core.exceptions import AppRegistryNotReady, ImproperlyConfigured
File "/home/mw/.djangoenv/local/lib/python2.7/site-packages/django/core/exceptions.py", line 5, in <module>
from django.utils.encoding import force_text
File "/home/mw/.djangoenv/local/lib/python2.7/site-packages/django/utils/encoding.py", line 10, in <module>
from django.utils.functional import Promise
File "/home/mw/.djangoenv/local/lib/python2.7/site-packages/django/utils/functional.py", line 1, in <module>
import copy
File "/usr/lib/python2.7/copy.py", line 52, in <module>
import weakref
File "/usr/lib/python2.7/weakref.py", line 14, in <module>
from _weakref import (
ImportError: cannot import name _remove_dead_weakref
On the djangoenv and outside it
$ python --version :
`Python 2.7.12`
Hoping to resolve the issue, I also install Python 2.7.16 on the desktop, so I get:
$ python2.7 --version
Python 2.7.16
Also when I try to install new virtualenv, I get the same error:
$ virtualenv .blaenv
Running virtualenv with interpreter /home/mw/.djangoenv/bin/python2
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/virtualenv.py", line 21, in <module>
import logging
File "/usr/lib/python2.7/logging/__init__.py", line 26, in <module>
import sys, os, time, cStringIO, traceback, warnings, weakref, collections
File "/usr/lib/python2.7/weakref.py", line 14, in <module>
from _weakref import (
ImportError: cannot import name _remove_dead_weakref
Answers to the same error in other contexts did not help and my search led to no more clues.
So really appreciate your hints.
As per https://askubuntu.com/questions/981663/python2-7-broken-by-weakref-import-error-please-help you probably need to recreate your virtualenv. I suspect your problem here is that you need to deactivate your current env before you try to make the new one, in order to ensure that your system python2 interpreter is the one running the virtualenv command.
I get the following error when instancing my Django app as a lambda function using Zappa.
[1549659279288] Instancing..
[1549659279532] cannot import name 'models': ImportError
Traceback (most recent call last):
File "/var/task/handler.py", line 580, in lambda_handler
return LambdaHandler.lambda_handler(event, context)
File "/var/task/handler.py", line 245, in lambda_handler
handler = cls()
File "/var/task/handler.py", line 151, in __init__
wsgi_app_function = get_django_wsgi(self.settings.DJANGO_SETTINGS)
File "/var/task/zappa/ext/django_zappa.py", line 9, in get_django_wsgi
from django.core.wsgi import get_wsgi_application
File "/var/task/django/core/wsgi.py", line 2, in <module>
from django.core.handlers.wsgi import WSGIHandler
File "/var/task/django/core/handlers/wsgi.py", line 8, in <module>
from django.core.handlers import base
File "/var/task/django/core/handlers/base.py", line 7, in <module>
from django.urls import get_resolver, set_urlconf
File "/var/task/django/urls/__init__.py", line 1, in <module>
from .base import (
File "/var/task/django/urls/base.py", line 8, in <module>
from .exceptions import NoReverseMatch, Resolver404
File "/var/task/django/urls/exceptions.py", line 1, in <module>
from django.http import Http404
File "/var/task/django/http/__init__.py", line 5, in <module>
from django.http.response import (
File "/var/task/django/http/response.py", line 13, in <module>
from django.core.serializers.json import DjangoJSONEncoder
File "/var/task/django/core/serializers/__init__.py", line 23, in <module>
from django.core.serializers.base import SerializerDoesNotExist
File "/var/task/django/core/serializers/base.py", line 6, in <module>
from django.db import models
ImportError: cannot import name 'models'
I have tried running the app locally using,
python manage.py runserver
and it works fine locally.
However, I get the above error when the app is deployed to a lambda function.
I'm using Django version 2.0.10, Python 3.6 along with an AWS Aurora RDS.
I expect the lambda function instance without errors in the same way as it does locally.
Invoke zappa to keep packaged zip after deploy and check folder structure or try to run the project from that folder.
Did you add Django to requirements file? As i recall Zappa rebuilds lambda package from a fresh virtual environment, installs requirements.txt and applies your folder.
Or check carefully cloudwatch logs for import errors. There are import errors that show up only when the container is refreshed after a deploy, that will not show up for every lambda invocations.
The following error is getting while attempting to run a sample Python Flask application.
Traceback (most recent call last):
File "/home/bineeth/Projects/python/xcontent/bin/flask", line 7, in <module>
from flask.cli import main
File "/home/bineeth/Projects/python/xcontent/lib/python3.5/site-packages/flask/__init__.py", line 18, in <module>
from werkzeug.exceptions import abort
File "/home/bineeth/Projects/python/xcontent/lib/python3.5/site-packages/werkzeug/__init__.py", line 152, in <module>
__import__('werkzeug.exceptions')
File "/home/bineeth/Projects/python/xcontent/lib/python3.5/site-packages/werkzeug/exceptions.py", line 66, in <module>
from werkzeug.http import HTTP_STATUS_CODES
File "/home/bineeth/Projects/python/xcontent/lib/python3.5/site-packages/werkzeug/http.py", line 31, in <module>
from werkzeug.datastructures import (# noqa
File "/home/bineeth/Projects/python/xcontent/lib/python3.5/site-packages/werkzeug/datastructures.py", line 24, in <module>
from werkzeug.http import dump_options_header, dump_header, generate_etag, \
ImportError: cannot import name 'dump_options_header'
Following is the source code;
from flask import Flask
app = Flask(__name__)
#app.route('/ping')
def ping():
return {'status':'completed'}
if __name__ == '__main__':
app.run(debug=False)
I am using Python 3 and LiClipse IDE. Also Linux Mint is the Operating System.
Appreciate your valuable help in this regard.
I am upgrading my django application from django1.5 to django1.11. I know its a huge jump. So I am getting so many errors and try to get it fixed. This is my project structure. I think I have made mistakes in appconfig. I couldn't figure it out.
Now I am stuck on this error.ImportError: cannot import name TrainingProfile
settings.py
INSTALLED_APPS = (
'admin.apps.AdminConfig',
'account.apps.AccountConfig',
'.............'
)
apps/admin/apps.py
class AdminConfig(AppConfig):
name = 'apps.admin'
label = 'admin_app'
apps/account/apps.py
class AccountConfig(AppConfig):
name = 'apps.account'
label = 'account_app'
apps/admin/models/init.py
from apps.admin.models.sector import *
from apps.admin.models.track import *
from apps.admin.models.training import *
...............
traceback
Traceback (most recent call last):
File "/home/sample-applications/upgrade/venv/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 228, in wrapper
fn(*args, **kwargs)
File "/home/sample-applications/upgrade/venv/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
autoreload.raise_last_exception()
File "/home/sample-applications/upgrade/venv/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 251, in raise_last_exception
six.reraise(*_exception)
File "/home/sample-applications/upgrade/venv/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 228, in wrapper
fn(*args, **kwargs)
File "/home/sample-applications/upgrade/venv/local/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/sample-applications/upgrade/venv/local/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models()
File "/home/sample-applications/upgrade/venv/local/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/sample-applications/upgrade/pursuite/django-pursuite/apps/admin/models/__init__.py", line 17, in <module>
from apps.admin.models.job import *
File "/home/sample-applications/upgrade/pursuite/django-pursuite/apps/admin/models/job.py", line 13, in <module>
from account.models import IndustryProfile
File "./apps/account/models.py", line 13, in <module>
from admin.models import Company, Track
File "./apps/admin/models/__init__.py", line 18, in <module>
from apps.admin.models.training import *
File "/home/sample-applications/upgrade/pursuite/django-pursuite/apps/admin/models/training.py", line 12, in <module>
from account.models import TrainingProfile
ImportError: cannot import name TrainingProfile
apps/admin/models/training.py
from django.db import models
from django.contrib import admin
from django.core.urlresolvers import reverse
from tinymce.models import HTMLField
from account.models import TrainingProfile
from analytics.models import State
from admin.common import html2text
__all__ = ['Training']
class Training(models.Model):
'''
Training
'''
class Meta:
'''
Meta properties for this model
'''
app_label = 'admin'
TRAINING_CHOICES = {
('T', 'Trainers'),
('S', 'Students'),
}
training_title = models.CharField(max_length=100, db_index=True)
provider = models.ForeignKey(TrainingProfile, db_index=True)
training_for = models.CharField(max_length=1, choices=TRAINING_CHOICES)
description = HTMLField()
location = models.ForeignKey(State, db_index=True)
create_date = models.DateTimeField(auto_now_add=True)
write_date = models.DateTimeField(auto_now=True)
The relevant part in your traceback is this:
File "/home/sample-applications/upgrade/pursuite/django-pursuite/apps/admin/models/__init__.py", line 17, in <module>
from apps.admin.models.job import *
File "/home/sample-applications/upgrade/pursuite/django-pursuite/apps/admin/models/job.py", line 13, in <module>
from account.models import IndustryProfile
File "./apps/account/models.py", line 13, in <module>
from admin.models import Company, Track
File "./apps/admin/models/__init__.py", line 18, in <module>
from apps.admin.models.training import *
File "/home/sample-applications/upgrade/pursuite/django-pursuite/apps/admin/models/training.py", line 12, in <module>
from account.models import TrainingProfile
ImportError: cannot import name TrainingProfile
From here I see that you are importing admin models from account models (Company and Track) and vice versa (IndustryProfile, TrainingProfile) which is making a circular import. It's very strange that this was working in Django 1.5 at all.
To fix your problem you can check how you are using these models and if the only thing is to put it as argument to models.ForeignKey you can remove the import and use string instead ('account.IndustryProfile', 'account.TrainingProfile', 'admin.Company' and 'admin.Track')
If you can't replace all of them try to replace at least these that will fix your problem.
More info at Django documentation https://docs.djangoproject.com/en/1.11/ref/models/fields/#foreignkey
Your model is in a file named training.py while the file name is not presented in the import at all. It is not in the file structure above neither, so just take a good look at your files and fix your import statement.