Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 381
Kampet Avatar asked Dec 31 '22 17:12

Kampet


2 Answers

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

like image 71
Artem Kolontay Avatar answered Jan 06 '23 03:01

Artem Kolontay


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
like image 26
NA SYD Avatar answered Jan 06 '23 03:01

NA SYD