Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 622103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:56:53+00:00 2026-05-13T18:56:53+00:00

I am trying to have a Django script run every 5 minutes via cron

  • 0

I am trying to have a Django script run every 5 minutes via cron on my dev laptop (Mac OS X). Here is the code in the script:

import sys
import os

def setup_environment():
    pathname = os.path.dirname(sys.argv[0])
    sys.path.append(os.path.abspath(pathname))
    sys.path.append(os.path.normpath(os.path.join(os.path.abspath(pathname), '../')))
    os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

setup_environment()


from common.models import TweetCache
import datetime

def main():
    print "(%s) Caching tweets..." % str(datetime.datetime.now()) 
    previous_tweets = TweetCache.objects.filter(username='atmospherian')
    for prev in previous_tweets:
        prev.delete()

    import twitter

    api = twitter.Api()
    tweets = api.GetUserTimeline('atmospherian')
    for t in tweets:
        tc = TweetCache(username='atmospherian', date=t.created_at, text=t.text)
        tc.save()

if __name__ == '__main__':
    main()

crontab:

*/5 * * * * python /absolute/path/to/tweet_cache.py

error from system mail:

X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=jason>
X-Cron-Env: <USER=jason>
X-Cron-Env: <HOME=/Users/jason>
Date: Tue, 16 Feb 2010 17:45:00 -0500 (EST)

Traceback (most recent call last):
  File "/Users/jason/Development/bandistry/tweet_cache.py", line 22, in <module>
    from common.models import TweetCache
  File "/Users/jason/Development/bandistry/common/models.py", line 1, in <module>
    from django.db import models
ImportError: No module named django.db

can anyone tell me what im doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:56:54+00:00Added an answer on May 13, 2026 at 6:56 pm

    sys.argv[0] isn’t always the most reliable way to get the current file’s path. I recommend this modification:

    pathname = os.path.dirname(os.path.abspath(__file__))
    sys.path.insert(0, pathname)
    sys.path.insert(0, os.path.abspath(os.path.join(pathname, '..')))
    

    Note the use of sys.path.insert instead of sys.path.append, and the use of file. Also, using abspath on file -before- dirname reduces the chance you reduce the entire filename down to the empty string or simply ‘.’ which may not even be accurate.

    Also, is the django package installed in one of those two paths you add? If not, you sill need to add that path

    Finally, a minor quip, probably unrelated to your django import issue, but you really should be doing:

    os.environ['DJANGO_SETTINGS_MODULE'] = 'bandistry.settings'
    

    It may work without, but it’s better if you put your entire django application in a package. This reduces the chance of you obscuring other package names.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get Django's manage.py to run with modified settings files. I have
I am trying to run a Django management command from cron. I am using
hey guys, im trying to run a script in the backend of my django
I was trying to setup django . I do have Django-1.1-alpha-1. I was trying
I have a Django app and I am trying to perform transactions over multiple
I have a query that I'm trying to figure the django way of doing
I have a small app I'm working on where I'm trying to use Django's
I'm trying to install django-extensions + graphviz + pygraph but i can't. I have
I'm trying to integrate TinyMCE or CKEditor into Django, but I have no idea
I'm trying to use iexact in my Django app. I have items in my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.