2017-04-10 34 views
0

我有以下項目設置。Django:ImportError:沒有名爲'services'的模塊

myproject/ 
    manage.py 
    requirement.txt 
    myproject/ 
     __init__.py 
     settings.py 
     urls.py 
    services/ 
     __init__.py 
     models.py 
     views.py 
     urls.py 
     management/ 
      __init__.py 
      commands/ 
      __init__.py 
      myscript.py 

myscript.py文件我想從services應用程序導入模型。我添加了以下行。

from services.models import TwitterRawFeeds, TwitterUserDetails, MarkedTweets 

但我得到錯誤ImportError: No module named 'services'

我試過相對進口以及..

from .models import TwitterRawFeeds, TwitterUserDetails, MarkedTweets 

這個時候,我得到這樣Parent module '' not loaded, cannot perform relative import

錯誤我是缺少在這裏,需要幫幫我。

安裝的程序部分..

INSTALLED_APPS = (
'django.contrib.admin', 
'django.contrib.auth', 
'django.contrib.contenttypes', 
'django.contrib.sessions', 
'django.contrib.messages', 
'django.contrib.staticfiles', 
'mongoengine.django.mongo_auth', 
'rest_framework', 
'rest_framework_mongoengine', 
'services', 
) 
+0

是'services'在'INSTALLED_APPS'? –

+0

是的。它在那裏。 – Shri

回答

3

__init__.py的存在使它成爲一個python模塊,因此將__init__.py添加到management目錄中,它應該可以工作。

運行與python manage.py myscriptmyproject文件夾中的命令,它應該工作,顯然使用的是python myscript.py來運行它,在這種情況下,mysript.py不會讀settings.py,沒有辦法你的腳本將加載services應用。在另一方面負荷運行myscript.pypython manage.py myscript所有設置和它的作品:)

myproject/ 
    manage.py 
    requirement.txt 
    myproject/ 
     __init__.py 
     settings.py 
     urls.py 
    services/ 
     __init__.py 
     models.py 
     views.py 
     urls.py 
     management/ 
      __init__.py # add this file 
      commands/ 
      __init__.py 
      myscript.py 
+0

已添加。仍然有相同的錯誤。 – Shri

+0

你可以發佈設置的INSTALLED_APPS部分嗎? – rrmerugu

+0

你用什麼版本的Django? 'mongoengine'不支持最新版本的django。我相信它只支持1.8?但'django-mongoengine'可能支持最新的一個 – rrmerugu

2

錯失內management/目錄的__init__.py?如你所知management/不是一個Python模塊。這只是一個普通的目錄。在裏面放一個__init__.py

+0

已添加。仍然有相同的錯誤。 – Shri

+0

怎麼樣從...模型導入TwitterRawFeeds,TwitterUserDetails,MarkedTweets'? –

+0

然後我得到'SystemError:父模塊''未加載,無法執行相對導入' – Shri