2016-12-17 36 views
-1

這是我的Django應用程序的文件夾結構單獨的Python腳本不能找到自定義模塊內的Django

|-----tt 
|  |--settings.py 
| 
|-----warehouse 
|  |--models.py 
|  |--__init__.py 
| 
|-----dashboard 
|  |--email_excel_reports.py 
| 

裏面我email_excel_reports.py

# for ORM query 
from warehouse.models import EntryFormLineItem 
from django.db.models import Sum 
from django.db.models.functions import Coalesce 
from django.utils import timezone 


line_items = EntryFormLineItem.objects.filter(approved_by__isnull=False, ... 

當我運行python email_excel_reports.pydashboard

我得到這個錯誤
Traceback (most recent call last): 
    File "/vagrant/dashboard/email_excel_reports.py", line 11, in <module> 
    from warehouse.models import EntryFormLineItem 
ImportError: No module named 'warehouse' 

熊記住當我運行python email_excel_reports.py時,我用nginx運行gunicorn,所以django應用程序運行成功。

__init__.py內部warehouse

default_app_config = 'warehouse.apps.WarehouseConfig' 

當我運行python manage.py shell然後鍵入相同的代碼行,有運行代碼沒有問題。

我該如何克服錯誤?

重新開放 - 爲什麼?

只是因爲修復是要將python manage.py shell <放置在python腳本文件的前面,並且此修補程序在所謂重複問題的答案中不可用。

+0

有你'warehouse'目錄'__init __。py'文件? – shady

+0

是的,我剛剛將內容添加到問題 –

+0

當您想要在命令行中運行此腳本時,您需要設置django環境。 – shady

回答

0

因爲需要包括Django的模塊,更好的方式來運行,這是

python manage.py shell < dashboard/email_excel_reports.py