2015-01-15 44 views
0

我知道有很多與此有關的問題,但這些答案似乎不適用於我的情況。我是django的新手(我已經完成了教程),但是我正在修復別人的代碼(我不能再聯繫)。ImportError:沒有名爲lop.models的模塊

我在Linux Derbian上運行django 1.5,使用python 2.7。 我收到此錯誤。

File "views-full.py", line 1, in <module> 
    from lop.models import File, V1, V2 
ImportError: No module named lop.models. 

我views-full.py:

from lop.models import File, V1, V2 
... 

我的樹是這樣的(爲了節省時間,我views-full.py正在LOP):

Main 
├── Main 
│   ├── __init__.py 
│   ├── __init__.pyc 
│   ├── settings.py 
│   ├── settings.pyc 
│   ├── urls.py 
│   ├── urls.pyc 
│   ├── wsgi.py 
│   └── wsgi.pyc 
├── manage.py 
├── lop 
│   ├── admin.py 
│   ├── admin.pyc 
│   ├── forms.py 
│   ├── forms.pyc 
│   ├── __init__.py 
│   ├── __init__.pyc 
│   ├── migrations 
│   │   ├── 0001_migrate.py 
│   │   ├── 0001_migrate.pyc 
│   │   ├── 0002_migrate.py 
│   │   ├── 0002_migrate.pyc 
│   │   ├── 0003_auto__add_category.py 
│   │   ├── 0003_auto__add_category.pyc 
│   │   ├── 0004_auto__add_field_script_category.py 
│   │   ├── 0004_auto__add_field_script_category.pyc 
│   │   ├── __init__.py 
│   │   └── __init__.pyc 
│   ├── models.py 
│   ├── models.pyc 
│   ├── tests.py 
│   ├── urls.py 
│   ├── urls.pyc 
│   ├── views 
│   │   ├── __init__.py 
│   │   ├── __init__.pyc 
│   │   ├── viewsb.py 
│   │   ├── viewsb.pyc 
│   │   └── viewsb.py.save 
│   ├── views-full.py 
│   ├── views.pyc 
│   ├── views.py.save 
│   └── views-test.py 
├── scripts [39 entries exceeds filelimit, not opening dir] 
├── sqlite3.db 
├── static [29 entries exceeds filelimit, not opening dir] 
├── templates 
│   ├── entry2-full.html 
│   ├── entry2.html 
│   ├── entry3-full.html 
│   ├── entry3.html 
│   ├── entry.html 
│   ├── index.html 
│   ├── index.html.old 
│   ├── scriptlist.html 
│   └── testData.html 
└── user-dirs [109 entries exceeds filelimit, not opening dir] 

正如你看,我的初始化 .py和models.py在同一個文件夾(我知道他們沒有在其他情況下存在的問題)。

我的settings.py:

...  
INSTALLED_APPS = (
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.sites', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'django.contrib.admin', 
    # 'django.contrib.admindocs', 
    'lop', 
    'south', 
) 
... 

我覺得我做一些新手的錯誤,但對我的光,我無法弄清楚。這是我在這個網站上的第一個問題(請溫和)。提前致謝。

回答

0

所以我想通了,這是從我的一個錯誤的問題。我認爲要測試任何編譯錯誤(語法),我會在shell上運行「python < filename> migrate」或「python < filename> makemigrations」。我相信所有的變化都需要進行遷移,而不僅僅是數據庫中的變化。當語法錯誤包括這個錯誤時,我會收到錯誤消息,並且在之前的其他情況下可以使用。有人最終告訴我,這不是你如何測試它,運行「python manage.py runserver 0.0.0.0:8000」會指出你的本地機器代碼中的任何錯誤。之後,我沒有收到我的特定文件的錯誤消息。

Gabriel L'Heureux,我仍然感謝你的幫助(我會給你一個提示,但我沒有足夠的積分來這樣做),所以你對此表示感謝。

1

我不熟悉Django的,但這裏是我在那種情況做:

import sys 
sys.path.append('/path/of/folder/where/module/is/') 
from new_module import new_function 
+0

唯一的問題是模塊的路徑可能不是永久的(如果有人被僱用後來下線,目錄將移動到新用戶的位置,路徑將不再有效)。在我的項目中,我定義了一個常量(BASE_DIR)來指定目錄的路徑,所以如果發生這種情況,他們只需要改變常量,以便在任何地方應用更改。但是,我認爲我不能和終端做同樣的事情。 – ChocolateCode 2015-01-15 17:40:31

+0

並且該常量不能用於定義模塊的路徑? ('sys.path.append(BASE_DIR +'/ specific/module/folder /'')我不得不承認我還不知道該怎麼辦,因爲除了已經給出的答案之外,這並沒有發生在我身上! – 2015-01-15 21:43:24

相關問題