'ImportError: Couldn't import Django ... Did you forget to activate a virtual environment?

I know there are several questions/answers about this, but I can;t figure out what I should do. I wanted to get started with Django and installed it with pip install and added Python37 and Python37-32 to my environmental variables, and I guess it worked, because I can run several Python commands in my shell. But every time I try to

    python manage.py runserver

it gives me an error.

I also set up my virtual environment and activated it, but I think there's a problem with Django. But because I installed it with pip install django I know it's there and I can use commands like django-admin startapp ... So I guess Django is working. I don't really know what PYTHONPATH means and where to find it. It would be pretty nice if anybody could take a look at my error.

Here you can see that Django is installed : #

**C:\Users\Kampet\Desktop\Python-Django\mysite>pip install django Requirement already satisfied: django in c:\users\kampet\appdata\local\programs\ python\python37-32\lib\site-packages (2.2.4) Requirement already satisfied: pytz in c:\users\kampet\appdata\local\programs\py thon\python37-32\lib\site-packages (from django) (2019.2) Requirement already satisfied: sqlparse in c:\users\kampet\appdata\local\program s\python\python37-32\lib\site-packages (from django) (0.3.0)**

# And thats my error
**C:\Users\Kampet\Desktop\Python-Django\mysite>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 16, in main
    ) from exc
ImportError: Couldn't import Django. Are you sure it's installed and available o
n your PYTHONPATH environment variable? Did you forget to activate a virtual env
ironment?**
###################

Here is where my virtual environment is located.

Python-Django

-----------------mysite

-------------------------main

-------------------------mysite

-------------------------manage.py

-----------------venv

-------------------------Include

-------------------------Lib

-------------------------Scripts

-------------------------pyvenv.cfg

This is my manage.py:

#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys


def main():
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise 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?"
        ) from exc
    execute_from_command_line(sys.argv)


if __name__ == '__main__':
    main()

#

I don't know why it can't find the module "django" / django.core.management I also can't find django.core.management anywhere in my files, but I reinstalled and upgraded django several times. I don't know if this helps you.

Thank you for your time.



Solution 1:[1]

On the Windows machine, you should activate venv by this command .\venv\Scripts\activate (please note, you should be in the folder where this venv is)

Then inside activated venv install Django pip install django and in the same terminal run the server python manage.py runserver

Solution 2:[2]

I had similar issue in my windows 10 system and solved using pipenv. Steps with commands given below.

  1. cd/Go to the project folder
  2. Setup the virtual environment pipenv install
  3. Activate the virtual environment pipenv shell
  4. Install django pip3 install django
  5. Run the project pipenv run python manage.py runserver

Solution 3:[3]

I had similar issue with:

  • django installed globally
  • virtual environment setup and activated
  • django project setup under venv activated

When trying to run the project, it could not import django anyway.

Adding path to manage.py in variables did not help but as mentioned above, installation of django under venv activated actually resolved the issue.

Your case may be different but try this scenario:

  1. Setup venv
  2. Activate venv
  3. Install django
  4. Create django proj
  5. Try to run django proj

Solution 4:[4]

try running it in anaconda prompt instead of cmd, that worked for me

Solution 5:[5]

This error occurs when you install pip packages as a normal user other than root user. pip install <package name> --user command actually creates a directory called as .local in the users home directory under which the packages are installed.

Fix:

  1. Find the django package installed location sudo find / -name 'django'
  2. Copy the path from find command output.
  3. Create export variable in ~/.bashrc file with PYTHONPATH variable.
  4. Save the file and exit.
  5. Exit from the normal user prompt.
  6. Login again as the normal user.
  7. Run the command env to check the PYTHONPATH variable set in the output.
  8. Run the command python manage.py runserver <ip address: port>

Solution 6:[6]

Make sure that you are working on the command prompt.

Use this command '

workon your_env_name

'

Solution 7:[7]

Install Python3, Make sure its on PATH, make sure PIP is installed, Install virtualenv with PIP, setup virtualenv, activate virtualenv, install django.

Or you can try to run the command pipenv shell before start the server with py manage.py runserver in vscode terminal.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Artem Kolontay
Solution 2 NA SYD
Solution 3 Guillaume Raymond
Solution 4 a_ko
Solution 5 Kalyan
Solution 6 Susheel B M
Solution 7