ImportError when using Haystack 2.0.0 with Django 1.5 and Gunicorn WSGI - django

I use django-haystack 2.0.0 to index my site, and it has been working great until I upgraded to Django 1.5 and started using the WSGI interface. If I just use the django_gunicorn command it works great, but the Django documentation "highly recommends" I use the gunicorn command.
When I start my site with the gunicorn command, Haystack throws the following error on any page load:
ImportError: cannot import name signals
I have no problems importing signals from the Django or Python shells. I use virtualenv and install all packages locally inside that environment. My wsgi.py file looks just like the default one in the django admin, except that I add the local path to the python path as such:
path = os.sep.join(os.path.abspath(__file__).split(os.sep)[:-2])
if path not in sys.path:
sys.path.append(path)`
Any help you could provide would be very appreciated, thank you!

I don't use gunicorn, but I had the same problem when I used the HAYSTACK_SIGNAL_PROCESSOR setting to point to a custom class that I wrote. That class imported one of my models, which eventually propagated up the import chain, to import my settings module, thus causing a circular import.
When using a setting such as HAYSTACK_SIGNAL_PROCESSOR that points to a class, make sure that class standsalone, and doesn't import either directly or indirectly the Django settings file.

Related

Set up django-celery-email for local dev

It seems the best way to send emails from the django-allauth app asynchronously is to simply install django-celery-email. But the packages warns that
This version requires the following versions:Python 2.7 and Python3.5, Django 1.11, 2.1, and 2.2 Celery 4.0
I've only been using python for several months and never encountered a situation where two python version are needed on a project. And I'm using the official recommendation of pipenv for local development. A quick google shows that it isn't possible to have two python interpreters installed in the virtual environment. Since the plugin seems so popular I wondered how others were setting it up? Apologies if I've missed something major that explains this.
A bonus answer would also take into account that I am using docker and the docker image will install the python packages like this.
RUN pipenv install --system --deploy --ignore-pipfile
Many thanks in advance.
I am pretty sure it is just inaccurate description in the project docs, so you need either python 2.7 or python >=3.5 to be installed
In the end I didn't use django-celery-email. It's easy to send the emails generated by the django-allauth app without this package.
I used these resources -
https://github.com/anymail/django-anymail/issues/79
https://docs.djangoproject.com/en/2.2/topics/email/#defining-a-custom-email-backend
Basically you do this to get it working.
In settings.py define a CustomEmailBackend -
EMAIL_BACKEND = "users.backends.CustomEmailBackend"
In a backend.py file define the backend -
from django.core.mail.backends.base import BaseEmailBackend
from .tasks import async_send_messages
class CustomEmailBackend(BaseEmailBackend):
def send_messages(self, email_messages):
async_send_messages.delay(email_messages)
return len(email_messages)
And this is the task -
from django.core.mail import get_connection
from abstract_base_user.celery import app
#app.task(rety_backoff=True, serializer="pickle")
def async_send_messages(email_messages):
conn = get_connection(backend='anymail.backends.mailgun.EmailBackend')
conn.send_messages(email_messages)
The celery django app should be set up in the standard way as defined at https://docs.celeryproject.org/en/latest/django/first-steps-with-django.html
And the celery settings in the settings.py need include the pickle content type -
CELERY_ACCEPT_CONTENT = ['json', 'pickle']
Obviously you need to include your anytime settings and broker settings too. But this should be enough to get anybody started.

Flask-mongoengine: Unable to import MongoEngine From flask-mongoengine

I must be missing something but I look around and couldn't find reference to this issue.
I have the very basic code, as seen in flask-mongoengine documentation.
test.py:
from flask import Flask
from flask_mongoengine import MongoEngine
When I run
python test.py
...
from flask_mongoengine import MongoEngine
ImportError: cannot import name 'MongoEngine'
Module in virtual environment contain (requirements.txt):
click==6.7
Flask==1.0.2
flask-mongoengine==0.9.5
Flask-WTF==0.14.2
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
mongoengine==0.15.3
pymongo==3.7.1
six==1.11.0
Werkzeug==0.14.1
WTForms==2.2.1
My interpreter is Python 3.6.5
Any help would be appreciated. Thanks.
Since your using a virtual environment did you try opening your editor from your virtual environment?
For example opening the vscode editor from command-line is "code". Go to your virtual environment via the terminal and activate then type "code" at your prompt.
terminal:~path/to/virtual-enviroment$ source bin/activate
(virtual-enviroment)terminal:~path/to/virtual-enviroment$ code
If that doesn't work I, myself, haven't used flask-mongoengine. I was nervous of any issues that would come from the abstraction of it and instead just used Mongoengine with Flask.
I'm assuming you're only using this library for connection management so if you can't solve your issue with flask-mongoengine but are still interested in using mongoengine this was my approach. ~
I would put this in a config file somewhere and import it where appropriate-
from flask import Flask
MONGODB_DB = 'DB_NAME'
MONGODB_HOST = '127.0.0.1' # or whatever your db address
MONGODB_PORT = 27017 # or whatever your port
app = Flask(__name__) # you can import app from config and it will keep its configurations
then I would connect and disconnect from the database within each HTTP request function like this-
from config import MONGO_DB, MONGODB_HOST, MONGODB_PORT
# to connect
db = connect(MONGODB_DB, host=MONGODB_HOST, port=MONGODB_PORT)
# to close connection before any returns
db.close()
Hope this helps.
I had this issue and managed to fix it by deactivating, reinstalling flask-mongoengine and reactivating the venv (all in the Terminal):
deactivate
pip install flask-mongoengine
# Not required but good to check it was properly installed
pip freeze
venv\Scripts\activate
flask run

Django Unable to Import Settings Module

I'm trying to host a simple Django app on AWS.
I can run it via the public DNS and runserver, but I can't seem to get the environment URL working.
I checked the log and it says "ImportError: Could not import settings 'AppName.settings'"
I've tried adding sys.path.append('/opt/python/current/app/ProjectName/AppName') to the WSGI file, but when I try to print out the sys.paths, it is not listed.
How can I fix this?
Thanks!
I think you need to import till project name like
sys.path.append('/opt/python/current/app/ProjectName')
That will ensure project directory is in pythonpath and you can import AppName from there.

Can't use django management commands because of Import Errors when haystack tries to import multiligualmodel

I'm using django-haystack for searching on my site.
I'm also using django multilingual model for I18n.
I import MultilingualModel in search_indexes.py
I ca run all manangement commands as long as I don't have haystack in the INSTALLED_APPS.
When haystack is in the INSTALLED_APPS and try to run syncdb or migrate (and several other management commands) I'm always getting:
django.core.exceptions.ImproperlyConfigured: ImportError haystack: cannot import name MultilingualModel
This is likely related to the hacks done in haystack.autodiscover(). This behavior is documented here: http://docs.haystacksearch.org/dev/debugging.html#import-errors-on-start-up-mentioning-handle-registrations There is a long discussion in this ticket: https://github.com/toastdriven/django-haystack/issues/84
The long and short if it is that moving haystack.autodiscover() into your urls.py can sometimes resolve this issue. Setting HAYSTACK_ENABLE_REGISTRATIONS = False when running syncdb or migrate has resolved this for me using this snippet in my settings.py:
# FIXME: This is a complete hack to get around circular imports in
# django-haystack and other apps such as django-endless-pagination
SKIP_COMMANDS = ['syncdb', 'migrate', 'schemamigration', 'datamigration']
if any([command in sys.argv for command in SKIP_COMMANDS]):
HAYSTACK_ENABLE_REGISTRATIONS = False
search_indexes.py doesn't get processed unless haystack is in INSTALLED_APPS. The problem is with the import of MultilingualModel in general. Either it's not truly installed in your environment (attempt to import it from a vanilla python shell), or you have the import wrong (it's actually in another module, for example).
Once you can successfully import MultilingualModel from a python shell, you won't have any problems.

Configuration error in django

I am using django with mod_python (I know it is deprecated -- I am just doing this for an exercise), and am getting the following error --
"Could not import project.views. Error was: No module named app.models"
I am getting the 'No module named app.models" in several other places as well. My syncdb is working fine, and if I go into the manage.py shell I can import the models fine. Why is this occurring and what do I need to change? Thank you.
You should use absolute imports everywhere. If your project is structured like so:
/project/settings.py
/project/app/models.py
/project/app/views.py
In INSTALLED_APPS you would use project.app. In app you'd import your models into views: import project.app.models, etc. Alternately you can try adjusting your PYTHONPATH so your imports work. When you run ./manage.py you are in your project folder, and Python automatically adds it to the PYTHONPATH. This doesn't happen automatically in most deployment scenarios (mod_python or other wise).