Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

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

I have installed virtual environment via pip install pipenv for my django project in vs code. Although I had successfully installed virtual environment and django(3.0.5) , but when I tried to python manage.py runserver then it throw me an error. It simply says that it was unable to find any module called django(but when I typed django-admin --version it shows that I had django installed and it's version is 3.0.5). In my cmd i got the following message:


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

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

(POLLSTAR_PROJECT) C:UsersAhnaaf Al RafeePOLLSTAR_PROJECT>cd pollstar

(POLLSTAR_PROJECT) C:UsersAhnaaf Al RafeePOLLSTAR_PROJECTpollstar>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:UsersAhnaaf Al RafeePOLLSTAR_PROJECTpollstar>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:UsersAhnaaf Al RafeePOLLSTAR_PROJECTpollstar>django-admin --version
3.0.5

(POLLSTAR_PROJECT) C:UsersAhnaaf Al RafeePOLLSTAR_PROJECTpollstar>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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this

the issue is that the place where you trying to create virtual environment doesn't contain manage.py file means your manage.py is not present in your project folder thats why you getting this error. Fix below

always use this pattern when creating a project and running virtual envionment

1.) locate to the directory, lets say you are here C:desktopprojects

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)

run server

python manage.py runserver

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...