我一直在谷歌上搜索這個整天和整個的解決方案還沒有來,希望你能幫助:Django的導入錯誤沒有模塊名爲
使用Django 1.6,我做了使用應用「構建」作爲項目的一部分稱爲「電腦製造商」。
該網站正常工作時,我用的是測試服務器,但我創建了一個文件來填充一些項目數據庫,它給我的錯誤,當我運行python fillDB.py
:
Traceback (most recent call last):
File "fillDB.py", line 1, in <module>
from builds.models import BuildsTable
ImportError: No module named builds.models
這是我的文件fillDB.py:
from builds.models import BuildsTable
moboDB = open("db.txt", "r")
lines = moboDB.read().split('\",')
print lines
def main():
global lines
global BuildsTable
for item in lines:
mobo = BuildsTable.objects.get(moboListing="%s" % item[0])
price_local = BuildsTable.objects.get(moboListing="%s" % item[1])
if(BuildsTable.objects.filter(
moboListing = mobo, price = price_local).exists() == False):
mydb = BuildsTable(moboListing = mobo, price = price_local)
mydb.save()
main()
這是我的models.py文件從 「構建」 應用程序,我提出:
from django.db import models
# Create your models here.
class BuildsTable(models.Model):
id = models.AutoField(primary_key=True)
moboListing = models.CharField(max_length=200)
price = models.IntegerField()
我的目錄看起來是這樣的:
├── builds
│ ├── admin.py
│ ├── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── computerbuilder
│ ├── dev
│ │ ├── db.txt
│ │ ├── fillDB.py
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── views.py
│ ├── wsgi.py
├── manage.py
└── requirements.txt
由於我使用一個外部文件不Django的一部分,我認爲這就是爲什麼它有麻煩認識它。如果有幫助,也使用postgres。
感謝,所以它說'sys.path。append('/ path/Root')'我必須將其更改爲我的項目頂層目錄的路徑? – achabacha322
是的,你是對的。 – dhana
謝謝,這工作!但現在我的代碼有另一個問題,將嘗試修復它,謝謝你的幫助 – achabacha322