2012-05-06 67 views
0

我創建了一個簡單的模塊來存儲我所有的選擇,在我的應用程序中重用他們命名爲「模塊名」怪沒模塊(見CharField選擇)Django的/ Python的

這裏我的項目結構

project_name/ 
    -- manage.py 
    -- settings.py 
    -- ...... 
    -- apps/ 
     -- __init__.py 
     -- simple_app/ 
      -- __init__.py 
      -- models.py 
      -- .... 
    -- common/ 
     -- __init__.py 
     -- choices/ 
      -- __init__.py 

在我的common.choices.__init__.py我有

from django.utils.translation import ugettext as _ 

SCALE_TYPE_CHOICES = (
    (1,_('Lineare')), 
    (2,_('Logaritmica')), 
    (3,_('Esponenziale')) 
) 

GENDER_CHOICES = (
    ('male',_('Maschio')), 
    ('female',_('Femmina')), 
    ('any',_('Tutti')), 
) 

和apps.simple.models.py

from common.choices import SCALE_TYPE_CHOICES, GENDER_CHOICES 
..... 

我passenger_wsgi.py

import sys, os 

sys.path.insert(0, os.getcwd()) 
sys.path.insert(0, os.path.join(os.getcwd(),"path_to_my_project")) 
sys.path.insert(0, os.path.join(os.getcwd(),"path_to_my_project/common")) 

os.environ['DJANGO_SETTINGS_MODULE'] = "project_name.settings" 
import django.core.handlers.wsgi 
application = django.core.handlers.wsgi.WSGIHandler() 

在我的dev的服務器,它工作正常,但在我的生產服務器,它拋出和錯誤 「沒有命名的模塊選擇」

manage.py殼

import common 
print common 
<module 'common' from 'absolute_project_path/common/__init__.pyc'> 

passenger_wsgi.py

import sys, os 
sys.path.insert(0, os.getcwd()) 
sys.path.insert(0, os.path.join(os.getcwd(),"path_to_my_project")) 
sys.path.insert(0, os.path.join(os.getcwd(),"path_to_my_project/common")) 

os.environ['DJANGO_SETTINGS_MODULE'] = "project_name.settings" 
import django.core.handlers.wsgi 
application = django.core.handlers.wsgi.WSGIHandler() 
import common 
print common 

Outputs 
<module 'common' from 'absolute_project_path/common/__init__.pyc'> 

有什麼想法?
謝謝!

回答

0

將項目所在的目錄添加到sys.path

+0

你是什麼意思是什麼呢?我的項目是在sys.path和設置導入正確(我認爲是) – Michael

+0

我的意思是我寫的。顯示你做了什麼。 –

+0

我已編輯我的帖子;)謝謝! – Michael

0

檢查以確保sys.path中沒有其他程序包/模塊common

import common 
print common # or common.__file__ 

您是append項目路徑sys.path而不是insert(0, path),從而可引入common在sys.path的W/O選擇將失敗。您可以使用insert來查看錯誤是否消失。從wsgi.py共同並將其添加

+0

我已編輯我的問題,仍然無法正常工作,謝謝! – Michael

+0

@Michael'print common'的輸出是什麼? – okm

+0

我的manage.py shell可以導入common(在添加指定項目路徑的sys.path.insert之後) – Michael

0

撈出來的settings.py代替

 
import os 
filedir = os.path.dirname(__file__) 
sys.path.append(os.path.join(filedir)) 
sys.path.append(os.path.join(filedir, 'common'))