ModuleNotFoundError: No module named 'django' problem in vs code

ModuleNotFoundError: No module named 'django' problem in vs code

我已经通过 pip install pipenv 在 vs code 中为我的 django 项目安装了虚拟环境。虽然我已经成功安装了虚拟环境和 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?

试试这个

问题是您尝试创建虚拟环境的地方不包含 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