ImportError: Couldn't import Django

Solution 1:

I think the best way to use django is with virtualenv it's safe and you can install many apps in virtualenv which does not affect any outer space of the system vitualenv uses the default version of python which is same as in your system to install virtualenv

sudo pip install virtualenv

or for python3

sudo pip3 install virtualenv

and then in your dir

mkdir ~/newproject

cd ~/newproject

Now, create a virtual environment within the project directory by typing

virtualenv newenv

To install packages into the isolated environment, you must activate it by typing:

source newenv/bin/activate

now install here with

pip install django

You can verify the installation by typing:

django-admin --version

To leave your virtual environment, you need to issue the deactivate command from anywhere on the system:

deactivate

Solution 2:

You need to install Django, this error is giving because django is not installed.

pip install django

Solution 3:

When you install Django on your computer all things go fine but when you install a Virtual environment it gets separated from all things. You will know it's importance when you will make a final project and deploy it to any cloud or hosting.

Just reinstall Django in the virtual environment and baam:

pip install Django

and then just run the command for testing:

python manage.py runsever

and you are all done.

Solution 4:

You need to use both commands: pip install django and pip3 install django that worked for me

Solution 5:

  1. Check that you have installed Django; by executing import django in python. you mustn't see ModuleNotFoundError if everything's ok.

  2. Check that you have installed virtualenv; by executing virtualenv --version. you must see the version number if everything's ok.

  3. Check that you have enabled virtualenv; there's got to be the name of your virtualenv in your command prompt starting line. enable it by source bin/activate. also, remember to deactivate it every time your job is finished with the virtualenv.

    my terminal changes after enabling virtualenv

  4. Check that your virtualenv includes django. a virtualenv by default has no modules installed. you either have to install django in your virtualenv (even if you have it in your machine already) or use virtualenv --system-site-packages when creating a virtualenv to include system site packages in the virtualenv.

  5. Add django to your path. open python, import django, then run django to see django's path. then add it to your ~/.bashrc (or ~/.zshrc if you're using zsh). more info in here

  6. Install django-admin by running pip install django-admin