Python Forum

Full Version: Python can't import Django and it's in the site-packages directory
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I'm trying to play around with the Django tutorial in the Django website, but I ran into an error and don't understand why I am getting it. I found some info about it in other forums, but the answers talk about setting a virtual environment or adding it to the PYTHONPATH environment variable. I still don't understand virtual environments and I'm trying to run this example without it—probably I'll learn and use one soon—and I have django in the following directory "C:\Users\Ivan\Anaconda3\pkgs\python-3.7.0-hea74fb7_0\Lib\site-packages". Isn't this directory supposed to be correct for python to import Django?

This is the exact command I'm using and the exact error I'm getting:

!python manage.py runserver
Output:
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 on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
Thanks in advance.
django needs a virtual env. to run.

open command prompt pip install virtualenv
virtualenv . (to use a specic python version - virtualenv . -p python3.8 or whatever)

source bin/activate for linux or don't remember the command for windows should not be hard to find

pip install django

django-admin startproject project-name

python-version manage.py runserver

This tutorial is pretty good in helping understand how to get started with django
https://www.youtube.com/watch?v=UmljXZIypDc
Hi, menator. Thank you very much for your reply.

I read something in the tutorial about creating the virtual environment, but I think I remember seeing it was optional. Probably I have a bigger concern right now because here https://docs.djangoproject.com/en/3.0/in...#verifying it tells you to import django to make sure it was installed successfully. However, when I do import django I get
Output:
ModuleNotFoundError: No module named 'django'
. This is what confuses me, because it is in my site-packages folder along with the other packages I have installed via pip. For example, chardet is there, and I can import it and use it normally. Actually, I can run !django-admin startproject project-name and create the project successfully. Probably I need to add it to my PYTHONPATH? If so, my question is, why do I have to do it if it's in my site-packages folder already?
Django needs a virtual environment to run.