简体   繁体   English

Django 错误“ModuleNotFoundError:没有名为“ocore”的模块

[英]Django error 'ModuleNotFoundError: No module named 'ocore'

New to Django and have just started building out a new project following a tutorial. Django 新手,刚刚开始按照教程构建一个新项目。

I have created a virtual environment as follows:我创建了一个虚拟环境,如下所示:

pip install virtualenv

virtualenv myproject_3_7_3

I then activate the virtual env and install Django as follows:然后我激活虚拟环境并安装 Django 如下:

cd myproject_3_7_3

source bin/activate

pip install django

This installs Django version 3.2.10 successfully, so then I activate the project, add some folder structure and create an app using the following commands:这会成功安装 Django 版本 3.2.10,然后我激活项目,添加一些文件夹结构并使用以下命令创建应用程序:

django-admin startproject myproject

cd myproject

mkdir apps

mkdir apps/core

python manage.py startapp core apps/core

I then open VSCode and I can see the project and all of the default folders/files there.然后我打开 VSCode,我可以在那里看到项目和所有默认文件夹/文件。

To start the app I run:要启动我运行的应用程序:

python manage.py startapp core apps/core

But I immediately get the following error in the terminal.但是我立即在终端中收到以下错误。

Traceback (most recent call last):
  File "/Users/myuser/myproject_3_7_3/lib/python3.7/site-packages/django/apps/config.py", line 244, in create
    app_module = import_module(app_name)
  File "//anaconda3/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'ocore'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/Users/myuser/myproject_3_7_3/lib/python3.7/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/Users/myuser/myproject_3_7_3/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
    django.setup()
  File "/Users/myuser/myproject_3_7_3/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/myuser/myproject_3_7_3/lib/python3.7/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "/Users/myuser/myproject_3_7_3/lib/python3.7/site-packages/django/apps/config.py", line 250, in create
    app_config_class.__qualname__,
django.core.exceptions.ImproperlyConfigured: Cannot import 'ocore'. Check that 'apps.core.apps.OcoreConfig.name' is correct.

Exploring the apps.py which gets created shows the following contents:探索创建的apps.py显示以下内容:

from django.apps import AppConfig


class OcoreConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'ocore'

This differs from the apps.py file given in the tutorial so my initial thought was that it might be due to a version difference, however searching this extensively yields no information about what ocore is or what this error means.这与教程中给出的apps.py文件不同,所以我最初的想法是这可能是由于版本差异,但是广泛搜索不会产生关于ocore是什么或这个错误意味着什么的信息。

Is anyone able to help me resolve this issue?有人能帮我解决这个问题吗?

Spelling mistake拼写错误

I tried to replicate your error.我试图复制你的错误。

While creating the app you mistakenly created with name "ocore".在创建应用程序时,您错误地创建了名称为“ocore”的应用程序。

But in settings.py但是在 settings.py

INSTALLED_APPS = [
   ...
   apps.core, # This is what you added I presume
   apps.ocore, # this must be done
]

The created app name should be same in INSTALLED_APPS array present in settings.py of django.创建的应用程序名称应与 django 的 settings.py 中的 INSTALLED_APPS 数组相同。

One more thing "startapp" only just create the directory structure for you with required files and configuration.另一件事“startapp”只是为您创建具有所需文件和配置的目录结构。 It will not run the project for that you have to do python manage.py runserver它不会运行您必须执行的项目python manage.py runserver

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

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