1

I dont quite understand how tasks work. I ran the example from the docs on the rq site and it ran just as it was supposed to. So then I tried to use a function that I created. So i imported it like this

my task.py

import os

import redis
from rq import Worker, Queue, Connection
from src.blog.my_scraps import panties

import requests

listen = ['high', 'default', 'low']

redis_url = os.getenv('REDISTOGO_URL', 'redis://localhost:6379')

conn = redis.from_url(redis_url)

if __name__ == '__main__':
    with Connection(conn):
        worker = Worker(map(Queue, listen))
        worker.work()


panties()

and what I got was this

(practice) apples-MBP:practice ray$ python my_raddqueue.py
Traceback (most recent call last):
  File "my_raddqueue.py", line 1, in <module>
    from my_task import conn, is_page_ok #count_words_at_url
  File "/Users/ray/Desktop/myheroku/practice/my_task.py", line 5, in <module>
    from src.blog.my_scraps import panties
  File "/Users/ray/Desktop/myheroku/practice/src/blog/my_scraps.py", line 3, in <module>
    from .models import Post
  File "/Users/ray/Desktop/myheroku/practice/src/blog/models.py", line 3, in <module>
    from taggit.managers import TaggableManager
  File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/taggit/managers.py", line 7, in <module>
    from django.contrib.contenttypes.models import ContentType
  File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/contrib/contenttypes/models.py", line 159, in <module>
    class ContentType(models.Model):
  File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/contrib/contenttypes/models.py", line 160, in ContentType
    app_label = models.CharField(max_length=100)
  File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1072, in __init__
    super(CharField, self).__init__(*args, **kwargs)
  File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 166, in __init__
    self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
  File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/conf/__init__.py", line 55, in __getattr__
    self._setup(name)
  File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

I am new to django and even newer to python, and I just started working with rq yesterday because I want to automate my site. I see that it says

You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

but I'm not clear on exactly what to do. Any help would be appreciated. thanks.

EDIT

this is the function I'm trying to use

 def p_panties():

        def swappo():
            user_one = ' "Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0" '
            user_two = ' "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5)" '
            user_thr = ' "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko" '
            user_for = ' "Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:10.0) Gecko/20100101 Firefox/10.0" '

            agent_list = [user_one, user_two, user_thr, user_for]
            a = random.choice(agent_list)
            return a

        headers = {
            "user-agent": swappo(),
            "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
            "accept-charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
            "accept-encoding": "gzip,deflate,sdch",
            "accept-language": "en-US,en;q=0.8",
        }

        from lxml import html
        pan_url = 'http://www.panvideos.com'
        shtml = requests.get(pan_url, headers=headers)
        soup = BeautifulSoup(shtml.text, 'html5lib')
        video_row = soup.find_all('div', {'class': 'video'})
        name = 'pan videos'

        if os.getenv('_system_name') == 'OSX':
            author = User.objects.get(id=2)
        else:
            author = User.objects.get(id=3)

        def youtube_link(url):
            youtube_page = requests.get(url, headers=headers)
            soupdata = BeautifulSoup(youtube_page.text, 'html5lib')
            video_row = soupdata.find_all('script', {'type': 'text/javascript'})
            entries = [{'text': str(div),
                        } for div in video_row]
            tubby = str(entries[4])
            urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', tubby)
            return urls

        def embed(url):
            new_embed = url.replace("watch?v=", "embed/")
            return new_embed

        entries = [{'href': div.a.get('href'),
                    'src': youtube_link(div.a.get('href'))[1],
                    'text': div.h4.text,
                    'comments': div.h4.text,
                    'name': name,
                    'url': div.a.get('href'),
                    'embed': embed(youtube_link(div.a.get('href'))[0]),
                    'author': author,
                    'video': True,
                    } for div in video_row][:13]

        for entry in entries:
            post = Post()
            post.title = entry['text']
            title = post.title
            if not Post.objects.filter(title=title):
                post.title = entry['text']
                post.name = entry['name']
                post.url = entry['url']
                post.body = entry['comments']
                post.image_url = entry['src']
                post.video_path = entry['embed']
                post.author = entry['author']
                post.video = entry['video']
                post.status = 'draft'
                post.save()
                post.tags.add("video", "Musica")

        return entries

EDIT

I tried

export DJANGO_SETTINGS_MODULE=gettingstarted.settings

and got the following traceback

Traceback (most recent call last):
      File "my_raddqueue.py", line 1, in <module>
        from my_task import conn, is_page_ok #count_words_at_url
      File "/Users/ray/Desktop/myheroku/practice/my_task.py", line 6, in <module>
        from src.blog.my_scraps import panties
      File "/Users/ray/Desktop/myheroku/practice/src/blog/my_scraps.py", line 3, in <module>
        from .models import Post
      File "/Users/ray/Desktop/myheroku/practice/src/blog/models.py", line 3, in <module>
        from taggit.managers import TaggableManager
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/taggit/managers.py", line 7, in <module>
        from django.contrib.contenttypes.models import ContentType
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/contrib/contenttypes/models.py", line 159, in <module>
        class ContentType(models.Model):
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/contrib/contenttypes/models.py", line 160, in ContentType
        app_label = models.CharField(max_length=100)
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1072, in __init__
        super(CharField, self).__init__(*args, **kwargs)
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 166, in __init__
        self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/conf/__init__.py", line 55, in __getattr__
        self._setup(name)
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/conf/__init__.py", line 43, in _setup
        self._wrapped = Settings(settings_module)
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/conf/__init__.py", line 99, in __init__
        mod = importlib.import_module(self.SETTINGS_MODULE)
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/importlib/__init__.py", line 126, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
    ImportError: No module named 'gettingstarted'

mysettings are in a settings directory

 gettingstarted\
     __init__.py
     base.py
     local.py
     production.py

I also tried

DJANGO_SETTINGS_MODULE="gettingstarted.settings.base"

and I also tried

export DJANGO_SETTINGS_MODULE="src.gettingstarted.settings.base"

and got got the message

        Traceback (most recent call last):
      File "my_raddqueue.py", line 1, in <module>
        from my_task import conn, is_page_ok #count_words_at_url
      File "/Users/ray/Desktop/myheroku/practice/my_task.py", line 6, in <module>
        from src.blog.my_scraps import panties
      File "/Users/ray/Desktop/myheroku/practice/src/blog/my_scraps.py", line 3, in <module>
        from .models import Post
      File "/Users/ray/Desktop/myheroku/practice/src/blog/models.py", line 3, in <module>
        from taggit.managers import TaggableManager
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/taggit/managers.py", line 7, in <module>
        from django.contrib.contenttypes.models import ContentType
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/contrib/contenttypes/models.py", line 159, in <module>
        class ContentType(models.Model):
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/models/base.py", line 94, in __new__
        app_config = apps.get_containing_app_config(module)
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/apps/registry.py", line 239, in get_containing_app_config
        self.check_apps_ready()
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/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.

EDIT

this is my wsgi.py

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "gettingstarted.settings")

from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise

application = get_wsgi_application()
application = DjangoWhiteNoise(application)

this is a snippet of my settings/base.py

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # local
    'blog',

    # Third party
    'crispy_forms',
    'taggit',
    'haystack',
    'whoosh',
    'pagedown',
    'markdown_deux'
)

EDIT

tried adding this to my my_task.py

import django
from django.conf import settings
from src.gettingstarted.settings import base

settings.configure(default_settings=base, DEBUG=True)
django.setup()

and I'm getting a new error message

File "my_raddqueue.py", line 1, in <module>
        from my_task import conn, count_words_at_url, lett, p_panties # is_page_ok
      File "/Users/ray/Desktop/myheroku/practice/my_task.py", line 13, in <module>
        django.setup()
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/__init__.py", line 17, in setup
        configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/conf/__init__.py", line 56, in __getattr__
        return getattr(self._wrapped, name)
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/conf/__init__.py", line 173, in __getattr__
        return getattr(self.default_settings, name)
    AttributeError: module 'src.gettingstarted.settings.base' has no attribute 'LOGGING_CONFIG'

EDIT

I added

LOGGING_CONFIG = None

to my base.py because of this message

module 'src.gettingstarted.settings.base' has no attribute 'LOGGING_CONFIG'

then when I added that I got a new message

module 'src.gettingstarted.settings.base' has no attribute 'LOGGING'

so then I added

LOGGING = None

then I got this message

 No module named 'blog'

I'm trying another approach at this point because this is getting annoying. I think the problem is occuring because my function scrapes and and adds data to my models which I guess needs authentication.

these two imports

from django.contrib.auth.models import User
from src.blog.models import Post

are causing the traceback message

        File "my_raddqueue.py", line 1, in <module>
        from my_task import conn, count_words_at_url, lett # is_page_ok
      File "/Users/ray/Desktop/myheroku/practice/my_task.py", line 10, in <module>
        from django.contrib.auth.models import User
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/contrib/auth/models.py", line 4, in <module>
        from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
        class AbstractBaseUser(models.Model):
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/contrib/auth/base_user.py", line 50, in AbstractBaseUser
        password = models.CharField(_('password'), max_length=128)
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1072, in __init__
        super(CharField, self).__init__(*args, **kwargs)
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 166, in __init__
        self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/conf/__init__.py", line 55, in __getattr__
        self._setup(name)
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/conf/__init__.py", line 43, in _setup
        self._wrapped = Settings(settings_module)
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/conf/__init__.py", line 99, in __init__
        mod = importlib.import_module(self.SETTINGS_MODULE)
      File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/importlib/__init__.py", line 126, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
    ImportError: No module named 'gettingstarted'
losee
  • 2,190
  • 3
  • 29
  • 55
  • Have a look [here](http://stackoverflow.com/a/26089302/2808371) and [here](https://docs.djangoproject.com/en/1.9/topics/settings/#designating-the-settings). Hopefuly this will help. – an0o0nym Aug 06 '16 at 15:38
  • @an0o0nym Hey since you've posted your answer I have been trying what you suggested and variations of it. Take a look above at the traceback – losee Aug 06 '16 at 18:08
  • The most common problem which can be causing that is that you might have an app in your `INSTALLED_APPS` (`settings.py`) that does not exist. So you may just want to check and update or delete if you find any incorrect apps in there. Also look for mising colons. – an0o0nym Aug 06 '16 at 18:58
  • @an0o0nym I added a snippet of my settings. the installed apps, above. DoYou see what might be causing this in my function? why would I have to do this extra step if my job is just essentially a function? – losee Aug 06 '16 at 19:27
  • See [this](http://stackoverflow.com/a/26215548/2808371)! [Here](https://docs.djangoproject.com/en/1.8/topics/settings/#calling-django-setup-is-required-for-standalone-django-usage) is some more explanation. – an0o0nym Aug 06 '16 at 19:58
  • @an0o0nym You are giving me the tools but I do not know how to use them. A lot of these terms I am not familiar with because the tutorial doesn't mention them. Thanks for trying to help me though – losee Aug 06 '16 at 20:16
  • @an0o0nym see above. I tried what you suggested, not sure if it's right though. I tried it with my local.py, base and production and got the new traceback stated above – losee Aug 06 '16 at 20:32

0 Answers0