简体   繁体   English

Django ImportError没有命名的模块

[英]Django ImportError no module named

I've been googling this all day and have not come across a solution, hope you can help: 我整天都在搜寻Google,并且没有找到解决方案,希望您能提供帮助:

Using Django 1.6, I made an app called "builds" as part of a project called "computerbuilder". 使用Django 1.6,我将一个名为“ builds”的应用程序作为一个名为“ computerbuilder”的项目的一部分。

The site works fine when I use the test server, however I created a file to populate the database with some items, and it's giving me the error when I run python fillDB.py : 当我使用测试服务器时,该站点工作正常,但是我创建了一个文件来填充一些项目,这在运行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

This is my file fillDB.py: 这是我的文件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()

This is my models.py file from the "builds" app I made: 这是我制作的“ builds”应用程序中的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()

My directory looks like this: 我的目录如下所示:

├── 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

Since I'm using an external file not part of Django, I think that's why its having trouble recognizing it. 由于我使用的外部文件不是Django的一部分,因此我认为这就是无法识别它的原因。 Also using postgres if that helps. 如果有帮助,也可以使用postgres。

Check you sys.path to see whether there have you path or not. 检查您的sys.path以查看是否有您的路径。 Every time you run your python project, python will append you current path to the sys.path. 每次运行python项目时,python都会将当前路径附加到sys.path。 And once you quit the python enviroment, python will remove the path you appended in. 并且一旦退出python环境,python就会删除您添加的路径。

Your problem is you run just run fillDB.py, python just append '../computerbuilder/dev' into sys.path, so python can not find builds module. 您的问题是您只运行fillDB.py,而python仅将'../computerbuilder/dev'附加到sys.path中,因此python无法找到builds模块。

The solution is move your fillDB.py file to the same level as builds folder 解决方案是将fillDB.py文件移动到与builds文件夹相同的级别

├── builds    
├── fillDB.py
│   ├── admin.py
│   ├── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── computerbuilder
│   ├── dev
│   │   ├── db.txt
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── views.py
│   ├── wsgi.py
├── manage.py
└── requirements.txt

Hope it can help you :D 希望它可以帮助您:D

You need to add the init .py in dev folder. 您需要在dev文件夹中添加init .py。 Because it treated as a python package. 因为它被视为python包。 Next you need to add your django project path in fillDB.py like this, 接下来,您需要像这样在fillDB.py中添加django项目路径,

Root
 ├──├── 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

Follow the above folder structure, And also you need to set the django environment variable to this file. 遵循上述文件夹结构,并且您还需要将django环境变量设置为此文件。

fillDB.py fillDB.py

import sys
import os


if __name__ == "__main__":
    sys.path.append('/path/Root')
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "computerbuilder.settings")

    from builds.models import BuildsTable

    mobo = BuildsTable.objects.all()
    print mobo

Hope this help you 希望这对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM