账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    导入数据时报错, ModuleNotFoundError: No module named 'Mxshop.settings'
    • 2020-01-01 00:00
    • 11
    26
    0

    开发环境: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()
    
    
    
    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部