[Answered ]-Django ImportError no module named

1πŸ‘

βœ…

You need to add the init.py in dev folder. Because it treated as a python package. Next you need to add your django project path in fillDB.py like this,

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.

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

πŸ‘€dhana

1πŸ‘

Check you sys.path to see whether there have you path or not. Every time you run your python project, python will append you current path to the sys.path. And once you quit the python enviroment, python will remove the path you appended in.

Your problem is you run just run fillDB.py, python just append β€˜../computerbuilder/dev’ into sys.path, so python can not find builds module.

The solution is move your fillDB.py file to the same level as builds folder

β”œβ”€β”€ 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 πŸ˜€

πŸ‘€Carlos Lin

Leave a comment