导入数据时报错, ModuleNotFoundError: No module named 'Mxshop.settings'

开发环境:win10+python3.5+django1.11
项目目录结构如下:
图片描述
图片描述

报错信息:

C:UsersAdministratorEnvsmxonlineScriptspython.exe D:/code/Mxshop/db_tools/import_category_data.py
Traceback (most recent call last):
File "D:/code/Mxshop/db_tools/import_category_data.py", line 14, in <module>

django.setup()

File "C:UsersAdministratorEnvsmxonlinelibsite-packagesdjango__init__.py", line 19, in setup

configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)

File "C:UsersAdministratorEnvsmxonlinelibsite-packagesdjangoconf__init__.py", line 56, in getattr

self._setup(name)

File "C:UsersAdministratorEnvsmxonlinelibsite-packagesdjangoconf__init__.py", line 43, in _setup

self._wrapped = Settings(settings_module)

File "C:UsersAdministratorEnvsmxonlinelibsite-packagesdjangoconf__init__.py", line 106, in init

mod = importlib.import_module(self.SETTINGS_MODULE)

File "C:UsersAdministratorEnvsmxonlinelibimportlib__init__.py", line 126, in import_module

return _bootstrap._gcd_import(name[level:], package, level)

File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'MxShop'
manage.py

!/usr/bin/env python

import os
import sys

if name == "__main__":

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Mxshop.settings")
try:
    from django.core.management import execute_from_command_line
except ImportError as exc:
    raise ImportError(
        "Couldn't import Django. Are you sure it's installed and "
        "available on your PYTHONPATH environment variable? Did you "
        "forget to activate a virtual environment?"
    ) from exc
execute_from_command_line(sys.argv)

import_category_data.py

-- coding: utf-8 --

独立使用django的model

import sys
import os

pwd = os.path.dirname(os.path.realpath(__file__))
sys.path.append(pwd+"../")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MxShop.settings")

import django
django.setup()

from goods.models import GoodsCategory

from db_tools.data.category_data import row_data

for lev1_cat in row_data:

lev1_intance = GoodsCategory()
lev1_intance.code = lev1_cat["code"]
lev1_intance.name = lev1_cat["name"]
lev1_intance.category_type = 1
lev1_intance.save()

for lev2_cat in lev1_cat["sub_categorys"]:
    lev2_intance = GoodsCategory()
    lev2_intance.code = lev2_cat["code"]
    lev2_intance.name = lev2_cat["name"]
    lev2_intance.category_type = 2
    lev2_intance.parent_category = lev1_intance
    lev2_intance.save()

    for lev3_cat in lev2_cat["sub_categorys"]:
        lev3_intance = GoodsCategory()
        lev3_intance.code = lev3_cat["code"]
        lev3_intance.name = lev3_cat["name"]
        lev3_intance.category_type = 3
        lev3_intance.parent_category = lev2_intance
        lev3_intance.save()


阅读 4k
1 个回答

你看下你的目录结构,是不是文件夹Mxshop文件夹下没有__init__.py?
如果没有,创建一个空白的 __init__.py文件

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题