【问题标题】:ModuleNotFoundError: No module named 'django.core'ModuleNotFoundError:没有名为 \'django.core\' 的模块
【发布时间】:2023-01-20 19:14:36
【问题描述】:

我想创建 django 项目,所以我配置了 virtualenv ,并且在使用此命令 python3 manage.py startapp Accounts 创建应用程序时安装了 django pipenv install django==4.0.1 我收到了这个错误。

(env) zakaria@ZAKARIA:/mnt/c/Users/ZAKARIA/Desktop/project$ python manage.py startapp Accounts
Traceback (most recent call last):
  File "manage.py", line 11, in main
    from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django.core'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 13, in main
    raise ImportError(
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?

任何人都可以帮助解决这个问题吗?

【问题讨论】:

  • 不是pip install django=4.0.1吗?您可以使用pip list 来检查已安装的包
  • 它安装在我的包中
  • @vinkomlacic 要使用 pip 安装特定版本,您需要使用双等号。不过,我不确定 pipenv(以前从未使用过)。
  • @ChaddRobertson 是的,这是一个错字。编辑: pip install django==4.0.1
  • @vinkomlacic 我总是以相同的方式使用 pipenv 并且一直在工作我不知道发生了什么

标签: python django


【解决方案1】:

而不是 python3 manage.py startapp Accounts 试试 python manage.py startapp Accounts 激活你的 venv

为了解释为什么这很重要,让我们做一个练习。从没有激活 venv 开始,尝试这个过程(如果你在 venv 中,你可能需要使用 deactivate 命令关闭:

python -m venv my_venv
# The following line assumes you're on Linux or Mac; it appears you're using WSL-2, which is fine
. my_venv/bin/activate
# The following command should show the path to the Python binary in your venv
which python
# The following command may show that you're not hitting the Python version in your venv, but somewhere else
which python3

您想确保您使用的是 venv 中的 Python 二进制文件。祝你好运!

【讨论】:

  • 我解决了这个问题,删除这个项目并重新启动我的电脑并创建新项目它的工作,我不知道发生了什么
【解决方案2】:

当我将 Django 与 Docker 连接时,我遇到了同样的问题。我通过运行以下命令解决了这个问题:

docker-compose create --build

【讨论】:

    猜你喜欢
    • 2010-09-23
    • 2014-12-25
    • 2019-03-28
    • 2022-01-07
    • 2017-12-14
    • 2020-12-10
    • 2021-09-02
    • 2020-10-10
    相关资源
    最近更新 更多