2014-09-02 43 views
2

我有一個簡單的python文件,我最終希望從chron運行。如何從命令行運行django python文件

它看起來像這樣:

from customers.models import Customer, NotificationLogEntry 


def hello(): 
    customers = Customer.objects.all() 
    for c in customers: 
     log_entry = NotificationLogEntry(
              customer=c, 
              sent=True, 
              notification_type=0, 
              notification_media_type=0, 
             ) 
     log_entry.save() 


if __name__ == '__main__': 
    hello() 

當我跑這跟:

python notifications.py 

我得到一個導入錯誤:

Traceback (most recent call last): 
    File "notifications.py", line 1, in <module> 
    from customers.models import Customer, NotificationLogEntry 
ImportError: No module named customers.models 

該模塊存在,我把它叫做無我的django應用程序中的任何問題。爲什麼我在我的應用程序之外獲得這個錯誤?

回答

1

需要設置up項目路徑

import os 

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project_name.settings") 

# your imports, e.g. Django models 
from customers.models import Customer, NotificationLogEntry