Search code examples
pythondjangoimporterror

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:\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?


Solution

  • 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:\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)

    run server

    python manage.py runserver