【问题标题】:ModuleNotFoundError: No module named 'django' problem in vs codeModuleNotFoundError:vs 代码中没有名为“django”的模块问题
【发布时间】:2020-04-23 06:47:43
【问题描述】:

我已经通过 pip install pipenv 为我的 django 项目在 vs 代码中安装了虚拟环境。虽然我已经成功安装了虚拟环境和 django(3.0.5) ,但是当我尝试 python manage.py runserver 时它会抛出一个错误。它只是说它找不到任何名为 django 的模块(但是当我输入 django-admin --version 时,它显示我安装了 django,它的版本是 3.0.5)。 在我的 cmd 中,我收到以下消息:


    C:\Users\Ahnaaf Al Rafee\POLLSTAR_PROJECT>"C:/Users/Ahnaaf Al Rafee/.virtualenvs/POLLSTAR_PROJECT-oW8GrevN/Scripts/activate.bat"

(POLLSTAR_PROJECT) C:\Users\Ahnaaf Al Rafee\POLLSTAR_PROJECT>python manage.py runserver
python: can't open file 'manage.py': [Errno 2] No such file or directory

(POLLSTAR_PROJECT) C:\Users\Ahnaaf Al Rafee\POLLSTAR_PROJECT>cd pollstar

(POLLSTAR_PROJECT) C:\Users\Ahnaaf Al Rafee\POLLSTAR_PROJECT\pollstar>python manage.py runserver
Traceback (most recent call last):
  File "manage.py", line 10, in main
    from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'

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

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

(POLLSTAR_PROJECT) C:\Users\Ahnaaf Al Rafee\POLLSTAR_PROJECT\pollstar>python manage.py runserver 8081
Traceback (most recent call last):
  File "manage.py", line 10, in main
    from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'


(POLLSTAR_PROJECT) C:\Users\Ahnaaf Al Rafee\POLLSTAR_PROJECT\pollstar>django-admin --version
3.0.5

(POLLSTAR_PROJECT) C:\Users\Ahnaaf Al Rafee\POLLSTAR_PROJECT\pollstar>python manage.py runserver
Traceback (most recent call last):
  File "manage.py", line 10, in main
    from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'

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

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 12, 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 freeze。并检查您的环境中是否安装了 django
  • 我认为你已经在你的 so 中安装了 django,但没有在你的 venv 中。

标签: python django importerror


【解决方案1】:

试试这个

问题是您尝试创建虚拟环境的位置不包含 manage.py 文件意味着您的 manage.py 不存在于您的项目文件夹中,这就是您收到此错误的原因。在下面修复

在创建项目和运行虚拟环境时始终使用此模式

1.) 定位到目录,假设你在这里 C:\desktop\projects

2.) 现在使用以下命令

pipenv install django==3.0.1

然后启动虚拟环境

pipenv shell

创建项目

django-admin startproject newproject .(不要避开这段时间)

运行服务器

python manage.py runserver

【讨论】:

    猜你喜欢
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 2020-03-20
    • 1970-01-01
    • 2022-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多