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

Question:

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:UsersKampetDesktopPython-Djangomysite>pip install django Requirement already satisfied: django in c:userskampetappdatalocalprograms pythonpython37-32libsite-packages (2.2.4) Requirement already satisfied: pytz in c:userskampetappdatalocalprogramspy thonpython37-32libsite-packages (from django) (2019.2) Requirement already satisfied: sqlparse in c:userskampetappdatalocalprogram spythonpython37-32libsite-packages (from django) (0.3.0)**

# And thats my error
**C:UsersKampetDesktopPython-Djangomysite>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.

Asked By: Kampet

||

Answers:

On the Windows machine, you should activate venv by this command .venvScriptsactivate (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

Answered By: Artem Kolontay

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
Answered By: Artem Dushko

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

Answered By: a_ko

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
Answered By: NA SYD

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>
Answered By: Kalyan

Make sure that you are working on the command prompt.

Use this command ‘

workon your_env_name

Answered By: Susheel B M

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.

Answered By: fekef

In my case, I reinstalled python to a global folder on the C drive and by mistake wrote PYTHONPATH With:Python and not C:PythonScripts in global variables, but in local variables it was written correctly

Answered By: Nicolay

enter image description here
On Windows, you should activate env by this command .envScriptsactivate (please note, you should be in the folder where this env is)

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

Answered By: Localnetwork
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.