我已经通过pip install pipenv在vs代码中为django项目安装了虚拟环境。虽然我已经成功安装了虚拟环境和django(3.0.5),但是当我尝试安装python manage.py runserver它时却抛出了一个错误。它只是说它找不到任何名为django的模块(但是当我键入django-admin --version它时,它表明我已经安装了django,它的版本是3.0.5)。在我的cmd中,我收到以下消息:
python manage.py runserver
django-admin --version
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.) locate to the directory, lets say you are here C:\desktop\projects
2.) now use below commands
pipenv install django==3.0.1
then start the virtual enviorment
pipenv shell
create project
django-admin startproject newproject . (don’t avoid this period)
django-admin startproject newproject .
run server